feat: add clipbin-media for reading segments with FFmpeg
Rust checks / windows-cross-check (push) Successful in 58s
Rust checks / linux (push) Failing after 10m50s

One shared FFmpeg binding (ffmpeg-next 9, dynamically linked: the system
FFmpeg on Linux, the OBS-bundled DLLs on Windows) for clip export and
playback, with a probe of segment duration, codec, size and audio tracks.
This commit is contained in:
2026-10-08 17:55:34 +02:00
parent 36c13449dd
commit f6cdef5dd2
4 changed files with 126 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
//! Reading recorded segments with FFmpeg: clip export (remuxing without
//! re-encoding) and decoding for playback.
//!
//! FFmpeg is linked dynamically: the system's libraries on Linux (the OBS
//! runtime already depends on them) and, on Windows, the FFmpeg DLLs shipped
//! in the official OBS release that scripts/obs/assemble-windows.sh bundles.
use std::{path::Path, sync::Once, time::Duration};
pub use ffmpeg_next as ffmpeg;
/// Initialises FFmpeg once per process.
pub fn init() {
static INIT: Once = Once::new();
INIT.call_once(|| {
let _ = ffmpeg::init();
ffmpeg::log::set_level(ffmpeg::log::Level::Error);
});
}
/// Basic facts about a segment file.
#[derive(Debug, Clone, PartialEq)]
pub struct Probe {
pub duration: Duration,
pub video_codec: String,
pub width: u32,
pub height: u32,
pub audio_tracks: usize,
}
pub fn probe(path: &Path) -> Result<Probe, ffmpeg::Error> {
init();
let input = ffmpeg::format::input(path)?;
let video = input
.streams()
.best(ffmpeg::media::Type::Video)
.ok_or(ffmpeg::Error::StreamNotFound)?;
let decoder = ffmpeg::codec::context::Context::from_parameters(video.parameters())?
.decoder()
.video()?;
let micros = input.duration().max(0) as u64;
Ok(Probe {
duration: Duration::from_micros(micros),
video_codec: decoder.id().name().to_owned(),
width: decoder.width(),
height: decoder.height(),
audio_tracks: input
.streams()
.filter(|stream| stream.parameters().medium() == ffmpeg::media::Type::Audio)
.count(),
})
}
#[cfg(test)]
mod tests {
use super::*;
/// Set CLIPBIN_TEST_SEGMENT to a recorded segment to run this.
#[test]
fn probes_a_recorded_segment() {
let Some(path) = std::env::var_os("CLIPBIN_TEST_SEGMENT") else {
return;
};
let probe = probe(Path::new(&path)).unwrap();
assert!(probe.duration > Duration::ZERO);
assert!(probe.width > 0 && probe.height > 0);
println!("{probe:?}");
}
}