python.fast-api-converter/app/ffmpeg_class.py

66 lines
No EOL
2.3 KiB
Python

class FFCmd:
def __init__(self, config):
self.config = config
@staticmethod
def video_info(select, source_file):
command_info = [
"ffprobe", "-v",
"error",
"-select_streams",
f"{select}",
"-show_entries",
"stream=index,channels,codec_name,codec_type,pix_fmt,level,"
"film_grain,r_frame_rate,bit_rate,sample_rate,width,height,size,tags:stream_tags=language,duration",
"-show_entries",
"format=size,bit_rate,nb_streams",
"-of", "json",
source_file
]
return command_info
def video_convert(self, obj):
command_convert = [
"ffmpeg", "-y", "-i", obj.source_file,
"-map", "0:0",
"-c:v", "libsvtav1",
"-preset", "5",
"-crf", "30",
"-g", "240",
"-pix_fmt", "yuv420p10le",
"-svtav1-params", "tune=0:film-grain=8:threads=16",
]
if len(obj.streams_audio):
for audio_stream in obj.streams_audio:
if audio_stream.get("tags", {}).get("language", None) in self.config["convert"]["language"]:
command_convert.extend([
"-map", f"0:{audio_stream['index']}",
f"-c:a", "libopus",
f"-b:a", "320k",
f"-ac", str(audio_stream['channels'])
])
# Subtitle-Streams einbinden
if len(obj.streams_subtitle):
for subtitle_stream in obj.streams_subtitle:
if subtitle_stream.get("codec_name") not in self.config["subtitle"]["blacklist"]:
if subtitle_stream.get("tags", {}).get("language", None) in self.config["convert"]["language"]:
command_convert.extend([
"-map", f"0:{subtitle_stream['index']}",
])
command_convert.append(obj.output_file)
return command_convert
@staticmethod
def command_as_string(cmd):
command_as_string: str = ""
for para in cmd:
if "/" not in para:
command_as_string += f"{para} "
else:
command_as_string += f"\"{para}\" "
return command_as_string.strip()