Use pconv as a Rust library
The pconv package is also a library. Add it without the default signals feature, which only serves the binary:
[dependencies]pconv = { git = "https://github.com/gmrdad82/pconv", tag = "vX.Y.Z", default-features = false }Replace vX.Y.Z with a tag. It needs ffmpeg and ffprobe on the PATH, as the binary does.
Probe, plan, run
Section titled “Probe, plan, run”A conversion is three calls: probe::file reads the source, plan::build turns a Request into ffmpeg steps (join::build for a join, with every clip’s Media), and run::execute runs them:
use pconv::{Event, Profile, Request, Size, plan, probe, run};use std::sync::atomic::AtomicBool;
fn main() -> Result<(), pconv::Error> { let mut request = Request::new("talk.mp4", "small.mp4"); request.profile = Profile::Best; request.size = Some(Size::parse("480")?); let media = probe::file(&request.input)?; let plan = plan::build(&request, &media)?; let stop = AtomicBool::new(false); let outcome = run::execute(&plan, &run::direct, &stop, &mut |event| { if let Event::Stage { stage, .. } = event { eprintln!("{stage:?}"); } })?; let path = outcome.output.display(); println!("{path} {} bytes in {:.2} s", outcome.bytes, outcome.seconds); Ok(())}$ cargo run --example libraryEncodesmall.mp4 632759 bytes in 1.42 sThis is examples/library.rs in the repository.
What each part does
Section titled “What each part does”Requestholds what the command line takes:profile,size,fps,quality(the CRF or QP),tune,overwrite,loudness(Loudness::youtube()orLoudness::new(-16.0)),audio_bitrate, andjoinsfor the clips after the first.Request::new(input, output)starts from the defaults.plan::buildchecks the request against the source and returns aPlan: the encoder, the size and rate, and the ffmpegsteps. The output’s extension picks the container, as on the command line.run::executeruns each step through the launcher you pass (run::directrunsffmpegwith the step’s arguments; pass your own to wrap it), writes a hidden partial and renames it into place. Settingstoptotruestops ffmpeg and removes the partial. The callback gets anEvent::Stagewhen a step starts andEvent::Progresswith the frame count as it goes.Outcomehas the output’s path,sha256,bytes,seconds, thedevice, every step’s ffmpegargs, and the loudness report when the request levelled the audio.- Errors are a
pconv::Error, whoseDisplayis the message the binary prints afterpconv:, without the binary’s hints such asuse --force.
The API reference has every public type.