diff --git a/editor.py b/editor.py index 2a743d1..5575042 100644 --- a/editor.py +++ b/editor.py @@ -9,7 +9,7 @@ from google.genai import types from pydantic import BaseModel from edit_builder import build_filter_string -from ffmpeg_utils import video_encode_args, QUALITY +from ffmpeg_utils import video_encode_args, QUALITY, METADATA_SCRUB class EditDecision(BaseModel): @@ -405,7 +405,8 @@ class VideoEditor: if not filter_data or not filter_data.get("filter_string"): print("⚠️ No filter string found. Copying original.") try: - subprocess.run(['ffmpeg', '-y', '-i', input_path, '-c', 'copy', output_path], timeout=1800) + subprocess.run(['ffmpeg', '-y', '-i', input_path, '-c', 'copy', + *METADATA_SCRUB, output_path], timeout=1800) except subprocess.TimeoutExpired: raise RuntimeError("FFmpeg copy timed out after 1800s.") return @@ -474,6 +475,7 @@ class VideoEditor: '-vf', filter_string, *video_encode_args(QUALITY), '-c:a', 'copy', + *METADATA_SCRUB, '-movflags', '+faststart', output_path ] diff --git a/ffmpeg_utils.py b/ffmpeg_utils.py index f16d2ee..4fb4ad0 100644 --- a/ffmpeg_utils.py +++ b/ffmpeg_utils.py @@ -42,6 +42,18 @@ _NVENC_ARGS = { "-pix_fmt", "yuv420p"], } +# Output args that drop container/stream metadata carried over from the source +# — most notably YouTube's "produced by Google Inc." stream handler, which +# otherwise survives every re-encode (ffmpeg copies input metadata by default) +# and rides into the published clip. The per-stream specifiers are required: +# global -map_metadata -1 alone leaves the audio handler_name intact on a +# stream copy. Empty audio/video specifiers are harmless when a clip lacks that +# stream (ffmpeg ignores them, verified). Spliced in before the output filename +# at each final-artifact producer; kept out of video_encode_args() so that +# stays purely codec/quality args. +METADATA_SCRUB = ["-map_metadata", "-1", "-map_chapters", "-1", + "-map_metadata:s:v", "-1", "-map_metadata:s:a", "-1"] + _probe_lock = threading.Lock() _nvenc_ok = None # None = not probed yet _announced = False diff --git a/hooks.py b/hooks.py index bb58fd7..26ae652 100644 --- a/hooks.py +++ b/hooks.py @@ -6,7 +6,7 @@ import urllib.request import uuid from PIL import Image, ImageDraw, ImageFont, ImageFilter -from ffmpeg_utils import video_encode_args, QUALITY +from ffmpeg_utils import video_encode_args, QUALITY, METADATA_SCRUB FONT_URL = "https://github.com/googlefonts/noto-fonts/raw/main/hinted/ttf/NotoSerif/NotoSerif-Bold.ttf" FONT_DIR = "fonts" @@ -366,6 +366,7 @@ def add_hook_to_video(video_path, text, output_path, position="top", font_scale= + (f":enable='between(t,0,{float(duration)})'" if duration else ""), '-c:a', 'copy', *video_encode_args(QUALITY), + *METADATA_SCRUB, '-movflags', '+faststart', output_path ] diff --git a/main.py b/main.py index 69bc009..fb2901a 100644 --- a/main.py +++ b/main.py @@ -22,7 +22,7 @@ from google.genai import types as genai_types import gemini_worker from clip_selection import build_transcript_windows, snap_clip_to_words -from ffmpeg_utils import video_encode_args, QUALITY, QUALITY_FAST +from ffmpeg_utils import video_encode_args, QUALITY, QUALITY_FAST, METADATA_SCRUB from dotenv import load_dotenv import json @@ -696,7 +696,7 @@ def finalize_clip_passthrough(input_video, final_output_video): print(f"🎬 Passthrough (native framing): {input_video}") cmd = [ 'ffmpeg', '-y', '-i', input_video, - '-c', 'copy', '-movflags', '+faststart', + '-c', 'copy', *METADATA_SCRUB, '-movflags', '+faststart', final_output_video, ] subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=1800) @@ -764,7 +764,7 @@ def apply_watermark(video_path): tmp_path = video_path + ".wm.mp4" cmd = ["ffmpeg", "-y", "-i", video_path, "-i", logo_path, "-filter_complex", filt, - *video_encode_args(QUALITY), "-c:a", "copy", + *video_encode_args(QUALITY), "-c:a", "copy", *METADATA_SCRUB, "-movflags", "+faststart", tmp_path] result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=1800) @@ -965,12 +965,12 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE if os.path.exists(temp_audio_output): merge_command = [ 'ffmpeg', '-y', '-i', temp_video_output, '-i', temp_audio_output, - '-c:v', 'copy', '-c:a', 'copy', final_output_video + '-c:v', 'copy', '-c:a', 'copy', *METADATA_SCRUB, final_output_video ] else: merge_command = [ 'ffmpeg', '-y', '-i', temp_video_output, - '-c:v', 'copy', final_output_video + '-c:v', 'copy', *METADATA_SCRUB, final_output_video ] try: diff --git a/reframe_v2.py b/reframe_v2.py index ec1e61b..cc6348c 100644 --- a/reframe_v2.py +++ b/reframe_v2.py @@ -21,7 +21,7 @@ import os import subprocess import tempfile -from ffmpeg_utils import video_encode_args, QUALITY_FAST +from ffmpeg_utils import video_encode_args, QUALITY_FAST, METADATA_SCRUB ANALYSIS_MAX_WIDTH = 640 @@ -238,7 +238,7 @@ def render(input_video, final_output_video, aspect_ratio): "-f", "concat", "-safe", "0", "-i", list_path, "-i", input_video, "-map", "0:v:0", "-map", "1:a:0?", - "-c:v", "copy", "-c:a", "copy", + "-c:v", "copy", "-c:a", "copy", *METADATA_SCRUB, final_output_video, ]) finally: diff --git a/subtitles.py b/subtitles.py index a011e91..6447e0d 100644 --- a/subtitles.py +++ b/subtitles.py @@ -3,7 +3,7 @@ import re import subprocess import sys -from ffmpeg_utils import video_encode_args, QUALITY +from ffmpeg_utils import video_encode_args, QUALITY, METADATA_SCRUB _STDIO_CONFIGURED = False @@ -491,6 +491,7 @@ def burn_subtitles(video_path, srt_path, output_path, alignment=2, fontsize=16, '-vf', vf, '-c:a', 'copy', *video_encode_args(QUALITY), + *METADATA_SCRUB, '-movflags', '+faststart', output_path ] diff --git a/tests/test_ffmpeg_utils.py b/tests/test_ffmpeg_utils.py index 056a635..4d39723 100644 --- a/tests/test_ffmpeg_utils.py +++ b/tests/test_ffmpeg_utils.py @@ -5,6 +5,7 @@ import pytest import ffmpeg_utils from ffmpeg_utils import ( DELIVERY, + METADATA_SCRUB, QUALITY, QUALITY_FAST, reset_encoder_cache, @@ -79,3 +80,19 @@ def test_returns_a_fresh_list_each_call(): first = video_encode_args(QUALITY) first.append("-mutated") assert "-mutated" not in video_encode_args(QUALITY) + + +def test_metadata_scrub_covers_global_and_per_stream(): + # Global -map_metadata -1 alone leaves the audio handler_name intact on a + # stream copy — the per-stream specifiers are what strip YouTube's + # "produced by Google Inc." handler out of the published clip. + assert METADATA_SCRUB[:2] == ["-map_metadata", "-1"] + assert "-map_metadata:s:v" in METADATA_SCRUB + assert "-map_metadata:s:a" in METADATA_SCRUB + + +def test_encode_args_stay_free_of_metadata_flags(): + # The scrub is spliced at call sites, not baked into the codec args — keep + # video_encode_args a pure codec/quality list. + for tier in (QUALITY, QUALITY_FAST, DELIVERY): + assert not any(a.startswith("-map_metadata") for a in video_encode_args(tier))