Skip to content

Commit

Permalink
Make parsing loudnorm output more robust, fixes #3
Browse files Browse the repository at this point in the history
At some point the output of the ffmpeg changed and parsing it with a
hardcoded line offset doesn't cut it anymore.

With this commit we now actively scan for lines within lines with
opening and closing braces.
  • Loading branch information
indiscipline committed Apr 26, 2024
1 parent cc707f2 commit ed1e580
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffmpeg-loudnorm-helper"
version = "0.2.0"
version = "0.2.1"
authors = ["Kirill I <[email protected]>"]

[[bin]]
Expand Down
19 changes: 13 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@ Windows CMD:
let output_s = String::from_utf8_lossy(&output.stderr);
if output.status.success() {
let loudness: Loudness = {
let json: String = {
let lines: Vec<&str> = output_s.lines().collect();
let (_, lines) = lines.split_at(lines.len() - 12);
lines.join("\n")
};
serde_json::from_str(&json).unwrap()
// Scanning loudnorm measurement output for the json object
let json_str = output_s.lines().scan(false, |in_json, line| {
match line.trim() {
"{" => { *in_json = true; Some(Some(line)) },
"}" => None, // Skip lines following json
_ if *in_json => Some(Some(line)),
_ => Some(None),
}
}).filter_map(|ln| ln).collect::<Vec<&str>>().join("\n") + "\n}";

serde_json::from_str(&json_str).expect(
&format!("Error parsing the loudnorm measurement output:\n{}", &json_str)
)
};

{
Expand Down

0 comments on commit ed1e580

Please sign in to comment.