Add Base functionality

This commit is contained in:
Esa Kataja
2024-11-14 23:46:13 +02:00
parent c298763049
commit cf3b7bae5d
8 changed files with 383 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
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
def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile:
for stream in stream_data["streams"]:
if stream["codec_type"] == "video":
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")
def main(filename: Path, imdb: str, nosubtitles: bool):
"""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)
print(" ".join(data.cmd()))
# json.dump(data, sys.stdout, indent=4)
if __name__ == "__main__":
main()