supplement video data from video stream metadata

This commit is contained in:
Djkáťo 2024-06-26 00:36:55 +02:00
parent 39ec9a5372
commit a05dba3518
5 changed files with 90 additions and 92 deletions

2
Cargo.lock generated
View file

@ -252,7 +252,7 @@ dependencies = [
[[package]] [[package]]
name = "n-mb" name = "n-mb"
version = "1.1.0" version = "1.1.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

2
rust-toolchain.toml Normal file
View file

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

View file

@ -267,8 +267,8 @@ impl FFMPEGCommand {
.to_str() .to_str()
.context("missing or bad path")?, .context("missing or bad path")?,
]); ]);
// dbg!(&command); dbg!(&command);
// dbg!(&command2); dbg!(&command2);
Ok(FFMPEGCommand { Ok(FFMPEGCommand {
file_name: path.file_name().unwrap().to_str().unwrap().to_owned(), file_name: path.file_name().unwrap().to_str().unwrap().to_owned(),
resolution: None, resolution: None,
@ -341,17 +341,19 @@ pub enum EncodingStatus {
} }
async fn parse_ffprobe(path: &PathBuf) -> anyhow::Result<MediaData> { async fn parse_ffprobe(path: &PathBuf) -> anyhow::Result<MediaData> {
let args = [
"-v",
"error",
"-select_streams",
"v:0",
"-show_entries",
"stream=width,height,duration,bit_rate",
"-of",
"csv=s=,:p=0",
];
let ffprobe = Command::new("ffprobe") let ffprobe = Command::new("ffprobe")
.args([ .args(args)
"-v",
"error",
"-select_streams",
"v:0",
"-show_entries",
"stream=width,height,duration,bit_rate",
"-of",
"csv=s=,:p=0",
])
.arg(path) .arg(path)
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.output() .output()
@ -367,27 +369,62 @@ async fn parse_ffprobe(path: &PathBuf) -> anyhow::Result<MediaData> {
let width = mem.first().and_then(|v| v.parse::<u16>().ok()); let width = mem.first().and_then(|v| v.parse::<u16>().ok());
let height = mem.get(1).and_then(|v| v.parse::<u16>().ok()); let height = mem.get(1).and_then(|v| v.parse::<u16>().ok());
let duration = mem
.get(2) let duration = match mem.get(2).and_then(|v| v.parse::<f32>().ok()) {
.and_then(|v| v.parse::<f32>().ok()) Some(d) => d,
.context("missing duration")?; None => {
let metadata = get_attribute_from_meta("duration", path)
.await
.context("can't find duration anywhere")?;
let res = metadata.parse::<f32>();
//see if metadatat had seconds directly
if let Ok(d) = res {
d
} else {
// try to convert 00:00:00:00 to 0.000s
if !metadata.contains(":") {
return Err(anyhow::anyhow!("can't find duration of media anywhere"));
} else {
let mut res = 0.;
let mut iter = metadata.split(':').rev();
let secs = iter.next().map(|n| n.parse::<f32>().ok()).flatten();
let mins = iter
.next()
.map(|n| n.parse::<f32>().ok().map(|m| m * 60.))
.flatten();
let hrs = iter
.next()
.map(|n| n.parse::<f32>().ok().map(|h| h * 3600.))
.flatten();
let days = iter
.next()
.map(|n| n.parse::<f32>().ok().map(|d| d * 24. * 3600.))
.flatten();
if let Some(s) = secs {
res = res + s
};
if let Some(m) = mins {
res = res + m
};
if let Some(h) = hrs {
res = res + h
};
if let Some(d) = days {
res = res + d
};
res
}
}
}
};
dbg!(&duration);
let old_kbit_rate = mem let old_kbit_rate = mem
.get(3) .get(3)
.and_then(|v| v.parse::<u32>().ok().map(|v| v / 1000)); .and_then(|v| v.parse::<u32>().ok().map(|v| v / 1000));
let resolution = width.zip(height); let resolution = width.zip(height);
// if let Ok(dur) = parse_duration(text) {
// duration = dur
// } else {
// bail!("FFProbe missing duration in media. Is file corrupted or non-existent?")
// }
// let old_kbit_rate = parse_bitrate(text).ok();
//
// let mut resolution = None;
// if text.contains("Stream") {
// resolution = parse_resolution(text).ok();
// }
//
Ok(MediaData { Ok(MediaData {
duration, duration,
resolution, resolution,
@ -395,67 +432,26 @@ async fn parse_ffprobe(path: &PathBuf) -> anyhow::Result<MediaData> {
}) })
} }
fn parse_duration(text: &str) -> anyhow::Result<f32> { async fn get_attribute_from_meta(attr: &str, path: &PathBuf) -> Option<String> {
let text = text[text.find("Duration").unwrap()..].to_owned(); let ffprobe = Command::new("ffprobe")
let dur_text = text[text .args([
.find(':') "-v",
.context("something wrong with the ffprobe output")? "error",
+ 2 "-select_streams",
..text "v:0",
.find(',') "-show_entries",
.context("something wrong with the ffprobe output")?] &format!("stream={attr}:stream_args={attr}"),
.to_owned(); "-of",
let durs_text: Vec<&str> = dur_text.split(':').collect(); "csv=s=,:p=0",
let mut durs_text_iter = durs_text.into_iter(); ])
let h = durs_text_iter .arg(path)
.next() .stderr(Stdio::piped())
.context("something wrong with the ffprobe output")? .output()
.parse::<f32>()?; .await
let m = durs_text_iter .ok()?;
.next() ffprobe.status.exit_ok().ok()?;
.context("something wrong with the ffprobe output")?
.parse::<f32>()?;
let s = durs_text_iter
.next()
.context("something wrong with the ffprobe output")?
.parse::<f32>()?;
Ok(h * 60. * 60. + m * 60. + s)
}
// fn parse_bitrate(text: &str) -> anyhow::Result<u16> { std::str::from_utf8(&ffprobe.stdout)
// let text = text[text.find("bitrate").unwrap()..].to_owned(); .ok()
// let bitrate_text = text[9..text.find('/').unwrap() - 2].to_owned(); .map(|v| v.to_string())
// }
// Ok(bitrate_text.parse::<u16>()?)
// }
//
// fn parse_resolution(text: &str) -> anyhow::Result<(u16, u16)> {
// let text = text[text.find("Stream").unwrap()..].to_owned();
// let sar_i = text
// .find("[SAR ")
// .context("something wrong with the ffprobe output")?
// - 1;
//
// let rb_b4_sar_i = text[..sar_i]
// .rfind(',')
// .context("something wrong with the ffprobe output")?
// + 1;
//
// let res_text = text[rb_b4_sar_i..sar_i].to_owned();
// let res_text = res_text.trim().to_owned();
//
// let width = res_text[..res_text
// .find('x')
// .context("something wrong with ffprobe output")?]
// .to_owned()
// .parse::<u16>()?;
//
// let height = res_text[res_text
// .find('x')
// .context("something wrong with ffprobe output")?
// + 1..]
// .to_owned()
// .parse::<u16>()?;
//
// Ok((width, height))
// }

BIN
test.avi Normal file

Binary file not shown.

BIN
test.mkv Normal file

Binary file not shown.