2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Only include -b:v 0 for VP9

also simplify building arguments
This commit is contained in:
asonix 2023-07-17 18:13:41 -05:00
parent 3aecef3a9e
commit 496bcc5dc1
3 changed files with 65 additions and 105 deletions

View file

@ -235,6 +235,13 @@ impl VideoFormat {
} }
impl OutputVideoFormat { 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( pub(crate) const fn from_parts(
video_codec: VideoCodec, video_codec: VideoCodec,
audio_codec: Option<AudioCodec>, audio_codec: Option<AudioCodec>,
@ -374,6 +381,10 @@ impl Mp4Codec {
} }
impl WebmAlphaCodec { impl WebmAlphaCodec {
const fn is_vp9(&self) -> bool {
matches!(self, Self::Vp9)
}
const fn ffmpeg_codec(self) -> &'static str { const fn ffmpeg_codec(self) -> &'static str {
match self { match self {
Self::Vp8 => "vp8", Self::Vp8 => "vp8",
@ -383,6 +394,13 @@ impl WebmAlphaCodec {
} }
impl WebmCodec { 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 { const fn ffmpeg_codec(self) -> &'static str {
match self { match self {
Self::Av1 => "av1", Self::Av1 => "av1",

View file

@ -60,66 +60,43 @@ async fn transcode_files(
output_format: OutputVideoFormat, output_format: OutputVideoFormat,
crf: u8, crf: u8,
) -> Result<(), FfMpegError> { ) -> 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() { if let Some(audio_codec) = output_format.ffmpeg_audio_codec() {
Process::run( args.extend(["-c:a", audio_codec]);
"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?;
} else { } else {
Process::run( args.push("-an")
"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.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(()) Ok(())
} }

View file

@ -79,56 +79,21 @@ async fn convert(
let input_arg = format!("{input}:{input_file_str}"); let input_arg = format!("{input}:{input_file_str}");
let output_arg = format!("{output}:-"); let output_arg = format!("{output}:-");
let quality = quality.map(|q| q.to_string());
let process = if coalesce { let mut args = vec!["convert", "-strip", "-auto-orient", &input_arg];
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 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); let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, input_file);