libffmpeg
libffmpeg is an async-first Rust library that provides a high-level interface to ffmpeg and ffprobe. Built on tokio with comprehensive tracing integration, it offers three execution modes, structured progress parsing, and graceful shutdown capabilities.Key Features
Three Execution Modes
Choose from slim (no monitoring), standard (with monitoring), or graceful (stdin-based quit with SIGKILL fallback) depending on your needs
ffprobe Support
Run ffprobe commands and extract media metadata like duration with simple, type-safe APIs
Async & Cancellable
Built on tokio with CancellationToken support for clean cancellation of long-running operations
Progress Parsing
Structured Progress type automatically parsed from ffmpeg’s
-progress pipe:1 outputTracing Integration
All functions instrumented with tracing spans and valuable support for comprehensive observability
Binary Discovery
Automatic binary discovery via libwhich with environment variable overrides for custom paths
Three Execution Modes Explained
libffmpeg provides three distinct ways to execute ffmpeg, each optimized for different use cases:ffmpeg_slim — No Monitoring
The lightest-weight variant. Spawns ffmpeg, waits for completion, and returns the exit result. Perfect when you don’t need real-time output.
ffmpeg — With Monitoring
Streams stdout/stderr through a CommandMonitorServer for real-time progress parsing or logging. Killed immediately on cancellation.
ffmpeg_graceful — Graceful Shutdown
Like standard ffmpeg, but on cancellation it sends q to stdin first, giving ffmpeg up to 5 seconds to exit cleanly before falling back to SIGKILL. This allows ffmpeg to finalize output files properly.
Binary Discovery
Bothffmpeg and ffprobe binaries are located automatically using the following lookup order:
- Environment variable — Check
LIBFFMPEG_FFMPEG_PATHorLIBFFMPEG_FFPROBE_PATHand validate it’s executable - PATH search — Fall back to searching
$PATHvia libwhich
If neither environment variable is set nor binary found in PATH, functions will return a
NotFound error.Core APIs
ffmpeg Module
| Function | Signature | Description |
|---|---|---|
ffmpeg_slim | (CancellationToken, Prepare) -> Result<CommandExit, FfmpegError> | Run ffmpeg with cancellation only |
ffmpeg | (CancellationToken, &CommandMonitorServer, Prepare) -> Result<CommandExit, FfmpegError> | Run ffmpeg with output monitoring |
ffmpeg_graceful | (CancellationToken, &CommandMonitorClient, &CommandMonitorServer, Prepare) -> Result<CommandExit, FfmpegError> | Run ffmpeg with graceful shutdown |
ffprobe Module
| Function | Signature | Description |
|---|---|---|
ffprobe | (CancellationToken, Prepare) -> Result<CommandExit, FfprobeError> | Run ffprobe with cancellation |
util Module
| Function | Signature | Description |
|---|---|---|
get_duration | (impl AsRef<Path>, CancellationToken) -> Result<Duration, DurationError> | Extract media duration via ffprobe |
Progress Parsing
When using ffmpeg with-progress pipe:1, the library can parse structured progress updates:
Progress struct includes:
frame: usize— Number of frames processedfps: f64— Current encoding speed (frames per second)bitrate: isize— Current bitrate in bytes per secondtotal_size: usize— Total output size in bytesout_time: Duration— Elapsed output timespeed: f64— Encoding speed multiplier (e.g., 2.0 = 2x realtime)progress: ProgressState— Continue, End, or Unknown
Dependencies
This crate uses several companion libraries:- libcmd — Async command execution with monitoring and cancellation
- libwhich — Binary discovery with PATH searching
- liberror — Error handling utilities
- libsignal — Signal handling (used in examples)
valuable crate for structured logging.
Next Steps
Installation
Add libffmpeg to your project and configure tracing support
Quickstart
Follow a step-by-step guide to your first transcode with progress monitoring