| |
| import argparse |
| import os |
| import subprocess |
| from pathlib import Path |
|
|
|
|
| VIDEO_EXTS = {".mp4", ".mov", ".mkv", ".avi", ".webm", ".m4v"} |
|
|
|
|
| def run_cmd(cmd): |
| return subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
|
|
|
|
| def get_duration_seconds(video_path: Path) -> float: |
| cmd = [ |
| "ffprobe", |
| "-v", |
| "error", |
| "-show_entries", |
| "format=duration", |
| "-of", |
| "default=noprint_wrappers=1:nokey=1", |
| str(video_path), |
| ] |
| out = run_cmd(cmd).stdout.strip() |
| return float(out) |
|
|
|
|
| def has_audio_stream(video_path: Path) -> bool: |
| cmd = [ |
| "ffprobe", |
| "-v", |
| "error", |
| "-select_streams", |
| "a:0", |
| "-show_entries", |
| "stream=index", |
| "-of", |
| "csv=p=0", |
| str(video_path), |
| ] |
| out = run_cmd(cmd).stdout.strip() |
| return bool(out) |
|
|
|
|
| def make_delay_plus_2s(src: Path, dst: Path, duration: float): |
| |
| cmd = [ |
| "ffmpeg", |
| "-y", |
| "-i", |
| str(src), |
| "-map", |
| "0:v:0", |
| "-map", |
| "0:a:0", |
| "-c:v", |
| "copy", |
| "-af", |
| f"adelay=2000:all=1,atrim=0:{duration}", |
| "-c:a", |
| "aac", |
| str(dst), |
| ] |
| run_cmd(cmd) |
|
|
|
|
| def make_advance_minus_2s(src: Path, dst: Path, duration: float): |
| |
| cmd = [ |
| "ffmpeg", |
| "-y", |
| "-i", |
| str(src), |
| "-map", |
| "0:v:0", |
| "-map", |
| "0:a:0", |
| "-c:v", |
| "copy", |
| "-af", |
| f"atrim=start=2,asetpts=PTS-STARTPTS,apad=pad_dur=2,atrim=0:{duration}", |
| "-c:a", |
| "aac", |
| str(dst), |
| ] |
| run_cmd(cmd) |
|
|
|
|
| def iter_videos(root: Path): |
| for dirpath, _, filenames in os.walk(root): |
| for name in filenames: |
| p = Path(dirpath) / name |
| if p.suffix.lower() in VIDEO_EXTS: |
| yield p |
|
|
|
|
| def purge_directory(root: Path): |
| if not root.exists(): |
| return |
| for dirpath, _, filenames in os.walk(root): |
| for name in filenames: |
| (Path(dirpath) / name).unlink() |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Generate +2s delay and -2s advance audio versions for all videos." |
| ) |
| parser.add_argument( |
| "--input-dir", |
| default="/home/ubuntu/uag_oops_data/uag_oops", |
| help="Source video directory.", |
| ) |
| parser.add_argument( |
| "--out-delay-dir", |
| default="/home/ubuntu/uag_oops_data/delay", |
| help="Output directory for +2s delay versions.", |
| ) |
| parser.add_argument( |
| "--out-advance-dir", |
| default="/home/ubuntu/uag_oops_data/early", |
| help="Output directory for -2s advance versions.", |
| ) |
| parser.add_argument( |
| "--overwrite", |
| action="store_true", |
| help="Overwrite output if file exists.", |
| ) |
| parser.add_argument( |
| "--no-purge-output", |
| action="store_true", |
| help="Do not purge output directories before generation.", |
| ) |
| args = parser.parse_args() |
|
|
| input_dir = Path(args.input_dir) |
| out_delay_dir = Path(args.out_delay_dir) |
| out_advance_dir = Path(args.out_advance_dir) |
|
|
| if not input_dir.exists(): |
| raise FileNotFoundError(f"Input directory not found: {input_dir}") |
|
|
| videos = sorted(iter_videos(input_dir)) |
| if not videos: |
| print(f"No videos found in: {input_dir}") |
| return |
|
|
| out_delay_dir.mkdir(parents=True, exist_ok=True) |
| out_advance_dir.mkdir(parents=True, exist_ok=True) |
| purge_output = not args.no_purge_output |
| if purge_output: |
| print("Purging output directories first...") |
| purge_directory(out_delay_dir) |
| purge_directory(out_advance_dir) |
|
|
| print(f"Found {len(videos)} videos.") |
| skipped_no_audio = 0 |
| processed = 0 |
|
|
| for idx, src in enumerate(videos, start=1): |
| rel = src.relative_to(input_dir) |
| delay_name = f"{rel.stem}_delay2s{rel.suffix}" |
| advance_name = f"{rel.stem}_early2s{rel.suffix}" |
| dst_delay = out_delay_dir / rel.parent / delay_name |
| dst_advance = out_advance_dir / rel.parent / advance_name |
| dst_delay.parent.mkdir(parents=True, exist_ok=True) |
| dst_advance.parent.mkdir(parents=True, exist_ok=True) |
|
|
| if (not args.overwrite) and dst_delay.exists() and dst_advance.exists(): |
| print(f"[{idx}/{len(videos)}] Skip existing: {rel}") |
| continue |
|
|
| if not has_audio_stream(src): |
| skipped_no_audio += 1 |
| print(f"[{idx}/{len(videos)}] Skip no-audio: {rel}") |
| continue |
|
|
| duration = get_duration_seconds(src) |
| print(f"[{idx}/{len(videos)}] Processing: {rel}") |
| try: |
| if args.overwrite or not dst_delay.exists(): |
| make_delay_plus_2s(src, dst_delay, duration) |
| if args.overwrite or not dst_advance.exists(): |
| make_advance_minus_2s(src, dst_advance, duration) |
| processed += 1 |
| except subprocess.CalledProcessError as exc: |
| print(f" Failed: {rel}") |
| print(exc.stderr[-500:]) |
|
|
| print("\nDone.") |
| print(f"Processed videos: {processed}") |
| print(f"Skipped no-audio: {skipped_no_audio}") |
| print(f"Delay output dir: {out_delay_dir}") |
| print(f"Advance output dir: {out_advance_dir}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|