import json import subprocess import click from pathlib import Path # from rich import print from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream from movie_details import get_movie_details from lib.fs import write_shell_script def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile: for stream in stream_data["streams"]: if ( stream["codec_type"] == "video" and stream["disposition"]["attached_pic"] == 0 ): VideoFile.video_streams.append(VideoStream(**stream)) elif stream["codec_type"] == "audio": VideoFile.audio_streams.append(AudioStream(**stream)) elif stream["codec_type"] == "subtitle": VideoFile.subtitle_streams.append(SubtitleStream(**stream)) return VideoFile @click.command() @click.argument("filename", type=click.Path(exists=True)) @click.option("-i", "--imdb", type=str, help="IMDB ID") @click.option("-ns", "--nosubtitles", is_flag=True, help="No subtitles") @click.option("-p", "--preset", type=int, default=2, help="Encoding preset (default: 2)") @click.option("-c", "--crf", type=int, default=30, help="CRF value (default: 30)") @click.option('-o', '--original-media-type', default='DVD', help='Set the ORIGINAL_MEDIA_TYPE metadata') def main(filename: Path, imdb: str, nosubtitles: bool, preset: int, crf: int, original_media_type: str): """Run ffprobe on a file and print the output in JSON format.""" video_details = VideoFile( imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles ) video_details = get_movie_details(video_details) cmd = [ "ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", filename, ] result = subprocess.run(cmd, capture_output=True, check=True) data = parse_streams(json.loads(result.stdout), video_details) for stream in data.video_streams: stream.preset = preset stream.crf = crf stream.original_media_type = original_media_type export_data = " ".join(data.cmd()) export_path = Path(filename).parent / "encode.sh" write_shell_script(export_data, export_path) print(Path(filename).parent) print(" ".join(data.cmd())) # json.dump(data, sys.stdout, indent=4) if __name__ == "__main__": main()