mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
989 lines
37 KiB
Python
989 lines
37 KiB
Python
import os
|
|
import random
|
|
import threading
|
|
from pathlib import Path
|
|
from typing import Any, Callable, List
|
|
from urllib.parse import quote_plus, urlencode, urlsplit, urlunsplit
|
|
|
|
import requests
|
|
from loguru import logger
|
|
from moviepy.video.io.VideoFileClip import VideoFileClip
|
|
|
|
from app.config import config
|
|
from app.models.schema import MaterialInfo, VideoAspect, VideoConcatMode
|
|
from app.services import material_cache, task_artifacts
|
|
from app.utils import utils
|
|
|
|
# Thread-safe counter for API key rotation
|
|
_api_key_counter = 0
|
|
_api_key_lock = threading.Lock()
|
|
|
|
|
|
def _safe_public_url(value: Any) -> str | None:
|
|
"""
|
|
只保留可公开展示的 HTTP(S) 页面地址,并移除查询参数和凭据。
|
|
|
|
素材下载地址可能携带 API Key、签名 JWT 或临时 token。任务清单只需要
|
|
帮助用户回到供应商的公开素材页,不应保存鉴权参数;用户信息形式的 URL
|
|
同样拒绝,避免 ``https://user:pass@example.com`` 一类内容落盘。
|
|
"""
|
|
if not isinstance(value, str) or not value.strip():
|
|
return None
|
|
|
|
try:
|
|
parsed = urlsplit(value.strip())
|
|
except ValueError:
|
|
return None
|
|
if (
|
|
parsed.scheme not in {"http", "https"}
|
|
or not parsed.hostname
|
|
or parsed.username is not None
|
|
or parsed.password is not None
|
|
):
|
|
return None
|
|
return urlunsplit((parsed.scheme, parsed.netloc, parsed.path, "", ""))
|
|
|
|
|
|
def _creator_info(value: Any) -> dict[str, str] | None:
|
|
"""从不同供应商的作者结构中提取统一的公开字段。"""
|
|
if isinstance(value, str) and value.strip():
|
|
return {"name": value.strip()}
|
|
if not isinstance(value, dict):
|
|
return None
|
|
|
|
creator: dict[str, str] = {}
|
|
creator_id = value.get("id")
|
|
creator_name = value.get("name") or value.get("username")
|
|
creator_page = _safe_public_url(
|
|
value.get("url") or value.get("profile_url") or value.get("profile_page")
|
|
)
|
|
if creator_id is not None:
|
|
creator["id"] = str(creator_id)
|
|
if creator_name:
|
|
creator["name"] = str(creator_name)
|
|
if creator_page:
|
|
creator["profile_page"] = creator_page
|
|
return creator or None
|
|
|
|
|
|
def _material_source_record(item: MaterialInfo, local_path: str) -> dict[str, Any]:
|
|
"""
|
|
为成功下载的素材生成轻量来源记录。
|
|
|
|
``source_info`` 可能来自缓存,甚至来自外部构造的 ``MaterialInfo``,因此
|
|
不能原样写入。这里按白名单重新构造,只保留公开页面、业务标识和尺寸,
|
|
并只记录本地文件名,避免用户目录或 Docker 挂载路径进入任务文件。
|
|
"""
|
|
source = item.source_info if isinstance(item.source_info, dict) else {}
|
|
record: dict[str, Any] = {
|
|
"provider": str(item.provider or source.get("provider") or ""),
|
|
"local_file": Path(local_path).name,
|
|
"duration": int(item.duration),
|
|
}
|
|
|
|
search_term = source.get("search_term")
|
|
asset_id = source.get("asset_id")
|
|
source_page = _safe_public_url(source.get("source_page"))
|
|
if isinstance(search_term, str) and search_term.strip():
|
|
record["search_term"] = search_term.strip()
|
|
if asset_id not in (None, ""):
|
|
record["asset_id"] = str(asset_id)
|
|
if source_page:
|
|
record["source_page"] = source_page
|
|
|
|
creator = _creator_info(source.get("creator"))
|
|
if creator:
|
|
record["creator"] = creator
|
|
|
|
raw_rendition = source.get("rendition")
|
|
if isinstance(raw_rendition, dict):
|
|
rendition = {}
|
|
for field in ("id", "width", "height"):
|
|
value = raw_rendition.get(field)
|
|
if value not in (None, ""):
|
|
rendition[field] = str(value) if field == "id" else value
|
|
if rendition:
|
|
record["rendition"] = rendition
|
|
return record
|
|
|
|
|
|
def _persist_material_sources(
|
|
task_id: str,
|
|
material_sources: list[dict[str, Any]],
|
|
) -> None:
|
|
"""
|
|
将当前实际下载成功的素材来源补充到任务清单。
|
|
|
|
任务记录是辅助能力,不能改变视频下载函数的返回值,也不能因为写盘失败
|
|
中断成片主流程。``patch_script_data`` 会负责原子替换和异常日志;这里仅在
|
|
成功后记录数量,便于确认任务追溯信息是否已经落盘。
|
|
"""
|
|
try:
|
|
saved = task_artifacts.patch_script_data(
|
|
task_id,
|
|
material_sources=material_sources,
|
|
)
|
|
if saved:
|
|
logger.info(
|
|
f"saved material source records: "
|
|
f"task_id={task_id}, count={len(material_sources)}"
|
|
)
|
|
except Exception as exc:
|
|
# task_artifacts 自身已经按失败降级设计,这里仍保留最后一道隔离,
|
|
# 防止未来实现调整或目录解析异常意外影响素材下载返回值。
|
|
logger.warning(
|
|
"failed to persist material source records: "
|
|
f"task_id={task_id}, error={type(exc).__name__}, detail={exc}"
|
|
)
|
|
|
|
|
|
def _get_tls_verify() -> bool:
|
|
# 默认开启 TLS 证书校验,防止素材搜索和下载过程被中间人篡改。
|
|
# 仅在企业代理、自签证书等明确需要的场景下,允许用户通过
|
|
# `config.toml` 显式设置 `tls_verify = false` 临时关闭。
|
|
tls_verify = config.app.get("tls_verify", True)
|
|
if isinstance(tls_verify, str):
|
|
tls_verify = tls_verify.strip().lower() not in ("0", "false", "no", "off")
|
|
|
|
if not tls_verify:
|
|
logger.warning(
|
|
"TLS certificate verification is disabled by config.app.tls_verify=false. "
|
|
"Only use this in trusted proxy environments."
|
|
)
|
|
|
|
return bool(tls_verify)
|
|
|
|
|
|
def get_api_key(cfg_key: str):
|
|
api_keys = config.app.get(cfg_key)
|
|
if not api_keys:
|
|
raise ValueError(
|
|
f"\n\n##### {cfg_key} is not set #####\n\n"
|
|
f"Please set it in the config.toml file: {config.config_file}\n"
|
|
)
|
|
|
|
# if only one key is provided, return it
|
|
if isinstance(api_keys, str):
|
|
return api_keys
|
|
|
|
global _api_key_counter
|
|
with _api_key_lock:
|
|
_api_key_counter += 1
|
|
return api_keys[_api_key_counter % len(api_keys)]
|
|
|
|
|
|
def _redact_secret(message: str, secret: str) -> str:
|
|
"""
|
|
对即将写入日志的异常文本做最小范围脱敏。
|
|
|
|
requests 的连接异常可能包含完整请求 URL,而 Pixabay API Key 通过查询
|
|
参数传递。这里同时替换原始值和 URL 编码值,既保留网络错误信息用于排查,
|
|
又避免密钥进入日志文件。
|
|
"""
|
|
safe_message = str(message)
|
|
if not secret:
|
|
return safe_message
|
|
|
|
safe_message = safe_message.replace(secret, "***")
|
|
encoded_secret = quote_plus(secret)
|
|
if encoded_secret != secret:
|
|
safe_message = safe_message.replace(encoded_secret, "***")
|
|
return safe_message
|
|
|
|
|
|
def _redact_request_error(error: Exception, *secrets: str) -> str:
|
|
"""
|
|
保留网络异常的可排查信息,同时移除 API Key 和代理凭据。
|
|
|
|
直接只记录异常类型会丢失 DNS、证书、超时等关键上下文;直接记录原始异常
|
|
又可能回显完整请求 URL。统一入口可以让三个素材供应商使用相同脱敏规则。
|
|
"""
|
|
safe_message = str(error)
|
|
for secret in secrets:
|
|
safe_message = _redact_secret(safe_message, str(secret or ""))
|
|
for proxy_url in config.proxy.values():
|
|
safe_message = _redact_secret(safe_message, str(proxy_url))
|
|
return safe_message
|
|
|
|
|
|
def _is_cloudflare_challenge(response: requests.Response) -> bool:
|
|
"""
|
|
识别 Cloudflare 返回的 HTML Challenge,而不是把它当成 Pixabay JSON。
|
|
|
|
Cloudflare 通常会设置 `cf-mitigated: challenge`;部分部署只返回带有
|
|
"Just a moment" 或 challenge-platform 的 HTML,因此保留内容特征兜底。
|
|
响应正文仅在内存中判断,不写入日志,避免记录无价值的大段 HTML。
|
|
"""
|
|
headers = getattr(response, "headers", {}) or {}
|
|
if str(headers.get("cf-mitigated", "")).lower() == "challenge":
|
|
return True
|
|
|
|
content_type = str(headers.get("content-type", "")).lower()
|
|
if "text/html" not in content_type:
|
|
return False
|
|
|
|
body = str(getattr(response, "text", "")).lower()
|
|
return "just a moment" in body or "/cdn-cgi/challenge-platform/" in body
|
|
|
|
|
|
def _matches_video_aspect(
|
|
width: Any,
|
|
height: Any,
|
|
video_aspect: VideoAspect,
|
|
*,
|
|
is_vertical: Any = None,
|
|
) -> bool:
|
|
"""
|
|
判断远端素材是否与目标画面方向一致。
|
|
|
|
Pexels、Pixabay 和 Coverr 的响应字段并不统一,因此先使用宽高做可靠判断;
|
|
Coverr 部分历史响应缺少尺寸时,再使用明确的 ``is_vertical`` 布尔值兜底。
|
|
无法确认方向的素材直接跳过,避免竖屏任务混入横屏素材并在成片中产生黑边。
|
|
"""
|
|
aspect = VideoAspect(video_aspect)
|
|
try:
|
|
normalized_width = int(float(width))
|
|
normalized_height = int(float(height))
|
|
except (TypeError, ValueError):
|
|
normalized_width = 0
|
|
normalized_height = 0
|
|
|
|
if normalized_width > 0 and normalized_height > 0:
|
|
if aspect == VideoAspect.portrait:
|
|
return normalized_height > normalized_width
|
|
if aspect == VideoAspect.landscape:
|
|
return normalized_width > normalized_height
|
|
return normalized_width == normalized_height
|
|
|
|
if isinstance(is_vertical, bool) and aspect != VideoAspect.square:
|
|
return is_vertical == (aspect == VideoAspect.portrait)
|
|
return False
|
|
|
|
|
|
def _filter_materials_by_aspect(
|
|
items: List[MaterialInfo],
|
|
video_aspect: VideoAspect,
|
|
) -> List[MaterialInfo]:
|
|
"""
|
|
对缓存结果再次校验方向。
|
|
|
|
素材搜索缓存最长保留 24 小时,升级前写入的缓存可能包含方向不匹配的素材。
|
|
在统一缓存入口过滤可以让修复立即生效,也能防御第三方 Provider 或旧缓存
|
|
遗漏远端筛选。无法读取 rendition 尺寸的旧条目按未验证处理并跳过。
|
|
"""
|
|
aspect = VideoAspect(video_aspect)
|
|
if aspect == VideoAspect.square:
|
|
# Pixabay 和 Coverr 很少提供原生方形素材。方形输出沿用既有行为,
|
|
# 接受可用候选并交给视频合成阶段裁剪,避免升级后 1:1 任务无素材。
|
|
return list(items)
|
|
|
|
filtered_items = []
|
|
for item in items:
|
|
source_info = item.source_info if isinstance(item.source_info, dict) else {}
|
|
rendition = source_info.get("rendition")
|
|
rendition = rendition if isinstance(rendition, dict) else {}
|
|
if _matches_video_aspect(
|
|
rendition.get("width"),
|
|
rendition.get("height"),
|
|
aspect,
|
|
):
|
|
filtered_items.append(item)
|
|
return filtered_items
|
|
|
|
|
|
def search_videos_pexels(
|
|
search_term: str,
|
|
minimum_duration: int,
|
|
video_aspect: VideoAspect = VideoAspect.portrait,
|
|
) -> List[MaterialInfo]:
|
|
aspect = VideoAspect(video_aspect)
|
|
video_orientation = aspect.name
|
|
video_width, video_height = aspect.to_resolution()
|
|
api_key = get_api_key("pexels_api_keys")
|
|
headers = {
|
|
"Authorization": api_key,
|
|
"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}
|
|
query_url = f"https://api.pexels.com/v1/videos/search?{urlencode(params)}"
|
|
logger.info(f"searching videos on pexels: term={search_term!r}")
|
|
|
|
try:
|
|
r = requests.get(
|
|
query_url,
|
|
headers=headers,
|
|
proxies=config.proxy,
|
|
verify=_get_tls_verify(),
|
|
timeout=(30, 60),
|
|
)
|
|
response = r.json()
|
|
video_items = []
|
|
if "videos" not in response:
|
|
logger.error("pexels video search returned an unsupported response")
|
|
return video_items
|
|
videos = response["videos"]
|
|
# loop through each video in the result
|
|
for v in videos:
|
|
duration = v["duration"]
|
|
# check if video has desired minimum duration
|
|
if duration < minimum_duration:
|
|
continue
|
|
video_files = v["video_files"]
|
|
# loop through each url to determine the best quality
|
|
for video in video_files:
|
|
w = int(video["width"])
|
|
h = int(video["height"])
|
|
if (
|
|
_matches_video_aspect(w, h, aspect)
|
|
and w == video_width
|
|
and h == video_height
|
|
):
|
|
item = MaterialInfo()
|
|
item.provider = "pexels"
|
|
item.url = video["link"]
|
|
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
|
|
),
|
|
"width": w,
|
|
"height": h,
|
|
},
|
|
}
|
|
video_items.append(item)
|
|
break
|
|
return video_items
|
|
except Exception as e:
|
|
logger.error(
|
|
"pexels video search failed: "
|
|
f"error={type(e).__name__}, detail={_redact_request_error(e, api_key)}"
|
|
)
|
|
|
|
return []
|
|
|
|
|
|
def search_videos_pixabay(
|
|
search_term: str,
|
|
minimum_duration: int,
|
|
video_aspect: VideoAspect = VideoAspect.portrait,
|
|
) -> List[MaterialInfo]:
|
|
aspect = VideoAspect(video_aspect)
|
|
|
|
video_width, video_height = aspect.to_resolution()
|
|
|
|
api_key = get_api_key("pixabay_api_keys")
|
|
# Build URL
|
|
params = {
|
|
"q": search_term,
|
|
"video_type": "all", # Accepted values: "all", "film", "animation"
|
|
"per_page": 50,
|
|
"key": api_key,
|
|
}
|
|
query_url = f"https://pixabay.com/api/videos/?{urlencode(params)}"
|
|
logger.info(
|
|
f"searching videos on pixabay: term={search_term!r}, "
|
|
f"proxy_enabled={bool(config.proxy)}"
|
|
)
|
|
|
|
try:
|
|
r = requests.get(
|
|
query_url, proxies=config.proxy, verify=_get_tls_verify(), timeout=(30, 60)
|
|
)
|
|
status_code = int(getattr(r, "status_code", 200))
|
|
headers = getattr(r, "headers", {}) or {}
|
|
content_type = str(headers.get("content-type", ""))
|
|
retry_after = headers.get("retry-after")
|
|
cf_ray = headers.get("cf-ray")
|
|
|
|
if _is_cloudflare_challenge(r):
|
|
logger.error(
|
|
"pixabay search was blocked by a Cloudflare challenge: "
|
|
f"status={status_code}, cf_ray={cf_ray or 'unknown'}. "
|
|
"Check the server network or proxy, or use Pexels/Coverr instead."
|
|
)
|
|
return []
|
|
|
|
if status_code == 429:
|
|
logger.error(
|
|
"pixabay API rate limit exceeded: "
|
|
f"status=429, retry_after={retry_after or 'unknown'}"
|
|
)
|
|
return []
|
|
|
|
if status_code >= 400:
|
|
logger.error(
|
|
"pixabay search request failed: "
|
|
f"status={status_code}, content_type={content_type or 'unknown'}"
|
|
)
|
|
return []
|
|
|
|
try:
|
|
response = r.json()
|
|
except ValueError:
|
|
logger.error(
|
|
"pixabay returned an unexpected non-JSON response: "
|
|
f"status={status_code}, content_type={content_type or 'unknown'}"
|
|
)
|
|
return []
|
|
|
|
video_items = []
|
|
if "hits" not in response:
|
|
logger.error("pixabay video search returned an unsupported response")
|
|
return video_items
|
|
videos = response["hits"]
|
|
# loop through each video in the result
|
|
for v in videos:
|
|
duration = v["duration"]
|
|
# check if video has desired minimum duration
|
|
if duration < minimum_duration:
|
|
continue
|
|
video_files = v["videos"]
|
|
# loop through each url to determine the best quality
|
|
for video_type in video_files:
|
|
video = video_files[video_type]
|
|
try:
|
|
w = int(video["width"])
|
|
h = int(video["height"])
|
|
except (KeyError, TypeError, ValueError):
|
|
continue
|
|
# Pixabay 很少返回原生方形视频;1:1 输出继续接受满足分辨率的
|
|
# 候选并由合成阶段裁剪。横竖屏则必须严格匹配目标方向。
|
|
orientation_matches = aspect == VideoAspect.square or (
|
|
_matches_video_aspect(w, h, aspect)
|
|
)
|
|
if orientation_matches and w >= video_width:
|
|
item = MaterialInfo()
|
|
item.provider = "pixabay"
|
|
item.url = video["url"]
|
|
item.duration = duration
|
|
item.source_info = {
|
|
"provider": "pixabay",
|
|
"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("pageURL")),
|
|
"creator": _creator_info(
|
|
{
|
|
"id": v.get("user_id"),
|
|
"name": v.get("user"),
|
|
}
|
|
),
|
|
"rendition": {
|
|
"id": video_type,
|
|
"width": w,
|
|
"height": video.get("height"),
|
|
},
|
|
}
|
|
video_items.append(item)
|
|
break
|
|
return video_items
|
|
except Exception as e:
|
|
error_message = _redact_request_error(e, api_key)
|
|
logger.error(
|
|
"pixabay search request failed: "
|
|
f"error={type(e).__name__}, detail={error_message}"
|
|
)
|
|
|
|
return []
|
|
|
|
|
|
def search_videos_coverr(
|
|
search_term: str,
|
|
minimum_duration: int,
|
|
video_aspect: VideoAspect = VideoAspect.portrait,
|
|
) -> List[MaterialInfo]:
|
|
"""
|
|
Coverr (https://coverr.co) - free HD/4K stock videos,
|
|
subject to Coverr license terms (https://coverr.co/license).
|
|
|
|
Coverr API notes (based on official docs at api.coverr.co/docs/):
|
|
- 鉴权: Authorization: Bearer <api_key>
|
|
- 搜索端点: GET /videos?query=...,响应结构 {"hits": [...], ...}
|
|
- 加 ?urls=true 在搜索响应里直接返回 mp4 直链
|
|
- URL 是 signed JWT(绑定 API key,无过期时间)
|
|
- Coverr 支持通过 filter=is_vertical:true/false 筛选横竖屏素材;
|
|
响应返回后仍根据 max_width/max_height 或 is_vertical 做本地校验
|
|
- duration 字段同时存在 number 和 string 两种形态,本函数都接受
|
|
|
|
本函数使用 urls.mp4_download 字段作为下载地址 —— 按 Coverr 官方文档
|
|
(https://api.coverr.co/docs/videos/#download-a-video) 的说法,
|
|
GET 这个 URL 本身就被 Coverr 当作一次合法的 download 事件计入统计,
|
|
无需再调用 PATCH /videos/:id/stats/downloads。
|
|
"""
|
|
aspect = VideoAspect(video_aspect)
|
|
api_key = get_api_key("coverr_api_keys")
|
|
headers = {"Authorization": f"Bearer {api_key}"}
|
|
params = {
|
|
"query": search_term,
|
|
"page_size": 20,
|
|
"urls": "true",
|
|
"sort": "popular",
|
|
}
|
|
# 服务端方向筛选可以直接从完整搜索结果中返回目标素材,避免先取热门结果再
|
|
# 本地过滤导致竖屏候选为空。方形素材没有对应布尔条件,继续依赖本地宽高校验。
|
|
if aspect == VideoAspect.portrait:
|
|
params["filter"] = "is_vertical:true"
|
|
elif aspect == VideoAspect.landscape:
|
|
params["filter"] = "is_vertical:false"
|
|
query_url = f"https://api.coverr.co/videos?{urlencode(params)}"
|
|
logger.info(f"searching videos on coverr: term={search_term!r}")
|
|
|
|
try:
|
|
r = requests.get(
|
|
query_url,
|
|
headers=headers,
|
|
proxies=config.proxy,
|
|
verify=_get_tls_verify(),
|
|
timeout=(30, 60),
|
|
)
|
|
response = r.json()
|
|
video_items: List[MaterialInfo] = []
|
|
|
|
if not isinstance(response, dict) or "hits" not in response:
|
|
logger.error("coverr video search returned an unsupported response")
|
|
return video_items
|
|
|
|
for v in response["hits"]:
|
|
# duration 在不同响应里可能是 number(11.625) 或 string("10.500000")
|
|
try:
|
|
duration = int(float(v.get("duration") or 0))
|
|
except (TypeError, ValueError):
|
|
continue
|
|
if duration < minimum_duration:
|
|
continue
|
|
|
|
video_id = v.get("id")
|
|
mp4_download_url = (v.get("urls") or {}).get("mp4_download")
|
|
if not video_id or not mp4_download_url:
|
|
continue
|
|
if aspect != VideoAspect.square and not _matches_video_aspect(
|
|
v.get("max_width"),
|
|
v.get("max_height"),
|
|
aspect,
|
|
is_vertical=v.get("is_vertical"),
|
|
):
|
|
continue
|
|
|
|
item = MaterialInfo()
|
|
item.provider = "coverr"
|
|
item.url = mp4_download_url
|
|
item.duration = duration
|
|
item.source_info = {
|
|
"provider": "coverr",
|
|
"search_term": search_term,
|
|
"asset_id": str(video_id),
|
|
"source_page": _safe_public_url(v.get("canonical_url") or v.get("url")),
|
|
"creator": _creator_info(v.get("creator") or v.get("author")),
|
|
"rendition": {
|
|
"id": "mp4_download",
|
|
"width": v.get("max_width"),
|
|
"height": v.get("max_height"),
|
|
},
|
|
}
|
|
video_items.append(item)
|
|
return video_items
|
|
except Exception as e:
|
|
logger.error(
|
|
"coverr video search failed: "
|
|
f"error={type(e).__name__}, detail={_redact_request_error(e, api_key)}"
|
|
)
|
|
|
|
return []
|
|
|
|
|
|
def save_video(video_url: str, save_dir: str = "") -> str:
|
|
if not save_dir:
|
|
save_dir = utils.storage_dir("cache_videos")
|
|
|
|
if not os.path.exists(save_dir):
|
|
os.makedirs(save_dir)
|
|
|
|
url_without_query = video_url.split("?")[0]
|
|
url_hash = utils.md5(url_without_query)
|
|
video_id = f"vid-{url_hash}"
|
|
video_path = f"{save_dir}/{video_id}.mp4"
|
|
|
|
# if video already exists, return the path
|
|
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
|
|
logger.info(f"video already exists: {video_path}")
|
|
return video_path
|
|
|
|
headers = {
|
|
"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"
|
|
}
|
|
|
|
# if video does not exist, download it
|
|
with open(video_path, "wb") as f:
|
|
f.write(
|
|
requests.get(
|
|
video_url,
|
|
headers=headers,
|
|
proxies=config.proxy,
|
|
verify=_get_tls_verify(),
|
|
timeout=(60, 240),
|
|
).content
|
|
)
|
|
|
|
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
|
|
clip = None
|
|
try:
|
|
clip = VideoFileClip(video_path)
|
|
duration = clip.duration
|
|
fps = clip.fps
|
|
if duration > 0 and fps > 0:
|
|
return video_path
|
|
except Exception as e:
|
|
logger.warning(f"invalid video file: {video_path} => {str(e)}")
|
|
try:
|
|
os.remove(video_path)
|
|
except Exception as remove_error:
|
|
logger.warning(
|
|
f"failed to remove invalid video file: {video_path}, error: {str(remove_error)}"
|
|
)
|
|
finally:
|
|
if clip is not None:
|
|
try:
|
|
clip.close()
|
|
except Exception as close_error:
|
|
logger.warning(
|
|
f"failed to close video clip: {video_path}, error: {str(close_error)}"
|
|
)
|
|
return ""
|
|
|
|
|
|
def _search_videos_with_cache(
|
|
provider: str,
|
|
search_videos: Callable[..., List[MaterialInfo]],
|
|
search_term: str,
|
|
minimum_duration: int,
|
|
video_aspect: VideoAspect,
|
|
) -> List[MaterialInfo]:
|
|
"""
|
|
统一处理三个在线素材源的 24 小时搜索缓存。
|
|
|
|
缓存只包裹搜索 API,不改变后续视频下载与去重逻辑。远端返回空列表时不写
|
|
缓存,因为现有 provider 接口使用空列表同时表示“没有结果”和“请求失败”;
|
|
在两者尚未拆分为明确结果类型前,宁可下次重试,也不能把临时故障缓存一天。
|
|
"""
|
|
cache_args = {
|
|
"provider": provider,
|
|
"search_term": search_term,
|
|
"minimum_duration": minimum_duration,
|
|
"video_aspect": video_aspect,
|
|
}
|
|
|
|
def load_cache_safely() -> List[MaterialInfo] | None:
|
|
try:
|
|
return material_cache.load_material_search_cache(**cache_args)
|
|
except Exception as exc:
|
|
# 缓存是可选优化,任何缓存实现异常都必须按未命中处理,不能阻断
|
|
# Pexels、Pixabay 或 Coverr 的正常远端搜索。
|
|
logger.warning(
|
|
"material search cache read failed, continue with remote search: "
|
|
f"provider={provider}, error={type(exc).__name__}, detail={exc}"
|
|
)
|
|
return None
|
|
|
|
def load_matching_cache() -> tuple[List[MaterialInfo] | None, int]:
|
|
cached_items = load_cache_safely()
|
|
if cached_items is None:
|
|
return None, 0
|
|
|
|
filtered_cached_items = _filter_materials_by_aspect(
|
|
cached_items,
|
|
video_aspect,
|
|
)
|
|
ignored_count = len(cached_items) - len(filtered_cached_items)
|
|
if ignored_count:
|
|
# 旧版本缓存可能混入其它方向的素材。即使仍有少量可用条目,也要刷新
|
|
# 完整候选集,否则在缓存有效期内会反复使用同一批少量视频。
|
|
return None, ignored_count
|
|
return filtered_cached_items, 0
|
|
|
|
cached_items, ignored_count = load_matching_cache()
|
|
if cached_items is not None:
|
|
return cached_items
|
|
if ignored_count:
|
|
logger.info(
|
|
"material search cache contains mismatched orientations, "
|
|
f"refresh from provider: provider={provider}, term={search_term!r}, "
|
|
f"ignored={ignored_count}"
|
|
)
|
|
|
|
cache_lock = material_cache.get_material_search_cache_lock(**cache_args)
|
|
with cache_lock:
|
|
# 等待相同搜索条件的线程完成后再次读取,避免多个 API 任务在首次缓存
|
|
# 未命中时同时请求远端,降低第三方接口限流和风控触发概率。
|
|
cached_items, _ = load_matching_cache()
|
|
if cached_items is not None:
|
|
return cached_items
|
|
|
|
items = search_videos(
|
|
search_term=search_term,
|
|
minimum_duration=minimum_duration,
|
|
video_aspect=video_aspect,
|
|
)
|
|
# Provider 正常会写入当前关键词,但测试替身、第三方扩展或旧实现可能
|
|
# 遗漏或携带错误值。缓存读取会根据缓存键恢复该字段,因此远端结果也在
|
|
# 同一入口校正,保证首次搜索与缓存命中的任务来源记录保持一致。
|
|
for item in items:
|
|
if isinstance(item.source_info, dict):
|
|
item.source_info = dict(item.source_info)
|
|
item.source_info["search_term"] = search_term
|
|
if items:
|
|
try:
|
|
material_cache.save_material_search_cache(
|
|
**cache_args,
|
|
items=items,
|
|
)
|
|
except Exception as exc:
|
|
logger.warning(
|
|
"material search cache write failed, use remote results: "
|
|
f"provider={provider}, error={type(exc).__name__}, detail={exc}"
|
|
)
|
|
return items
|
|
|
|
|
|
def download_videos(
|
|
task_id: str,
|
|
search_terms: List[str],
|
|
source: str = "pexels",
|
|
video_aspect: VideoAspect = VideoAspect.portrait,
|
|
video_concat_mode: VideoConcatMode = VideoConcatMode.random,
|
|
audio_duration: float = 0.0,
|
|
max_clip_duration: int = 5,
|
|
match_script_order: bool = False,
|
|
) -> List[str]:
|
|
provider = "pexels"
|
|
remote_search_videos = search_videos_pexels
|
|
if source == "pixabay":
|
|
provider = "pixabay"
|
|
remote_search_videos = search_videos_pixabay
|
|
elif source == "coverr":
|
|
provider = "coverr"
|
|
remote_search_videos = search_videos_coverr
|
|
|
|
def search_videos(
|
|
search_term: str,
|
|
minimum_duration: int,
|
|
video_aspect: VideoAspect,
|
|
) -> List[MaterialInfo]:
|
|
return _search_videos_with_cache(
|
|
provider=provider,
|
|
search_videos=remote_search_videos,
|
|
search_term=search_term,
|
|
minimum_duration=minimum_duration,
|
|
video_aspect=video_aspect,
|
|
)
|
|
|
|
material_directory = config.app.get("material_directory", "").strip()
|
|
if material_directory == "task":
|
|
material_directory = utils.task_dir(task_id)
|
|
elif material_directory and not os.path.isdir(material_directory):
|
|
material_directory = ""
|
|
|
|
if match_script_order:
|
|
return _download_videos_by_script_order(
|
|
task_id=task_id,
|
|
search_terms=search_terms,
|
|
search_videos=search_videos,
|
|
video_aspect=video_aspect,
|
|
audio_duration=audio_duration,
|
|
max_clip_duration=max_clip_duration,
|
|
material_directory=material_directory,
|
|
)
|
|
|
|
valid_video_items = []
|
|
valid_video_urls = []
|
|
found_duration = 0.0
|
|
for search_term in search_terms:
|
|
video_items = search_videos(
|
|
search_term=search_term,
|
|
minimum_duration=max_clip_duration,
|
|
video_aspect=video_aspect,
|
|
)
|
|
logger.info(f"found {len(video_items)} videos for '{search_term}'")
|
|
|
|
for item in video_items:
|
|
if item.url not in valid_video_urls:
|
|
valid_video_items.append(item)
|
|
valid_video_urls.append(item.url)
|
|
found_duration += item.duration
|
|
|
|
logger.info(
|
|
f"found total videos: {len(valid_video_items)}, required duration: {audio_duration} seconds, found duration: {found_duration} seconds"
|
|
)
|
|
video_paths = []
|
|
material_sources: list[dict[str, Any]] = []
|
|
|
|
concat_mode_value = getattr(video_concat_mode, "value", video_concat_mode)
|
|
if concat_mode_value == VideoConcatMode.random.value:
|
|
random.shuffle(valid_video_items)
|
|
|
|
total_duration = 0.0
|
|
for item in valid_video_items:
|
|
try:
|
|
source_info = item.source_info if isinstance(item.source_info, dict) else {}
|
|
logger.info(
|
|
f"downloading {item.provider} video: "
|
|
f"asset_id={source_info.get('asset_id') or 'unknown'}"
|
|
)
|
|
saved_video_path = save_video(
|
|
video_url=item.url, save_dir=material_directory
|
|
)
|
|
if saved_video_path:
|
|
logger.info(f"video saved: {saved_video_path}")
|
|
video_paths.append(saved_video_path)
|
|
try:
|
|
material_sources.append(
|
|
_material_source_record(item, saved_video_path)
|
|
)
|
|
except Exception as source_error:
|
|
# 来源记录异常不能把已经成功下载的素材视为下载失败,更不能
|
|
# 阻断视频生成;保留供应商和异常类型用于后续定位。
|
|
logger.warning(
|
|
"failed to prepare material source record: "
|
|
f"provider={item.provider}, "
|
|
f"error={type(source_error).__name__}, detail={source_error}"
|
|
)
|
|
seconds = min(max_clip_duration, item.duration)
|
|
total_duration += seconds
|
|
if total_duration > audio_duration:
|
|
logger.info(
|
|
f"total duration of downloaded videos: {total_duration} seconds, skip downloading more"
|
|
)
|
|
break
|
|
except Exception as e:
|
|
logger.error(
|
|
"failed to download material video: "
|
|
f"provider={item.provider}, error={type(e).__name__}, "
|
|
f"detail={_redact_request_error(e, item.url)}"
|
|
)
|
|
logger.success(f"downloaded {len(video_paths)} videos")
|
|
_persist_material_sources(task_id, material_sources)
|
|
return video_paths
|
|
|
|
|
|
def _download_videos_by_script_order(
|
|
task_id: str,
|
|
search_terms: List[str],
|
|
search_videos,
|
|
video_aspect: VideoAspect,
|
|
audio_duration: float,
|
|
max_clip_duration: int,
|
|
material_directory: str,
|
|
) -> List[str]:
|
|
"""
|
|
按脚本文案顺序下载素材。
|
|
|
|
默认下载逻辑会把所有关键词的候选素材合并成一个大列表;如果第一个
|
|
关键词返回很多结果,最终下载时可能一直消耗这个关键词的素材,后续
|
|
脚本主题就排不上时间线。这里按关键词分组后轮询下载:
|
|
第 1 轮取每个关键词的第 1 个候选,第 2 轮取每个关键词的第 2 个候选。
|
|
这样在不重写视频合成引擎的前提下,尽量保证素材顺序贴近文案顺序。
|
|
"""
|
|
logger.info("downloading videos with script-order material matching")
|
|
candidate_groups = []
|
|
valid_video_urls = set()
|
|
found_duration = 0.0
|
|
|
|
for search_term in search_terms:
|
|
video_items = search_videos(
|
|
search_term=search_term,
|
|
minimum_duration=max_clip_duration,
|
|
video_aspect=video_aspect,
|
|
)
|
|
logger.info(f"found {len(video_items)} videos for '{search_term}'")
|
|
|
|
term_items = []
|
|
for item in video_items:
|
|
if item.url in valid_video_urls:
|
|
continue
|
|
term_items.append(item)
|
|
valid_video_urls.add(item.url)
|
|
found_duration += item.duration
|
|
|
|
if term_items:
|
|
candidate_groups.append((search_term, term_items))
|
|
|
|
logger.info(
|
|
f"found total ordered video candidates: {sum(len(items) for _, items in candidate_groups)}, "
|
|
f"required duration: {audio_duration} seconds, found duration: {found_duration} seconds"
|
|
)
|
|
|
|
video_paths = []
|
|
material_sources: list[dict[str, Any]] = []
|
|
total_duration = 0.0
|
|
candidate_index = 0
|
|
while candidate_groups and total_duration <= audio_duration:
|
|
has_candidate = False
|
|
for search_term, term_items in candidate_groups:
|
|
if candidate_index >= len(term_items):
|
|
continue
|
|
|
|
has_candidate = True
|
|
item = term_items[candidate_index]
|
|
try:
|
|
source_info = (
|
|
item.source_info if isinstance(item.source_info, dict) else {}
|
|
)
|
|
logger.info(
|
|
f"downloading ordered {item.provider} video for {search_term!r}: "
|
|
f"asset_id={source_info.get('asset_id') or 'unknown'}"
|
|
)
|
|
saved_video_path = save_video(
|
|
video_url=item.url, save_dir=material_directory
|
|
)
|
|
if saved_video_path:
|
|
logger.info(f"video saved: {saved_video_path}")
|
|
video_paths.append(saved_video_path)
|
|
try:
|
|
material_sources.append(
|
|
_material_source_record(item, saved_video_path)
|
|
)
|
|
except Exception as source_error:
|
|
logger.warning(
|
|
"failed to prepare ordered material source record: "
|
|
f"provider={item.provider}, "
|
|
f"error={type(source_error).__name__}, "
|
|
f"detail={source_error}"
|
|
)
|
|
total_duration += min(max_clip_duration, item.duration)
|
|
if total_duration > audio_duration:
|
|
logger.info(
|
|
f"total duration of downloaded videos: {total_duration} seconds, skip downloading more"
|
|
)
|
|
break
|
|
except Exception as e:
|
|
logger.error(
|
|
"failed to download ordered material video: "
|
|
f"provider={item.provider}, error={type(e).__name__}, "
|
|
f"detail={_redact_request_error(e, item.url)}"
|
|
)
|
|
|
|
if not has_candidate:
|
|
break
|
|
candidate_index += 1
|
|
|
|
logger.success(f"downloaded {len(video_paths)} ordered videos")
|
|
_persist_material_sources(task_id, material_sources)
|
|
return video_paths
|
|
|
|
|
|
if __name__ == "__main__":
|
|
download_videos(
|
|
"test123", ["Money Exchange Medium"], audio_duration=100, source="pixabay"
|
|
)
|