ff-encode
Encode video and audio to any format with a builder chain. The encoder validates your codec, resolution, and bitrate settings before allocating any FFmpeg context — invalid configurations are caught as Err, not discovered at the first push_video call.
Installation
[]
= "0.7"
= "0.7"
# Enable GPL-licensed encoders (libx264, libx265).
# Requires GPL compliance or MPEG LA licensing in your project.
# ff-encode = { version = "0.7", features = ["gpl"] }
By default, only LGPL-compatible encoders are enabled.
Quick Start
use ;
let mut encoder = create
.video // width, height, fps
.video_codec
.bitrate_mode
.audio // sample_rate, channels
.audio_codec
.audio_bitrate
.build?;
for frame in &video_frames
for frame in &audio_frames
encoder.finish?;
Quality Modes
// Constant bitrate — predictable file size and bandwidth.
.bitrate_mode // 4 Mbps
// Constant rate factor — quality-driven; file size varies.
.bitrate_mode // 0–51, lower = better
// Variable bitrate — target average with hard ceiling.
.bitrate_mode
Per-Codec Video Options
VideoCodecOptions provides typed configuration for each codec. Options are
applied via av_opt_set before avcodec_open2; unsupported values are logged
as warnings and skipped — build() never fails due to an unsupported option.
use ;
// H.264 — profile, level, B-frames, GOP, refs, preset, tune
let opts = H264;
// H.265 — profile (Main / Main10 for 10-bit HDR), preset
let opts = H265;
// AV1 (libaom) — cpu_used, tile layout, usage mode
let opts = Av1;
// AV1 (SVT-AV1 / libsvtav1) — preset 0–13, tiles, raw params
// Requires FFmpeg built with --enable-libsvtav1.
let opts = Av1Svt;
// VP9 — cpu_used, constrained quality, row-based multithreading
let opts = Vp9;
let mut encoder = create
.video
.video_codec
.bitrate_mode
.codec_options
.build?;
Per-Codec Audio Options
AudioCodecOptions provides typed configuration for Opus, AAC, MP3, and FLAC.
use ;
// Opus — application mode + frame duration, OGG container
let opts = Opus;
let mut encoder = create
.audio
.audio_codec
.audio_bitrate
.container
.codec_options
.build?;
// AAC — profile (LC / HE / HEv2), optional VBR quality
let opts = Aac;
// MP3 — VBR quality scale 0 (best) … 9 (smallest)
let opts = Mp3;
// FLAC — compression level 0–12, native FLAC container
let opts = Flac;
let mut encoder = create
.audio
.audio_codec
.container
.codec_options
.build?;
Professional Formats
ProRes and DNxHD/DNxHR require specific pixel formats and FFmpeg encoder
support (prores_ks and dnxhd respectively).
use ;
// Apple ProRes HQ — yuv422p10le, .mov container
let opts = ProRes;
let mut encoder = create
.video
.video_codec
.pixel_format
.codec_options
.build?;
// Avid DNxHR SQ — yuv422p, any resolution
let opts = Dnxhd;
let mut encoder = create
.video
.video_codec
.codec_options
.build?;
| ProRes Profile | Pixel Format | Notes |
|---|---|---|
Proxy, Lt, Standard, Hq |
yuv422p10le |
422 chroma |
P4444, P4444Xq |
yuva444p10le |
444 chroma + alpha |
| DNxHD/HR Variant | Pixel Format | Notes |
|---|---|---|
Dnxhd115, Dnxhd145, Dnxhd220 |
yuv422p |
Fixed bitrate, 1080p only |
Dnxhd220x, DnxhrHqx |
yuv422p10le |
10-bit |
DnxhrLb, DnxhrSq, DnxhrHq, DnxhrR444 |
yuv422p |
Any resolution |
HDR Metadata
use ;
// HDR10 — static metadata (MaxCLL, MaxFALL, mastering display)
// hdr10_metadata() automatically sets BT.2020 primaries, PQ transfer,
// and BT.2020 NCL colour matrix on the codec context.
let meta = Hdr10Metadata ;
let mut encoder = create
.video
.video_codec
.bitrate_mode
.pixel_format
.codec_options
.hdr10_metadata
.build?;
// HLG — broadcast HDR without MaxCLL/MaxFALL side data
let mut encoder = create
.video
.video_codec
.pixel_format
.color_transfer
.color_space
.color_primaries
.build?;
Hardware Encoding
use ;
let mut encoder = create
.video
.video_codec
.hardware_encoder
.build?;
HardwareEncoder::Auto selects NVENC, QuickSync, AMF, or VideoToolbox based on what is available at runtime. If no hardware encoder is found, the builder falls back to a software encoder automatically.
Progress Tracking
let mut encoder = create
.video
.video_codec
.bitrate_mode
.on_progress
.build?;
LGPL Compliance
By default, ff-encode only links encoders that are compatible with the LGPL license — hardware encoders (NVENC, QSV, AMF, VideoToolbox) or software encoders for VP9 and AV1.
Enable the gpl feature to add libx264 and libx265. This changes the license terms of your binary; ensure you comply with the GPL or hold an appropriate MPEG LA commercial license before distributing.
Error Handling
| Variant | When it occurs |
|---|---|
EncodeError::InvalidConfig |
Codec, resolution, or bitrate settings are invalid |
EncodeError::UnsupportedCodec |
Requested codec not available in this FFmpeg build |
EncodeError::HardwareUnavailable |
Hardware encoder requested but no device found |
EncodeError::Io |
Write error on the output file |
EncodeError::Encode |
FFmpeg returned an error during frame encoding |
Feature Flags
| Flag | Description | Default |
|---|---|---|
hwaccel |
Enables hardware encoder detection (NVENC, QSV, AMF, VideoToolbox, VA-API). | enabled |
gpl |
Enables GPL-licensed encoders (libx264, libx265). Requires GPL compliance or MPEG LA licensing. | disabled |
tokio |
Enables AsyncVideoEncoder and AsyncAudioEncoder. Each encoder runs a worker thread and exposes an async push / finish interface backed by a bounded tokio::sync::mpsc channel (capacity 8). Requires a tokio 1.x runtime. |
disabled |
[]
= { = "0.7", = ["tokio"] }
When the tokio feature is disabled, only the synchronous VideoEncoder, AudioEncoder, and ImageEncoder APIs are compiled. No tokio dependency is pulled in.
MSRV
Rust 1.93.0 (edition 2024).
License
MIT OR Apache-2.0