diff --git a/src/app.py b/src/app.py index d5d9e95..03efe9d 100644 --- a/src/app.py +++ b/src/app.py @@ -34,29 +34,13 @@ def validate_imdb(ctx: click.Context, param: click.Parameter, value: str) -> str return value -@app.command() -def main( - filename: Path = typer.Argument(..., exists=True, help="Input video file"), - imdb: str = typer.Option(None, "-i", "--imdb", callback=validate_imdb, help="IMDB ID"), - nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"), - preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"), - crf: int = typer.Option(30, "-c", "--crf", help="CRF value"), - original_media_type: OriginalMediaType = typer.Option(OriginalMediaType.DVD, "-o", "--original-media-type", case_sensitive=False, help="Source of the original media."), - film_grain: int = typer.Option(20, "-fg", "--film-grain", help="Film grain amount (default: 20)") -): - video_details = VideoFile( - imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles - ) - video_details = get_movie_details(video_details) - +# Helper functions for SOLID refactoring +def run_ffprobe(filename: Path) -> dict: + """Execute ffprobe and return parsed JSON data.""" cmd = [ - "ffprobe", - "-v", - "quiet", - "-print_format", - "json", - "-show_format", - "-show_streams", + "ffprobe", "-v", "quiet", + "-print_format", "json", + "-show_format", "-show_streams", filename, ] try: @@ -69,26 +53,54 @@ def main( error_output = e.stderr.decode() if e.stderr else str(e) typer.secho(error_output, fg="red", err=True) raise typer.Exit(code=1) - data = parse_streams(json.loads(result.stdout), video_details) - data.original_media_type = original_media_type - for stream in data.video_streams: + return json.loads(result.stdout) + +def prepare_video_file(filename: Path, imdb: str, nosubtitles: bool) -> VideoFile: + """Initialize VideoFile with metadata.""" + return VideoFile(imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles) + +def apply_stream_settings(video_details: VideoFile, preset: int, crf: int, original_media_type: OriginalMediaType, film_grain: int) -> VideoFile: + """Apply encoding settings to video streams.""" + video_details.original_media_type = original_media_type + for stream in video_details.video_streams: stream.preset = preset stream.crf = crf stream.original_media_type = original_media_type stream.film_grain = film_grain - export_data = " ".join(data.cmd()) - export_path = Path(filename).parent / "encode.sh" - # Confirm overwrite if script exists + return video_details + +def confirm_and_write_script(export_data: str, export_path: Path): + """Confirm overwrite and write shell script.""" if export_path.exists(): if not typer.confirm(f"'{export_path}' already exists. Overwrite? "): typer.echo("Aborting.") raise typer.Exit() - else: - export_path.unlink() + export_path.unlink() write_shell_script(export_data, export_path) - print(Path(filename).parent) - print(" ".join(data.cmd())) - # json.dump(data, sys.stdout, indent=4) + +@app.command() +def main( + filename: Path = typer.Argument(..., exists=True, help="Input video file"), + imdb: str = typer.Option(None, "-i", "--imdb", callback=validate_imdb, help="IMDB ID"), + nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"), + preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"), + crf: int = typer.Option(30, "-c", "--crf", help="CRF value"), + original_media_type: OriginalMediaType = typer.Option(OriginalMediaType.DVD, "-o", "--original-media-type", case_sensitive=False, help="Source of the original media."), + film_grain: int = typer.Option(20, "-fg", "--film-grain", help="Film grain amount (default: 20)") +): + video_details = prepare_video_file(filename, imdb, nosubtitles) + video_details = get_movie_details(video_details) + + stream_data = run_ffprobe(filename) + video_details = parse_streams(stream_data, video_details) + video_details = apply_stream_settings(video_details, preset, crf, original_media_type, film_grain) + + export_data = " ".join(video_details.cmd()) + export_path = filename.parent / "encode.sh" + confirm_and_write_script(export_data, export_path) + + typer.echo(str(filename.parent)) + typer.echo(export_data) if __name__ == "__main__":