利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(material): accept usable Pexels video renditions
Rudy Celekli550d14fb3231
diff --git a/app/services/material.py b/app/services/material.pyindex 97e31a27..7a584a5e 100644--- a/app/services/material.py+++ b/app/services/material.py@@ -341,7 +341,6 @@ def search_videos_pexels(video_aspect: VideoAspect = VideoAspect.portrait,) -> List[MaterialInfo]:aspect = VideoAspect(video_aspect)- video_orientation = aspect.namevideo_width, video_height = aspect.to_resolution()api_key = get_api_key("pexels_api_keys")headers = {@@ -349,7 +348,9 @@ def search_videos_pexels("User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",}# Build URL- params = {"query": search_term, "per_page": 20, "orientation": video_orientation}+ params = {"query": search_term, "per_page": 20}+ if aspect != VideoAspect.square:+ params["orientation"] = aspect.namequery_url = f"https://api.pexels.com/v1/videos/search?{urlencode(params)}"logger.info(f"searching videos on pexels: term={search_term!r}")@@ -385,7 +386,9 @@ def search_videos_pexels(video_files = v.get("video_files")if not isinstance(video_files, list):continue- # loop through each url to determine the best quality+ # Prefer the smallest rendition that can fill the output canvas.+ # Square output can crop either orientation, as other providers do.+ renditions = []for video in video_files:if not isinstance(video, dict):continue@@ -398,34 +401,36 @@ def search_videos_pexels(if not isinstance(video_url, str) or not video_url:continueif (- _matches_video_aspect(w, h, aspect)- and w == video_width- and h == video_height+ (aspect == VideoAspect.square or _matches_video_aspect(w, h, aspect))+ and w >= video_width+ and h >= video_height):- item = MaterialInfo()- item.provider = "pexels"- item.url = video_url- item.duration = duration- item.source_info = {- "provider": "pexels",- "search_term": search_term,- "asset_id": (- str(v.get("id")) if v.get("id") is not None else None+ renditions.append((w * h, video, w, h, video_url))+ if renditions:+ _, video, w, h, video_url = min(renditions, key=lambda candidate: candidate[0])+ item = MaterialInfo()+ item.provider = "pexels"+ item.url = video_url+ item.duration = duration+ item.source_info = {+ "provider": "pexels",+ "search_term": search_term,+ "asset_id": (+ str(v.get("id")) if v.get("id") is not None else None+ ),+ "source_page": _safe_public_url(v.get("url")),+ "creator": _creator_info(v.get("user")),+ "rendition": {+ "id": (+ str(video.get("id"))+ if video.get("id") is not None+ else None),- "source_page": _safe_public_url(v.get("url")),- "creator": _creator_info(v.get("user")),- "rendition": {- "id": (- str(video.get("id"))- if video.get("id") is not None- else None- ),- "width": w,- "height": h,- },- }- video_items.append(item)- break+ "width": w,+ "height": h,+ },+ }+ video_items.append(item)return video_itemsexcept Exception as e:logger.error(diff --git a/test/services/test_pexels_renditions.py b/test/services/test_pexels_renditions.pynew file mode 100644index 00000000..072f3280--- /dev/null+++ b/test/services/test_pexels_renditions.py@@ -0,0 +1,114 @@+from unittest.mock import MagicMock, patch+import pytest+from app.models.schema import VideoAspect+from app.services import material+++ "aspect,width,height",+ [+ (VideoAspect.portrait, 2160, 3840),+ (VideoAspect.landscape, 3840, 2160),+ (VideoAspect.square, 1920, 1080),+ (VideoAspect.square, 1080, 1920),+ ],+)+def test_pexels_retains_usable_high_resolution_and_square_crop_sources(+ aspect, width, height+):+ response = MagicMock()+ response.json.return_value = {+ "videos": [+ {+ "id": 1,+ "duration": 10,+ "video_files": [+ {+ "id": 2,+ "width": width,+ "height": height,+ "link": "https://example.test/source.mp4",+ }+ ],+ }+ ]+ }+ with (+ patch.object(material, "get_api_key", return_value="fake-key"),+ patch.object(material.requests, "get", return_value=response),+ ):+ found = material.search_videos_pexels("coffee", 5, aspect)+ assert len(found) == 1+ assert found[0].url == "https://example.test/source.mp4"+ assert found[0].source_info["rendition"]["width"] == width+++def test_pexels_still_rejects_wrong_orientation_and_insufficient_dimensions():+ response = MagicMock()+ response.json.return_value = {+ "videos": [+ {+ "id": 1,+ "duration": 10,+ "video_files": [+ {+ "width": 1920,+ "height": 1080,+ "link": "https://example.test/landscape.mp4",+ },+ {+ "width": 720,+ "height": 1280,+ "link": "https://example.test/small.mp4",+ },+ ],+ }+ ]+ }+ with (+ patch.object(material, "get_api_key", return_value="fake-key"),+ patch.object(material.requests, "get", return_value=response),+ ):+ assert material.search_videos_pexels("coffee", 5, VideoAspect.portrait) == []+++def test_pexels_prefers_smallest_adequate_rendition():+ response = MagicMock()+ response.json.return_value = {+ "videos": [+ {+ "duration": 10,+ "video_files": [+ {+ "width": 2160,+ "height": 3840,+ "link": "https://example.test/4k.mp4",+ },+ {+ "width": 1080,+ "height": 1920,+ "link": "https://example.test/hd.mp4",+ },+ ],+ }+ ]+ }+ with (+ patch.object(material, "get_api_key", return_value="fake-key"),+ patch.object(material.requests, "get", return_value=response),+ ):+ found = material.search_videos_pexels("coffee", 5, VideoAspect.portrait)+ assert found[0].url == "https://example.test/hd.mp4"+++def test_square_search_does_not_exclude_crop_compatible_orientations():+ from urllib.parse import parse_qs, urlsplit++ response = MagicMock()+ response.json.return_value = {"videos": []}+ with (+ patch.object(material, "get_api_key", return_value="fake-key"),+ patch.object(material.requests, "get", return_value=response) as request,+ ):+ material.search_videos_pexels("coffee", 5, VideoAspect.square)+ assert "orientation" not in parse_qs(urlsplit(request.call_args.args[0]).query)