Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(video): encode with the BT.709 matrix and tag the color

heojunfo6d26398d64d8
diff --git a/app/services/video.py b/app/services/video.py
index 3956644a..7a0d97b8 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -115,6 +115,15 @@ _SUPPORTED_VIDEO_CODECS = (
"h264_videotoolbox",
)
_runtime_disabled_video_codecs = set()
+# MoviePy pipes sRGB frames, and ffmpeg's default RGB→YUV matrix is BT.601 with no color tags,
+# while players and YouTube decode untagged HD as BT.709 and shift the colors. Convert with the
+# BT.709 matrix and tag the stream; setparams writes the tags the -color_* flags alone do not.
+# The pixel format stays the encoder's choice: MoviePy leaves odd-sized frames to libx264.
+_BT709_VIDEO_FILTER = (
+ "scale=out_color_matrix=bt709:out_range=tv,"
+ "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=tv"
+)
+_BT709_FFMPEG_PARAMS = ["-vf", _BT709_VIDEO_FILTER]
def _get_subtitle_spring_scale(time_seconds: float, duration_seconds: float) -> float:
@@ -428,6 +437,7 @@ def _write_videofile_with_codec_fallback(
硬件编码器是否可用不仅取决于 FFmpeg,还取决于显卡、驱动和当前运行环境。
生成任务不能因为高级编码器不可用而整体失败,所以这里把回退集中处理。
"""
+ kwargs.setdefault("ffmpeg_params", _BT709_FFMPEG_PARAMS)
if atomic_output:
# Final videos can be downloaded by path while they are being rendered.
# Keep both failed encodes and in-progress writes away from that path.
@@ -648,6 +658,8 @@ def concat_video_clips_with_ffmpeg(
codec,
"-threads",
str(threads or 2),
+ "-vf",
+ _BT709_VIDEO_FILTER,
"-pix_fmt",
"yuv420p",
]
@@ -1822,7 +1834,9 @@ def render_image_zoom_video(image_path: str, clip_duration: int = 5) -> str:
dir=os.path.dirname(os.path.abspath(video_file)),
)
os.close(descriptor)
- final_clip.write_videofile(temp_path, fps=30, logger=None)
+ final_clip.write_videofile(
+ temp_path, fps=30, logger=None, ffmpeg_params=_BT709_FFMPEG_PARAMS
+ )
finally:
close_clip(final_clip)
os.replace(temp_path, video_file)
diff --git a/test/services/test_video_color_tags.py b/test/services/test_video_color_tags.py
new file mode 100644
index 00000000..a99f6573
--- /dev/null
+++ b/test/services/test_video_color_tags.py
@@ -0,0 +1,59 @@
+import re
+import subprocess
+
+import numpy as np
+from PIL import Image
+
+from app.services import video
+
+# A saturated green: BT.601 encodes decoded as BT.709 lose the most here.
+SOURCE_RGB = (40, 180, 60)
+
+
+def _stream_color(binary, path):
+ # ffprobe is not bundled with imageio-ffmpeg; ffmpeg's own stream line carries the same tags.
+ result = subprocess.run([binary, "-hide_banner", "-i", str(path)], capture_output=True, text=True)
+ match = re.search(r"Video: h264.*?, (yuv420p\([^)]*\))", result.stderr)
+ assert match, result.stderr
+ return match.group(1)
+
+
+def _decode_as_player(binary, path):
+ # Decode the way players and YouTube treat HD: BT.709, limited range.
+ raw = subprocess.run(
+ [binary, "-v", "error", "-i", str(path), "-frames:v", "1",
+ "-vf", "scale=in_color_matrix=bt709:in_range=tv,format=rgb24,crop=2:2", "-f", "rawvideo", "-"],
+ check=True, capture_output=True,
+ ).stdout
+ return np.frombuffer(raw, dtype=np.uint8).reshape(2, 2, 3)[0, 0].astype(int)
+
+
+def _assert_bt709(binary, path):
+ assert _stream_color(binary, path).startswith("yuv420p(tv, bt709")
+ # Each 4:2:0 limited-range generation rounds by a level or two; a BT.601 encode is ~20 off.
+ assert np.abs(_decode_as_player(binary, path) - SOURCE_RGB).max() <= 8
+
+
+def test_clip_concat_and_image_renders_are_bt709(tmp_path, monkeypatch):
+ binary = video.utils.get_ffmpeg_binary()
+ monkeypatch.setattr(video, "_get_effective_video_codec", lambda *args: "libx264")
+ image = tmp_path / "still.png"
+ Image.new("RGB", (64, 64), SOURCE_RGB).save(image)
+
+ # Stock footage arrives tagged BT.709.
+ source = tmp_path / "source.mp4"
+ subprocess.run(
+ [binary, "-y", "-loop", "1", "-framerate", "10", "-i", str(image), "-t", "0.4",
+ "-vf", video._BT709_VIDEO_FILTER, "-c:v", "libx264", str(source)],
+ check=True, capture_output=True,
+ )
+ clip_file = tmp_path / "clip.mp4"
+ with video.VideoFileClip(str(source), audio=False) as clip:
+ video._write_videofile_with_codec_fallback(clip, str(clip_file), codec="libx264", logger=None, fps=10)
+ _assert_bt709(binary, clip_file)
+
+ combined = tmp_path / "combined.mp4"
+ video.concat_video_clips_with_ffmpeg([str(clip_file)], str(combined), 1, str(tmp_path))
+ _assert_bt709(binary, combined)
+
+ _assert_bt709(binary, video.render_image_zoom_video(str(image), clip_duration=1))