Add MP4 upload support

This commit is contained in:
2026-05-22 20:36:33 +01:00
parent 665ea41c65
commit a25a60f217
6 changed files with 185 additions and 29 deletions

82
main.py
View File

@@ -7,6 +7,7 @@ import argparse
import asyncio
import shutil
import time
from pathlib import Path
from src.audio_separation import DEFAULT_MIX_MODE
from src.core_utils import ConfigurationError
@@ -28,7 +29,11 @@ Examples:
""",
)
parser.add_argument("url", help="YouTube video URL to subtitle")
parser.add_argument("url", nargs="?", help="YouTube video URL to subtitle")
parser.add_argument(
"--input-file",
help="Path to a local MP4 file to dub instead of downloading from YouTube.",
)
parser.add_argument(
"--lang",
"-l",
@@ -148,6 +153,24 @@ def _build_translation_config(args: argparse.Namespace) -> TranslationConfig:
)
def _validate_source_args(args: argparse.Namespace) -> None:
"""Ensure exactly one source input is configured."""
if bool(args.url) == bool(args.input_file):
raise SystemExit("Provide either a YouTube URL or --input-file, but not both.")
def _prepare_local_video(input_file: str, media_module, cache_dir: Path) -> tuple[Path, Path]:
"""Validate a local MP4 and extract its audio for the shared pipeline."""
video_path = Path(input_file).expanduser().resolve()
if not video_path.exists():
raise FileNotFoundError(f"Input file not found: {video_path}")
if video_path.suffix.lower() != ".mp4":
raise ValueError("Only MP4 input files are supported.")
audio_path = cache_dir / f"{video_path.stem}_uploaded.wav"
return video_path, media_module.extract_audio_from_video(video_path, audio_path)
def _get_source_language_hint() -> str:
"""Read an optional source language override from the environment."""
import os
@@ -190,6 +213,7 @@ def main() -> None:
"""Run the full YouTube Auto Dub pipeline."""
parser = build_parser()
args = parser.parse_args()
_validate_source_args(args)
import src.engines
import src.media
@@ -233,32 +257,42 @@ def main() -> None:
)
print(f"\n{'=' * 60}")
print("STEP 1: DOWNLOADING CONTENT")
print("STEP 1: PREPARING CONTENT")
print(f"{'=' * 60}")
print(f"[*] Target URL: {args.url}")
print(f"[*] Target Language: {args.lang.upper()}")
try:
video_path = src.youtube.downloadVideo(
args.url,
browser=args.browser,
cookies_file=args.cookies,
)
audio_path = src.youtube.downloadAudio(
args.url,
browser=args.browser,
cookies_file=args.cookies,
)
print(f"[+] Video downloaded: {video_path}")
print(f"[+] Audio extracted: {audio_path}")
except Exception as exc:
print(f"\n[!] DOWNLOAD FAILED: {exc}")
print("\n[-] TROUBLESHOOTING TIPS:")
print(" 1. Close all browser windows if using --browser")
print(" 2. Export fresh cookies.txt and use --cookies")
print(" 3. Check if video is private/region-restricted")
print(" 4. Verify YouTube URL is correct")
return
if args.input_file:
print(f"[*] Source MP4: {args.input_file}")
try:
video_path, audio_path = _prepare_local_video(args.input_file, src.media, src.engines.CACHE_DIR)
print(f"[+] Local video ready: {video_path}")
print(f"[+] Audio extracted: {audio_path}")
except Exception as exc:
print(f"\n[!] LOCAL INPUT FAILED: {exc}")
return
else:
print(f"[*] Target URL: {args.url}")
try:
video_path = src.youtube.downloadVideo(
args.url,
browser=args.browser,
cookies_file=args.cookies,
)
audio_path = src.youtube.downloadAudio(
args.url,
browser=args.browser,
cookies_file=args.cookies,
)
print(f"[+] Video downloaded: {video_path}")
print(f"[+] Audio extracted: {audio_path}")
except Exception as exc:
print(f"\n[!] DOWNLOAD FAILED: {exc}")
print("\n[-] TROUBLESHOOTING TIPS:")
print(" 1. Close all browser windows if using --browser")
print(" 2. Export fresh cookies.txt and use --cookies")
print(" 3. Check if video is private/region-restricted")
print(" 4. Verify YouTube URL is correct")
return
print(f"\n{'=' * 60}")
print("STEP 2: SPEECH TRANSCRIPTION")