Skip to content

Commit

Permalink
Map vp9 --preset to -cpu-used
Browse files Browse the repository at this point in the history
Error when setting reserved --enc,enc-input args
Avoid overriding --enc b:a, movflags, ac
Resolves #45
Resolves #46
  • Loading branch information
alexheretic committed Jul 26, 2022
1 parent 861c781 commit d94efd6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased (v0.4.1)
* For `-e libvpx-vp9` map `--preset` number to ffmpeg `-cpu-used` (0-5).
* When overriding with a ffmpeg encoder avoid setting `b:a`, `movflags` or `ac` if explicitly set via `--enc`.
* Add error output when using `--enc-input` with the default svt-av1 encoder.
* Add errors for `--enc`/`--enc-input` args that are already provided by existing args or inferred.

# v0.4.0
* Add `--encoder`/`-e` encoder override.
Any [encoder ffmpeg supports](https://ffmpeg.org/ffmpeg-all.html#toc-Video-Encoders)
Expand Down
24 changes: 24 additions & 0 deletions src/command/args/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use anyhow::ensure;
use clap::Parser;
use std::{
collections::HashMap,
fmt::{self, Write},
path::PathBuf,
sync::Arc,
Expand Down Expand Up @@ -289,6 +290,29 @@ impl Encode {
})
.collect();

// ban usage of the bits we already set via other args & logic
let reserved = HashMap::from([
("-c:a", " use --acodec"),
("-codec:a", " use --acodec"),
("-acodec", " use --acodec"),
("-i", ""),
("-y", ""),
("-n", ""),
("-c:v", ""),
("-codec:v", ""),
("-vcodec", ""),
("-pix_fmt", " use --pix-format"),
("-crf", ""),
("-preset", " use --preset"),
("-vf", " use --vfilter"),
("-filter:v", " use --vfilter"),
]);
for arg in args.iter().chain(input_args.iter()) {
if let Some(hint) = reserved.get(arg.as_str()) {
anyhow::bail!("Encoder argument `{arg}` not allowed{hint}");
}
}

Ok(FfmpegEncodeArgs {
input: &self.input,
vcodec,
Expand Down
15 changes: 11 additions & 4 deletions src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use anyhow::Context;
use std::{
collections::HashSet,
path::{Path, PathBuf},
process::Stdio,
sync::Arc,
Expand Down Expand Up @@ -95,12 +96,18 @@ pub fn encode(
audio_codec: Option<&str>,
downmix_to_stereo: bool,
) -> anyhow::Result<impl Stream<Item = anyhow::Result<FfmpegOut>>> {
let output_is_mp4 = output.extension().and_then(|e| e.to_str()) == Some("mp4");
let oargs: HashSet<_> = output_args.iter().map(|a| a.as_str()).collect();

let add_faststart =
output.extension().and_then(|e| e.to_str()) == Some("mp4") && !oargs.contains("-movflags");

let audio_codec = audio_codec.unwrap_or_else(|| {
svtav1::default_audio_codec(input, output, downmix_to_stereo, has_audio)
});

let set_ba_128k = audio_codec == "libopus" && !oargs.contains("-b:a");
let downmix_to_stereo = downmix_to_stereo && !oargs.contains("-ac");

let enc = Command::new("ffmpeg")
.kill_on_drop(true)
.args(input_args.iter().map(|a| &**a))
Expand All @@ -115,8 +122,8 @@ pub fn encode(
.arg2("-c:s", "copy")
.arg2("-c:a", audio_codec)
.arg2_if(downmix_to_stereo, "-ac", 2)
.arg2_if(audio_codec == "libopus", "-b:a", "128k")
.arg2_if(output_is_mp4, "-movflags", "+faststart")
.arg2_if(set_ba_128k, "-b:a", "128k")
.arg2_if(add_faststart, "-movflags", "+faststart")
.arg(output)
.stdout(Stdio::null())
.stderr(Stdio::piped())
Expand Down Expand Up @@ -144,7 +151,7 @@ trait VCodecSpecific {
impl VCodecSpecific for Arc<str> {
fn preset_arg(&self) -> &str {
match &**self {
"libaom-av1" => "-cpu-used",
"libaom-av1" | "libvpx-vp9" => "-cpu-used",
_ => "-preset",
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/svtav1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn encode(
audio_codec: Option<&str>,
downmix_to_stereo: bool,
) -> anyhow::Result<impl Stream<Item = anyhow::Result<FfmpegOut>>> {
let output_is_mp4 = output.extension().and_then(|e| e.to_str()) == Some("mp4");
let add_faststart = output.extension().and_then(|e| e.to_str()) == Some("mp4");

let audio_codec = audio_codec
.unwrap_or_else(|| default_audio_codec(input, output, downmix_to_stereo, has_audio));
Expand Down Expand Up @@ -139,7 +139,7 @@ pub fn encode(
.arg2_if(downmix_to_stereo, "-ac", 2)
.arg2("-c:v", "copy")
.arg2_if(audio_codec == "libopus", "-b:a", "128k")
.arg2_if(output_is_mp4, "-movflags", "+faststart")
.arg2_if(add_faststart, "-movflags", "+faststart")
.arg(output)
.spawn()
.context("ffmpeg to-output")?;
Expand Down

0 comments on commit d94efd6

Please sign in to comment.