minhluanquach's picture
download
raw
2.91 kB
"""Merge separate video-only (.f137.mp4) and audio-only (.f140.m4a) files
that yt-dlp created when ffmpeg was unavailable during download."""
import shutil
import subprocess
import sys
from pathlib import Path
RAW_DIR = Path("data/raw")
def find_ffmpeg() -> str:
"""Find ffmpeg executable path."""
# Try common locations on Windows
candidates = [
"ffmpeg",
r"C:\Users\minhl\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-8.0.1-full_build\bin\ffmpeg.exe",
]
for c in candidates:
try:
subprocess.run([c, "-version"], capture_output=True, timeout=5)
return c
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
# As absolute fallback, search PATH env from registry
import os
path = os.environ.get("PATH", "")
for d in path.split(";"):
p = Path(d) / "ffmpeg.exe"
if p.exists():
return str(p)
raise FileNotFoundError("ffmpeg not found. Install with: winget install Gyan.FFmpeg")
def main():
ffmpeg = find_ffmpeg()
print(f"Using ffmpeg: {ffmpeg}")
videos = sorted(RAW_DIR.glob("*.f137.mp4"))
print(f"Found {len(videos)} video-only files to merge")
merged = 0
failed = 0
no_audio = 0
for vf in videos:
base = vf.name.replace(".f137.mp4", "")
af = RAW_DIR / f"{base}.f140.m4a"
out = RAW_DIR / f"{base}.mp4"
if out.exists():
continue
if not af.exists():
# No audio — just rename
vf.rename(out)
no_audio += 1
continue
cmd = [
ffmpeg, "-y",
"-i", str(vf), "-i", str(af),
"-c", "copy", "-shortest",
str(out),
"-loglevel", "error",
]
r = subprocess.run(cmd, capture_output=True, timeout=120)
if r.returncode == 0:
merged += 1
else:
print(f" FAILED {base}: {r.stderr.decode(errors='replace')[:100]}")
failed += 1
print(f"\nDone: merged={merged}, no_audio={no_audio}, failed={failed}")
# Clean up source files if merge was successful
if failed == 0 and merged > 0:
resp = input("Remove original .f137/.f140 files? [y/N] ")
if resp.strip().lower() == "y":
for f in RAW_DIR.glob("*.f137.mp4"):
base = f.name.replace(".f137.mp4", "")
if (RAW_DIR / f"{base}.mp4").exists():
f.unlink()
for f in RAW_DIR.glob("*.f140.m4a"):
base = f.name.replace(".f140.m4a", "")
if (RAW_DIR / f"{base}.mp4").exists():
f.unlink()
print("Cleaned up source files.")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
2.91 kB
·
Xet hash:
cd988be792ba33ab77aabbd2e075c739d45df942938e16439ba8c0106a62d6ab

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.