Refactor app.py: extract SOLID helper functions and simplify main command
Moved ffprobe call into run_ffprobe Introduced prepare_video_file, apply_stream_settings, and confirm_and_write_script helpers Streamlined main() to delegate to new helpers
This commit is contained in:
+44
-32
@@ -34,29 +34,13 @@ def validate_imdb(ctx: click.Context, param: click.Parameter, value: str) -> str
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
# Helper functions for SOLID refactoring
|
||||||
def main(
|
def run_ffprobe(filename: Path) -> dict:
|
||||||
filename: Path = typer.Argument(..., exists=True, help="Input video file"),
|
"""Execute ffprobe and return parsed JSON data."""
|
||||||
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)
|
|
||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
"ffprobe",
|
"ffprobe", "-v", "quiet",
|
||||||
"-v",
|
"-print_format", "json",
|
||||||
"quiet",
|
"-show_format", "-show_streams",
|
||||||
"-print_format",
|
|
||||||
"json",
|
|
||||||
"-show_format",
|
|
||||||
"-show_streams",
|
|
||||||
filename,
|
filename,
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
@@ -69,26 +53,54 @@ def main(
|
|||||||
error_output = e.stderr.decode() if e.stderr else str(e)
|
error_output = e.stderr.decode() if e.stderr else str(e)
|
||||||
typer.secho(error_output, fg="red", err=True)
|
typer.secho(error_output, fg="red", err=True)
|
||||||
raise typer.Exit(code=1)
|
raise typer.Exit(code=1)
|
||||||
data = parse_streams(json.loads(result.stdout), video_details)
|
return json.loads(result.stdout)
|
||||||
data.original_media_type = original_media_type
|
|
||||||
for stream in data.video_streams:
|
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.preset = preset
|
||||||
stream.crf = crf
|
stream.crf = crf
|
||||||
stream.original_media_type = original_media_type
|
stream.original_media_type = original_media_type
|
||||||
stream.film_grain = film_grain
|
stream.film_grain = film_grain
|
||||||
export_data = " ".join(data.cmd())
|
return video_details
|
||||||
export_path = Path(filename).parent / "encode.sh"
|
|
||||||
# Confirm overwrite if script exists
|
def confirm_and_write_script(export_data: str, export_path: Path):
|
||||||
|
"""Confirm overwrite and write shell script."""
|
||||||
if export_path.exists():
|
if export_path.exists():
|
||||||
if not typer.confirm(f"'{export_path}' already exists. Overwrite? "):
|
if not typer.confirm(f"'{export_path}' already exists. Overwrite? "):
|
||||||
typer.echo("Aborting.")
|
typer.echo("Aborting.")
|
||||||
raise typer.Exit()
|
raise typer.Exit()
|
||||||
else:
|
|
||||||
export_path.unlink()
|
export_path.unlink()
|
||||||
write_shell_script(export_data, export_path)
|
write_shell_script(export_data, export_path)
|
||||||
print(Path(filename).parent)
|
|
||||||
print(" ".join(data.cmd()))
|
@app.command()
|
||||||
# json.dump(data, sys.stdout, indent=4)
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user