feat(clips): borrar la metadata heredada del source en los clips finales

Los clips arrastraban el handler de stream "ISO Media file produced by
Google Inc." del vídeo original de YouTube: ffmpeg copia la metadata del
input por defecto, así que sobrevivía a todos los reencodes y acababa
publicado en la red social. Rastro innecesario del origen.

Se añade METADATA_SCRUB en ffmpeg_utils y se aplica en cada productor de
fichero final: el concat de reframe_v2 (el camino real de prod), el merge
de process_video_to_vertical, el passthrough horizontal, el watermark, y
los reencodes de subtítulos/hooks/editor.

Detalle que costó verificar: -map_metadata -1 global NO basta — el
handler_name a nivel de stream sobrevive a un -c copy. Hacen falta los
especificadores por stream (-map_metadata:s:v -1 -map_metadata:s:a -1);
son inofensivos cuando falta un stream. Verificado con ffprobe sobre un
clip real del pipeline completo: sin rastro de Google.

No promete boost de alcance (eso lo decide el fingerprint de contenido,
no los tags del fichero); es higiene y quitar el rastro del origen.
This commit is contained in:
juancarlos.cavero 2026-07-23 01:34:59 +02:00
parent be4510aa43
commit 6ea46ca5d2
7 changed files with 44 additions and 11 deletions

View file

@ -9,7 +9,7 @@ from google.genai import types
from pydantic import BaseModel from pydantic import BaseModel
from edit_builder import build_filter_string 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): class EditDecision(BaseModel):
@ -405,7 +405,8 @@ class VideoEditor:
if not filter_data or not filter_data.get("filter_string"): if not filter_data or not filter_data.get("filter_string"):
print("⚠️ No filter string found. Copying original.") print("⚠️ No filter string found. Copying original.")
try: 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: except subprocess.TimeoutExpired:
raise RuntimeError("FFmpeg copy timed out after 1800s.") raise RuntimeError("FFmpeg copy timed out after 1800s.")
return return
@ -474,6 +475,7 @@ class VideoEditor:
'-vf', filter_string, '-vf', filter_string,
*video_encode_args(QUALITY), *video_encode_args(QUALITY),
'-c:a', 'copy', '-c:a', 'copy',
*METADATA_SCRUB,
'-movflags', '+faststart', '-movflags', '+faststart',
output_path output_path
] ]

View file

@ -42,6 +42,18 @@ _NVENC_ARGS = {
"-pix_fmt", "yuv420p"], "-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() _probe_lock = threading.Lock()
_nvenc_ok = None # None = not probed yet _nvenc_ok = None # None = not probed yet
_announced = False _announced = False

View file

@ -6,7 +6,7 @@ import urllib.request
import uuid import uuid
from PIL import Image, ImageDraw, ImageFont, ImageFilter 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_URL = "https://github.com/googlefonts/noto-fonts/raw/main/hinted/ttf/NotoSerif/NotoSerif-Bold.ttf"
FONT_DIR = "fonts" 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 ""), + (f":enable='between(t,0,{float(duration)})'" if duration else ""),
'-c:a', 'copy', '-c:a', 'copy',
*video_encode_args(QUALITY), *video_encode_args(QUALITY),
*METADATA_SCRUB,
'-movflags', '+faststart', '-movflags', '+faststart',
output_path output_path
] ]

10
main.py
View file

@ -22,7 +22,7 @@ from google.genai import types as genai_types
import gemini_worker import gemini_worker
from clip_selection import build_transcript_windows, snap_clip_to_words 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 from dotenv import load_dotenv
import json import json
@ -696,7 +696,7 @@ def finalize_clip_passthrough(input_video, final_output_video):
print(f"🎬 Passthrough (native framing): {input_video}") print(f"🎬 Passthrough (native framing): {input_video}")
cmd = [ cmd = [
'ffmpeg', '-y', '-i', input_video, 'ffmpeg', '-y', '-i', input_video,
'-c', 'copy', '-movflags', '+faststart', '-c', 'copy', *METADATA_SCRUB, '-movflags', '+faststart',
final_output_video, final_output_video,
] ]
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=1800) 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" tmp_path = video_path + ".wm.mp4"
cmd = ["ffmpeg", "-y", "-i", video_path, "-i", logo_path, cmd = ["ffmpeg", "-y", "-i", video_path, "-i", logo_path,
"-filter_complex", filt, "-filter_complex", filt,
*video_encode_args(QUALITY), "-c:a", "copy", *video_encode_args(QUALITY), "-c:a", "copy", *METADATA_SCRUB,
"-movflags", "+faststart", tmp_path] "-movflags", "+faststart", tmp_path]
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
timeout=1800) 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): if os.path.exists(temp_audio_output):
merge_command = [ merge_command = [
'ffmpeg', '-y', '-i', temp_video_output, '-i', temp_audio_output, '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: else:
merge_command = [ merge_command = [
'ffmpeg', '-y', '-i', temp_video_output, 'ffmpeg', '-y', '-i', temp_video_output,
'-c:v', 'copy', final_output_video '-c:v', 'copy', *METADATA_SCRUB, final_output_video
] ]
try: try:

View file

@ -21,7 +21,7 @@ import os
import subprocess import subprocess
import tempfile 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 ANALYSIS_MAX_WIDTH = 640
@ -238,7 +238,7 @@ def render(input_video, final_output_video, aspect_ratio):
"-f", "concat", "-safe", "0", "-i", list_path, "-f", "concat", "-safe", "0", "-i", list_path,
"-i", input_video, "-i", input_video,
"-map", "0:v:0", "-map", "1:a:0?", "-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, final_output_video,
]) ])
finally: finally:

View file

@ -3,7 +3,7 @@ import re
import subprocess import subprocess
import sys import sys
from ffmpeg_utils import video_encode_args, QUALITY from ffmpeg_utils import video_encode_args, QUALITY, METADATA_SCRUB
_STDIO_CONFIGURED = False _STDIO_CONFIGURED = False
@ -491,6 +491,7 @@ def burn_subtitles(video_path, srt_path, output_path, alignment=2, fontsize=16,
'-vf', vf, '-vf', vf,
'-c:a', 'copy', '-c:a', 'copy',
*video_encode_args(QUALITY), *video_encode_args(QUALITY),
*METADATA_SCRUB,
'-movflags', '+faststart', '-movflags', '+faststart',
output_path output_path
] ]

View file

@ -5,6 +5,7 @@ import pytest
import ffmpeg_utils import ffmpeg_utils
from ffmpeg_utils import ( from ffmpeg_utils import (
DELIVERY, DELIVERY,
METADATA_SCRUB,
QUALITY, QUALITY,
QUALITY_FAST, QUALITY_FAST,
reset_encoder_cache, reset_encoder_cache,
@ -79,3 +80,19 @@ def test_returns_a_fresh_list_each_call():
first = video_encode_args(QUALITY) first = video_encode_args(QUALITY)
first.append("-mutated") first.append("-mutated")
assert "-mutated" not in video_encode_args(QUALITY) 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))