Audio and video processing can often seem daunting, but in development, we frequently encounter related requirements such as format conversion, editing, watermarking, and audio extraction.

FFmpeg is an industry-standard tool that is almost omnipotent; many popular software applications (like VLC, YouTube, OBS) rely on it. However, FFmpeg also presents challenges for developers:

  1. High learning curve: It requires understanding concepts like multiplexing/demultiplexing, encoding/decoding, pixel formats, and sampling rates.

  2. Implemented in C: Direct invocation can lead to pitfalls in memory management, potentially causing memory leaks, illegal access, and program crashes.

  3. Low-level code that's hard to maintain: One might easily fall into debugging nightmares.

Rust is renowned for its memory safety and strong performance. So, is there a simple, safe, and idiomatic way to use FFmpeg in Rust?

ez-ffmpeg: Elegant FFmpeg Usage for Rust Developers

ez-ffmpeg allows you to create and execute FFmpeg tasks through chainable method calls, just like writing regular Rust code.

It uses FFI to call FFmpeg's underlying C code and automatically manages memory, freeing you from common memory safety concerns associated with C.

Quick Start: Format Conversion in Rust

Suppose we need to convert the format of a video (or audio, image). With ez-ffmpeg, it takes just a few lines of code:

1. Install FFmpeg

If FFmpeg isn't already installed in your environment, you can install it as follows:

macOS:

brew install ffmpeg

Windows:

vcpkg install ffmpeg
# If this is your first time installing vcpkg, you'll need to set the VCPKG_ROOT environment variable

2. Add Rust Dependency

In your Cargo.toml, include ez-ffmpeg:

[dependencies]
ez-ffmpeg = "*"

3. Run the Code

use ez_ffmpeg::FfmpegContext;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Build the FFmpeg context
    let context = FfmpegContext::builder()
        .input("input.mp4")   // Input file
        .output("output.mov") // Output file
        .build()?;

    // 2. Start and wait for the task to complete
    context.start()?.wait()?;
    Ok(())
}

It's that simple! You only need to focus on the input and output, and ez-ffmpeg handles the format conversion.

More Than Just Format Conversion

ez-ffmpeg also enables easy implementation of video editing, audio extraction, filter addition, RTMP streaming, GPU acceleration, and more.

Check out the official examples: examples

Conclusion

Compared to FFmpeg's command-line approach, ez-ffmpeg allows Rust developers to seamlessly translate FFmpeg commands into code without the need to learn C.

Additionally, it supports custom filters and inputs/outputs, implemented directly in Rust, eliminating the complexity of C extensions and the pain of compiling FFmpeg.

🔗 Open Source Project: ez-ffmpeg