Skip to content

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.

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 library
Encode
small.mp4 632759 bytes in 1.42 s

This is examples/library.rs in the repository.

  • Request holds what the command line takes: profile, size, fps, quality (the CRF or QP), tune, overwrite, loudness (Loudness::youtube() or Loudness::new(-16.0)), audio_bitrate, and joins for the clips after the first. Request::new(input, output) starts from the defaults.
  • plan::build checks the request against the source and returns a Plan: the encoder, the size and rate, and the ffmpeg steps. The output’s extension picks the container, as on the command line.
  • run::execute runs each step through the launcher you pass (run::direct runs ffmpeg with the step’s arguments; pass your own to wrap it), writes a hidden partial and renames it into place. Setting stop to true stops ffmpeg and removes the partial. The callback gets an Event::Stage when a step starts and Event::Progress with the frame count as it goes.
  • Outcome has the output’s path, sha256, bytes, seconds, the device, every step’s ffmpeg args, and the loudness report when the request levelled the audio.
  • Errors are a pconv::Error, whose Display is the message the binary prints after pconv: , without the binary’s hints such as use --force.

The API reference has every public type.