diff --git a/src/formats/video.rs b/src/formats/video.rs index 82c00ad..4fe0731 100644 --- a/src/formats/video.rs +++ b/src/formats/video.rs @@ -235,6 +235,13 @@ impl VideoFormat { } impl OutputVideoFormat { + pub(crate) const fn is_vp9(&self) -> bool { + match self { + Self::Webm { video_codec, .. } => video_codec.is_vp9(), + Self::Mp4 { .. } => false, + } + } + pub(crate) const fn from_parts( video_codec: VideoCodec, audio_codec: Option, @@ -374,6 +381,10 @@ impl Mp4Codec { } impl WebmAlphaCodec { + const fn is_vp9(&self) -> bool { + matches!(self, Self::Vp9) + } + const fn ffmpeg_codec(self) -> &'static str { match self { Self::Vp8 => "vp8", @@ -383,6 +394,13 @@ impl WebmAlphaCodec { } impl WebmCodec { + const fn is_vp9(self) -> bool { + match self { + Self::Av1 => false, + Self::Alpha(AlphaCodec { codec, .. }) => codec.is_vp9(), + } + } + const fn ffmpeg_codec(self) -> &'static str { match self { Self::Av1 => "av1", diff --git a/src/validate/ffmpeg.rs b/src/validate/ffmpeg.rs index 8ec390f..41cbc8d 100644 --- a/src/validate/ffmpeg.rs +++ b/src/validate/ffmpeg.rs @@ -60,66 +60,43 @@ async fn transcode_files( output_format: OutputVideoFormat, crf: u8, ) -> Result<(), FfMpegError> { + let mut args = vec![ + "-hide_banner", + "-v", + "warning", + "-f", + input_format.ffmpeg_format(), + "-i", + input_path, + "-pix_fmt", + output_format.pix_fmt(), + "-vf", + "scale=trunc(iw/2)*2:trunc(ih/2)*2", + ]; + if let Some(audio_codec) = output_format.ffmpeg_audio_codec() { - Process::run( - "ffmpeg", - &[ - "-hide_banner", - "-v", - "warning", - "-f", - input_format.ffmpeg_format(), - "-i", - input_path, - "-pix_fmt", - output_format.pix_fmt(), - "-vf", - "scale=trunc(iw/2)*2:trunc(ih/2)*2", - "-c:a", - audio_codec, - "-c:v", - output_format.ffmpeg_video_codec(), - "-b:v", - "0", - "-crf", - &crf.to_string(), - "-f", - output_format.ffmpeg_format(), - output_path, - ], - )? - .wait() - .await?; + args.extend(["-c:a", audio_codec]); } else { - Process::run( - "ffmpeg", - &[ - "-hide_banner", - "-v", - "warning", - "-f", - input_format.ffmpeg_format(), - "-i", - input_path, - "-pix_fmt", - output_format.pix_fmt(), - "-vf", - "scale=trunc(iw/2)*2:trunc(ih/2)*2", - "-an", - "-c:v", - output_format.ffmpeg_video_codec(), - "-b:v", - "0", - "-crf", - &crf.to_string(), - "-f", - output_format.ffmpeg_format(), - output_path, - ], - )? - .wait() - .await?; + args.push("-an") } + args.extend(["-c:v", output_format.ffmpeg_video_codec()]); + + if output_format.is_vp9() { + args.extend(["-b:v", "0"]); + } + + let crf = crf.to_string(); + + args.extend([ + "-crf", + &crf, + "-f", + output_format.ffmpeg_format(), + output_path, + ]); + + Process::run("ffmpeg", &args)?.wait().await?; + Ok(()) } diff --git a/src/validate/magick.rs b/src/validate/magick.rs index 5b36bbc..83e4015 100644 --- a/src/validate/magick.rs +++ b/src/validate/magick.rs @@ -79,56 +79,21 @@ async fn convert( let input_arg = format!("{input}:{input_file_str}"); let output_arg = format!("{output}:-"); + let quality = quality.map(|q| q.to_string()); - let process = if coalesce { - if let Some(quality) = quality { - Process::run( - "magick", - &[ - "convert", - "-strip", - "-auto-orient", - &input_arg, - "-quality", - &quality.to_string(), - "-coalesce", - &output_arg, - ], - )? - } else { - Process::run( - "magick", - &[ - "convert", - "-strip", - "-auto-orient", - &input_arg, - "-coalesce", - &output_arg, - ], - )? - } - } else if let Some(quality) = quality { - Process::run( - "magick", - &[ - "convert", - "-strip", - "-auto-orient", - &input_arg, - "-quality", - &quality.to_string(), - &output_arg, - ], - )? - } else { - Process::run( - "magick", - &["convert", "-strip", "-auto-orient", &input_arg, &output_arg], - )? - }; + let mut args = vec!["convert", "-strip", "-auto-orient", &input_arg]; - let reader = process.read(); + if let Some(quality) = &quality { + args.extend(["-quality", quality]); + } + + if coalesce { + args.push("-coalesce"); + } + + args.push(&output_arg); + + let reader = Process::run("magick", &args)?.read(); let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, input_file);