Skip to content

Commit

Permalink
Show full ffmpeg command after errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Jun 27, 2024
1 parent d76c46d commit 58384eb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 59 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
* Show full ffmpeg command after errors.

# v0.7.14
* Fix bash completions of some filenames.

Expand Down
76 changes: 38 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub fn encode_sample(

temporary::add(&dest, TempKind::Keepable);

let enc = Command::new("ffmpeg")
.kill_on_drop(true)
let mut cmd = Command::new("ffmpeg");
cmd.kill_on_drop(true)
.arg("-y")
.args(input_args.iter().map(|a| &**a))
.arg2("-i", input)
Expand All @@ -100,11 +100,12 @@ pub fn encode_sample(
.arg(&dest)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.context("ffmpeg encode_sample")?;
.stderr(Stdio::piped());
let cmd_str = cmd.to_cmd_str();

let stream = FfmpegOut::stream(enc, "ffmpeg encode_sample");
let enc = cmd.spawn().context("ffmpeg encode_sample")?;

let stream = FfmpegOut::stream(enc, "ffmpeg encode_sample", cmd_str);
Ok((dest, stream))
}

Expand Down Expand Up @@ -146,8 +147,8 @@ pub fn encode(
false => "0",
};

let enc = Command::new("ffmpeg")
.kill_on_drop(true)
let mut cmd = Command::new("ffmpeg");
cmd.kill_on_drop(true)
.args(input_args.iter().map(|a| &**a))
.arg("-y")
.arg2("-i", input)
Expand All @@ -168,11 +169,12 @@ pub fn encode(
.arg(output)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.context("ffmpeg encode")?;
.stderr(Stdio::piped());
let cmd_str = cmd.to_cmd_str();

let enc = cmd.spawn().context("ffmpeg encode")?;

Ok(FfmpegOut::stream(enc, "ffmpeg encode"))
Ok(FfmpegOut::stream(enc, "ffmpeg encode", cmd_str))
}

pub fn pre_extension_name(vcodec: &str) -> &str {
Expand Down
27 changes: 24 additions & 3 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ pub fn exit_ok(name: &'static str, done: io::Result<ExitStatus>) -> anyhow::Resu
pub fn exit_ok_stderr(
name: &'static str,
done: io::Result<ExitStatus>,
cmd_str: &str,
stderr: &Chunks,
) -> anyhow::Result<()> {
exit_ok(name, done)
.map_err(|e| anyhow!("{e}\n---stderr---\n{}\n------------", stderr.out.trim()))
exit_ok(name, done).map_err(|e| {
anyhow!(
"{e}\n----cmd-----\n{cmd_str}\n---stderr---\n{}\n------------",
stderr.out.trim()
)
})
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -98,6 +103,7 @@ impl FfmpegOut {
pub fn stream(
child: Child,
name: &'static str,
cmd_str: String,
) -> impl Stream<Item = anyhow::Result<FfmpegOut>> {
let mut chunks = Chunks::default();
ProcessChunkStream::from(child).filter_map(move |item| match item {
Expand All @@ -106,7 +112,7 @@ impl FfmpegOut {
FfmpegOut::try_parse(chunks.last_line()).map(Ok)
}
Item::Stdout(_) => None,
Item::Done(code) => match exit_ok_stderr(name, code, &chunks) {
Item::Done(code) => match exit_ok_stderr(name, code, &cmd_str, &chunks) {
Ok(_) => None,
Err(err) => Some(Err(err)),
},
Expand Down Expand Up @@ -232,6 +238,9 @@ pub trait CommandExt {

/// Adds two arguments if `condition` otherwise noop.
fn arg2_if(&mut self, condition: bool, a: impl ArgString, b: impl ArgString) -> &mut Self;

/// Convert to readable shell-like string.
fn to_cmd_str(&self) -> String;
}
impl CommandExt for tokio::process::Command {
fn arg2(&mut self, a: impl ArgString, b: impl ArgString) -> &mut Self {
Expand All @@ -251,6 +260,18 @@ impl CommandExt for tokio::process::Command {
false => self,
}
}

fn to_cmd_str(&self) -> String {
let cmd = self.as_std();
cmd.get_args().map(|a| a.to_string_lossy()).fold(
cmd.get_program().to_string_lossy().to_string(),
|mut all, next| {
all.push(' ');
all += &next;
all
},
)
}
}

pub trait ArgString {
Expand Down
15 changes: 9 additions & 6 deletions src/vmaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ pub fn run(
distorted: &Path,
filter_complex: &str,
) -> anyhow::Result<impl Stream<Item = VmafOut>> {
let vmaf: ProcessChunkStream = Command::new("ffmpeg")
.kill_on_drop(true)
let mut cmd = Command::new("ffmpeg");
cmd.kill_on_drop(true)
.arg2("-r", "24")
.arg2("-i", distorted)
.arg2("-r", "24")
.arg2("-i", reference)
.arg2("-filter_complex", filter_complex)
.arg2("-f", "null")
.arg("-")
.try_into()
.context("ffmpeg vmaf")?;
.arg("-");

let cmd_str = cmd.to_cmd_str();
let vmaf: ProcessChunkStream = cmd.try_into().context("ffmpeg vmaf")?;

let mut chunks = Chunks::default();
let vmaf = vmaf.filter_map(move |item| match item {
Item::Stderr(chunk) => VmafOut::try_from_chunk(&chunk, &mut chunks),
Item::Stdout(_) => None,
Item::Done(code) => VmafOut::ignore_ok(exit_ok_stderr("ffmpeg vmaf", code, &chunks)),
Item::Done(code) => {
VmafOut::ignore_ok(exit_ok_stderr("ffmpeg vmaf", code, &cmd_str, &chunks))
}
});

Ok(vmaf)
Expand Down

0 comments on commit 58384eb

Please sign in to comment.