Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M9.15: filmcraft-cli probe reports MPEG TS/PS programs, streams and access units

Brandon Thomas89c856510661
diff --git a/Cargo.lock b/Cargo.lock
index 637edb650..5d0e90bd3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1746,6 +1746,7 @@ dependencies = [
"filmcraft-codecs",
"filmcraft-engine",
"filmcraft-media",
+ "filmcraft-mpegts",
"filmcraft-mxf",
"filmcraft-ogg",
"filmcraft-time",
diff --git a/apps/filmcraft-cli/Cargo.toml b/apps/filmcraft-cli/Cargo.toml
index ac19c2b91..d23b772c8 100644
--- a/apps/filmcraft-cli/Cargo.toml
+++ b/apps/filmcraft-cli/Cargo.toml
@@ -22,6 +22,7 @@ filmcraft-engine = { workspace = true }
filmcraft-media = { workspace = true }
filmcraft-codecs = { workspace = true }
filmcraft-mxf = { workspace = true }
+filmcraft-mpegts = { workspace = true }
filmcraft-ogg = { workspace = true }
filmcraft-time = { workspace = true }
filmcraft-automation = { workspace = true }
diff --git a/apps/filmcraft-cli/src/probe.rs b/apps/filmcraft-cli/src/probe.rs
index 27e3537ad..c5f97ba17 100644
--- a/apps/filmcraft-cli/src/probe.rs
+++ b/apps/filmcraft-cli/src/probe.rs
@@ -1,4 +1,5 @@
-//! `filmcraft-cli probe`: media info as JSON, plus container details for MXF (operational
+//! `filmcraft-cli probe`: media info as JSON, plus container details for MPEG TS / PS (program,
+//! streams with their codecs, access units, picture types and timestamp ranges), MXF (operational
//! pattern, tracks, index, timecode), Ogg (logical streams, pre-skip, granules), Broadcast WAV
//! (time reference) and image sequences (frames, missing numbers).
@@ -79,6 +80,56 @@ fn ogg_details(src: &filmcraft_codecs::OggSource) -> Value {
json!({"streams": streams, "warnings": f.warnings})
}
+fn mpeg_details(src: &filmcraft_codecs::MpegSource) -> Value {
+ let Some(f) = src.file() else {
+ return json!({"format": "MPEG video elementary stream"});
+ };
+ let (vi, ai) = src.stream_indices();
+ let streams: Vec<Value> = f
+ .streams
+ .iter()
+ .enumerate()
+ .map(|(i, s)| {
+ let id = match s.id {
+ filmcraft_mpegts::StreamId::Pid(pid) => json!({"pid": pid}),
+ filmcraft_mpegts::StreamId::Ps { stream_id, sub_id } => {
+ json!({"streamId": format!("0x{stream_id:02x}"), "subStreamId": sub_id.map(|x| format!("0x{x:02x}"))})
+ }
+ };
+ let mut v = json!({
+ "id": id,
+ "streamType": format!("0x{:02x}", s.stream_type),
+ "codec": s.codec.name(),
+ "kind": format!("{:?}", s.kind()),
+ "units": s.units.len(),
+ "keyUnits": s.units.iter().filter(|u| u.key).count(),
+ "pesPackets": s.pes_packets,
+ "ptsRange": s.pts_range().map(|(a, b)| json!([a, b])),
+ "used": Some(i) == vi || Some(i) == ai,
+ });
+ if let Some(l) = &s.language {
+ v["language"] = json!(l);
+ }
+ if let Some(l) = s.lpcm {
+ v["lpcm"] = json!({"sampleRate": l.sample_rate, "channels": l.channels, "bits": l.bits});
+ }
+ if s.units.iter().any(|u| u.picture.is_some()) {
+ let p = |t: u8| s.units.iter().filter(|u| u.picture.is_some_and(|p| p.coding_type == t)).count();
+ v["pictures"] =
+ json!({"I": p(1), "P": p(2), "B": p(3), "fieldPairs": s.units.iter().filter(|u| u.picture.is_some_and(|p| p.structure != 3)).count()});
+ }
+ v
+ })
+ .collect();
+ json!({
+ "format": f.format.name(),
+ "program": f.program,
+ "pcrRange": f.pcr_range.map(|(a, b)| json!([a, b])),
+ "streams": streams,
+ "warnings": f.warnings,
+ })
+}
+
/// Probe `path`; with `image_sequence`, `path` is the first frame of a numbered still sequence.
pub fn probe(path: &str, image_sequence: bool) -> Result<Value, String> {
if image_sequence {
@@ -111,6 +162,12 @@ pub fn probe(path: &str, image_sequence: bool) -> Result<Value, String> {
v["mxf"] = mxf_details(&src);
return Ok(v);
}
+ if filmcraft_codecs::mpeg::sniff(head) {
+ let src = filmcraft_codecs::MpegSource::open(&name, bytes).map_err(|e| format!("{path}: {e}"))?;
+ let mut v = serde_json::to_value(filmcraft_media::MediaSource::info(&src)).unwrap_or_default();
+ v["mpeg"] = mpeg_details(&src);
+ return Ok(v);
+ }
if filmcraft_ogg::sniff(head)
&& let Ok(src) = filmcraft_codecs::OggSource::open(&name, bytes.clone())
{
diff --git a/apps/filmcraft-cli/tests/cli.rs b/apps/filmcraft-cli/tests/cli.rs
index 7fd219ee3..b7fedfc2c 100644
--- a/apps/filmcraft-cli/tests/cli.rs
+++ b/apps/filmcraft-cli/tests/cli.rs
@@ -24,6 +24,36 @@ fn help_and_usage_errors() {
assert_eq!(cli(&["exec", "file.newBin", "notkv"]).status.code(), Some(2));
}
+#[test]
+fn probe_reports_mpeg_transport_and_program_streams() {
+ let Some(ffmpeg) = ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg", "/usr/bin/ffmpeg"].into_iter().find(|p| std::path::Path::new(p).exists()) else {
+ eprintln!("SKIPPED (probe_reports_mpeg_transport_and_program_streams): ffmpeg not found");
+ return;
+ };
+ let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/cli-tests").join(format!("mpeg-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ for (name, fmt, extra) in [("a.ts", "mpegts", &["-mpegts_m2ts_mode", "0"][..]), ("b.vob", "vob", &[][..])] {
+ let out = dir.join(name);
+ let st = Command::new(ffmpeg)
+ .args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000"])
+ .args(["-t", "0.5", "-c:v", "mpeg2video", "-c:a", "mp2", "-ac", "2", "-f", fmt])
+ .args(extra)
+ .arg(&out)
+ .status()
+ .unwrap();
+ assert!(st.success());
+ let v = json_out(&cli(&["probe", out.to_str().unwrap()]));
+ assert!(v["video"]["codec"].as_str().unwrap().starts_with("MPEG-2 Video"), "{v}");
+ assert_eq!(v["audio"]["codec"], "MPEG Audio");
+ let streams = v["mpeg"]["streams"].as_array().unwrap();
+ assert_eq!(streams.len(), 2, "{v}");
+ assert_eq!(streams[0]["codec"], "MPEG-2 Video");
+ assert!(streams[0]["pictures"]["I"].as_u64().unwrap() >= 1);
+ assert_eq!(v["mpeg"]["format"], if fmt == "mpegts" { "MPEG-2 TS" } else { "MPEG-2 PS" });
+ }
+ let _ = std::fs::remove_dir_all(&dir);
+}
+
#[test]
fn exec_inspect_and_describe() {
let seq = json_out(&cli(&["--demo", "inspect", "sequence", "--compact"]));