handbrake
handbrake is a Rust crate that provides a safe, ergonomic, and asynchronous interface for controlling the HandBrakeCLI video transcoder.
It allows Rust applications to programmatically start, configure, monitor, and control HandBrake encoding jobs without needing to manually handle command-line arguments or parse raw text output.
Features
- Fluent Job Configuration: Use a builder pattern to easily configure encoding jobs (e.g.,
job.preset("Fast 1080p30").quality(22.0)). - Asynchronous API: Built on
tokio, the entire API isasync, making it suitable for modern, high-performance applications. - Real-time Monitoring: Subscribe to a stream of structured events:
Config: The full job configuration, parsed from HandBrake's JSON output.Progress: Real-time updates on percentage, FPS, and ETA.Log: Raw log messages fromHandBrakeCLI.Fragment: Rawstdoutdata, useful when piping video output.Done: Signals the completion (success or failure) of the job.
- Two Execution Modes:
- Monitored: Get a
JobHandleto receive live events and control the process. - Fire-and-Forget: Simply execute a job and wait for its final exit status.
- Monitored: Get a
- Process Control: Gracefully
cancel()or forcefullykill()a running encoding job. - Flexible Setup: Automatically finds
HandBrakeCLIin your system'sPATHor lets you specify a direct path to the executable.
Quick Start
First, add handbrake to your Cargo.toml:
[]
= "0.1.0" # Replace with the latest version
= { = "1", = ["full"] }
= "0.3"
Monitored Execution
Here is a basic example of how to start and monitor an encoding job.
use ;
use StreamExt;
use PathBuf;
async
Fire-and-Forget Execution
If you don't need real-time progress updates, you can use the status() method for a simpler, fire-and-forget approach.
use HandBrake;
use PathBuf;
async
How it Works
The crate is designed around three main components:
HandBrake: The factory for creating jobs. It locates and validates theHandBrakeCLIexecutable on your system.JobBuilder: A fluent interface to define all the parameters for a specific encoding job.JobHandle: Returned when a job is started in monitored mode. It represents the running process and gives you access to the event stream and control methods likecancel()andkill().
This crate works by spawning HandBrakeCLI as a child process and parsing its stdout and stderr streams in real-time. It parses progress indicators from stdout, and JSON configuration and logs from stderr, to generate a structured stream of JobEvents.
For a complete list of available options and detailed explanations, please refer to the official HandBrakeCLI documentation.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.