I want to change the current code so that it works even when the video already is hevc but it's above the MAX_HEIGHT
or above the MAX_BITRATE
.
I've placed some instructions, like the one to change frame rate, inside the conditionals but others outside because I think they are needed always. Like defining the audio codec, because if I don't it doesn't choose the one it already has but a default vorbis
audio codec. Is this correct or have I placed one of this instructions in the wrong place?
def prepare_ffmpeg_command(self) -> List[str]:
cmd = ["ffmpeg", "-progress", "pipe:1", "-i", str(self.path),
"-map", "0", "-map", "-v", "-map", "V"]
self.push_encode_video_args_to_command(cmd)
self.push_change_frame_rate_args_to_command(cmd)
try:
self.push_encode_audio_args_to_command(cmd)
except Exception:
logger.warning("No audio stream found")
cmd.extend(["-y", str(self.dest)]) # 'y' arg overwrites output files without asking.
logger.info(f"Video.prepare_ffmpeg_command() -> {cmd}")
return cmd
def push_encode_video_args_to_command(self, cmd: List[str]) -> None:
self.codec = get_codec(self.video_metadata)
if self.codec != VIDEO_CODEC:
self.skip = False
cmd.append("-c:v")
encoder = choose_encoder(self.codec)
crf = str(CRF)
if self.get_bitdepth().is_10bit:
if encoder == 'libx265':
cmd.extend(["libx265", "-x265-params", f"crf={crf}:profile=main10"])
else:
cmd.extend(["libaom-av1", "-cpu-used", "8", "-threads", "0", "-x265-params", f"crf={crf}:profile=main10"])
else:
if encoder == 'libx265':
cmd.extend(["libx265", "-crf", crf])
else:
cmd.extend(["libaom-av1", "-cpu-used", "8", "-threads", "0", "-crf", crf])
cmd.extend(["-maxrate", bitrate_to_string(MAX_VIDEO_BITRATE), "-preset", PRESET])
if ('height' in self.video_metadata
and int(self.video_metadata['height']) > MAX_HEIGHT):
cmd.extend(["-vf", f"scale=-2:{MAX_HEIGHT}"])
def push_change_frame_rate_args_to_command(self, cmd: List[str]) -> None:
if ('r_frame_rate' in self.video_metadata
and convert_str_to_float(self.video_metadata['r_frame_rate']) > MAX_FRAME_RATE):
cmd.extend(["-r", str(MAX_FRAME_RATE)])
self.skip = False
def push_encode_audio_args_to_command(self, cmd: List[str]) -> None:
if get_codec(self.audio_metadata) != AUDIO_CODEC:
self.skip = False
cmd.extend(["-c:a", AUDIO_CODEC])
if int(self.audio_metadata["sample_rate"]) > MAX_AUDIO_BITRATE:
self.skip = False
cmd.extend(["-b:a", bitrate_to_string(MAX_AUDIO_BITRATE)])