video-master-adapt/bulk_add_masters.py
2025-10-15 16:25:04 +02:00

102 lines
3 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Bulk add master videos from a directory.
"""
import sys
import click
from pathlib import Path
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from video_matcher.matcher import VideoMatcher
console = Console()
# Common video file extensions
VIDEO_EXTENSIONS = {'.mp4', '.mov', '.avi', '.mkv', '.webm', '.flv', '.wmv', '.m4v'}
@click.command()
@click.argument('directory', type=click.Path(exists=True))
@click.option('--recursive', '-r', is_flag=True, help='Recursively search subdirectories')
@click.option('--pattern', '-p', default='*.mp4', help='File pattern to match (default: *.mp4)')
def bulk_add(directory, recursive, pattern):
"""Bulk add master videos from a directory."""
dir_path = Path(directory)
if not dir_path.is_dir():
console.print(f"[red]✗[/red] Error: {directory} is not a directory")
sys.exit(1)
# Find all video files
if recursive:
video_files = list(dir_path.rglob(pattern))
else:
video_files = list(dir_path.glob(pattern))
# Filter to only video extensions
video_files = [f for f in video_files if f.suffix.lower() in VIDEO_EXTENSIONS]
if not video_files:
console.print(f"[yellow]No video files found matching pattern '{pattern}'[/yellow]")
sys.exit(0)
console.print(f"\n[bold]Found {len(video_files)} video file(s)[/bold]\n")
# Show files to be added
for i, video_file in enumerate(video_files, 1):
console.print(f" {i}. {video_file.name}")
console.print()
if not click.confirm('Add these videos as masters?'):
console.print("Cancelled.")
sys.exit(0)
# Initialize matcher
matcher = VideoMatcher()
# Process each video
success_count = 0
error_count = 0
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
console=console
) as progress:
task = progress.add_task("[cyan]Adding masters...", total=len(video_files))
for video_file in video_files:
try:
# Use filename (without extension) as master_id
master_id = video_file.stem
progress.update(task, description=f"[cyan]Processing {video_file.name}...")
matcher.add_master(str(video_file), master_id)
success_count += 1
except Exception as e:
error_count += 1
console.print(f"[red]✗[/red] Error processing {video_file.name}: {e}")
progress.advance(task)
# Summary
console.print(f"\n[bold]Summary:[/bold]")
console.print(f" [green]✓[/green] Successfully added: {success_count}")
if error_count > 0:
console.print(f" [red]✗[/red] Errors: {error_count}")
if __name__ == '__main__':
bulk_add()