Skip to main content

ez_ffmpeg/
lib.rs

1// In test builds, libtest-generated code references test items through the
2// deprecated `opengl` module path, which a file-level allow cannot cover.
3#![cfg_attr(test, allow(deprecated))]
4// Safety-hygiene lint (clippy-only; does not affect normal builds). Every
5// public `unsafe fn` must document its contract with a `# Safety` section.
6// Broader gates (`clippy::undocumented_unsafe_blocks`, `unsafe_op_in_unsafe_fn`)
7// are deferred until the pre-existing unsafe-doc/import backlog is paid down.
8#![warn(clippy::missing_safety_doc)]
9
10//! # ez-ffmpeg
11//!
12//! **ez-ffmpeg** provides a safe and ergonomic Rust interface for [FFmpeg](https://ffmpeg.org)
13//! integration. By abstracting away much of the raw C API complexity,
14//! It abstracts the complexity of the raw C API, allowing you to configure media pipelines,
15//! perform transcoding and filtering, and inspect streams with ease.
16//!
17//! ## Crate Layout
18//!
19//! - **`core`**: The foundational module that contains the main building blocks for configuring
20//!   and running FFmpeg pipelines. This includes:
21//!   - `Input` / `Output`: Descriptors for where media data comes from and goes to (files, URLs,
22//!     custom I/O callbacks, etc.).
23//!   - `FilterComplex` and [`FrameFilter`](filter::frame_filter::FrameFilter): Mechanisms for applying FFmpeg filter graphs or
24//!     custom transformations.
25//!   - `container_info`: Utilities to extract information about the container, such as duration and format details.
26//!   - `stream_info`: Utilities to query media metadata (duration, codecs, etc.).
27//!   - `hwaccel`: Helpers for enumerating and configuring hardware-accelerated video codecs
28//!     (CUDA, VAAPI, VideoToolbox, etc.).
29//!   - `codec`: Tools to list and inspect available encoders/decoders.
30//!   - `device`: Utilities to discover system cameras, microphones, and other input devices.
31//!   - `filter`: Query FFmpeg's built-in filters and infrastructure for building custom frame-processing filters.
32//!   - `context`: Houses [`FfmpegContext`] for assembling an FFmpeg job.
33//!   - `scheduler`: Provides [`FfmpegScheduler`] which manages the lifecycle of that job.
34//!
35//! - **`wgpu_filter`** (feature `"wgpu"`): GPU-accelerated frame filters via wgpu
36//!   (Vulkan/Metal/DX12/GL). Provide a WGSL fragment shader and apply effects with
37//!   correct color handling, headless operation, and GPU/CPU overlap.
38//!
39//! - **`opengl`** (feature `"opengl"`, deprecated): The former OpenGL filter path,
40//!   superseded by `wgpu_filter`. Kept for backward compatibility; it requires a
41//!   display connection and will be removed in a future major release.
42//!
43//! - **`rtmp`** (feature `"rtmp"`): Embedded RTMP server `EmbedRtmpServer` built for production streaming,
44//!   using native epoll/kqueue/WSAPoll via libc FFI (edge-triggered on Linux/macOS, level-triggered on Windows),
45//!   zero-copy GOP fanout with `Arc<[FrameData]>`, and tiered backpressure (1/2/4MB) on a 2-thread model;
46//!   10,000+ conns on Linux/macOS (8,000 on Windows) with in-process ingest (no TCP between FFmpeg and server).
47//!
48//! - **`flv`** (feature `"flv"`): Provides data structures and helpers for handling FLV
49//!   containers, useful if you’re working with RTMP or other FLV-based workflows.
50//!
51//! - **`subtitle`** (feature `"subtitle"`): Burns ASS/SRT subtitles onto video frames inside
52//!   the frame pipeline with a pure-Rust renderer — independent of whether the linked FFmpeg
53//!   was built with `--enable-libass`. Accepts subtitle files or in-memory scripts and
54//!   explicit font files.
55//!
56//! ## Basic Usage
57//!
58//! For a simple pipeline, you typically do the following:
59//!
60//! 1. Build a [`FfmpegContext`] by specifying at least one [input](Input)
61//!    and one [output](Output). Optionally, add filter descriptions
62//!    (`filter_desc`) or attach [`FrameFilter`](filter::frame_filter::FrameFilter) pipelines at either the input (post-decode)
63//!    or the output (pre-encode) stage.
64//! 2. Create an [`FfmpegScheduler`] from that context, then call `start()` and `wait()` (or `.await`
65//!    if you enable the `"async"` feature) to run the job.
66//!
67//! ```rust,ignore
68//! use ez_ffmpeg::FfmpegContext;
69//! use ez_ffmpeg::FfmpegScheduler;
70//!
71//! fn main() -> Result<(), Box<dyn std::error::Error>> {
72//!     // 1. Build the FFmpeg context
73//!     let context = FfmpegContext::builder()
74//!         .input("input.mp4")
75//!         .filter_desc("hue=s=0") // Example filter: desaturate
76//!         .output("output.mov")
77//!         .build()?;
78//!
79//!     // 2. Run it via FfmpegScheduler (sync mode)
80//!     let result = FfmpegScheduler::new(context)
81//!         .start()?
82//!         .wait();
83//!     result?; // If any error occurred, propagate it
84//!     Ok(())
85//! }
86//! ```
87//!
88//! ## Feature Flags
89//!
90//! **`ez-ffmpeg`** uses Cargo features to provide optional functionality. By default, no optional
91//! features are enabled, allowing you to keep dependencies minimal. You can enable features as needed
92//! in your `Cargo.toml`:
93//!
94//! ```toml
95//! [dependencies.ez-ffmpeg]
96//! version = "*"
97//! features = ["wgpu", "rtmp", "flv", "async"]
98//! ```
99//!
100//! ### Core Features
101//!
102//! - **`wgpu`**: Enables wgpu-based GPU filters (WGSL shaders, headless-capable).
103//! - **`opengl`** (deprecated): Enables the former OpenGL-based filters; superseded by `wgpu`.
104//! - **`rtmp`**: Embedded RTMP server tuned for scale (10,000+ conns on Linux/macOS, 8,000 on Windows),
105//!   native epoll/kqueue/WSAPoll IO (edge-triggered on Linux/macOS), zero-copy GOP, and in-process ingest
106//!   that avoids TCP between FFmpeg and server.
107//! - **`flv`**: Adds FLV container parsing and handling.
108//! - **`subtitle`**: Native ASS/SRT subtitle burn-in rendered in pure Rust — no system
109//!   libraries beyond FFmpeg itself (see the `subtitle` module docs).
110//! - **`async`**: Adds asynchronous functionality: [`FfmpegScheduler`] additionally implements
111//!   `Future`, so a running scheduler can be `.await`ed as a non-blocking alternative to the
112//!   always-available synchronous `wait()`.
113//! - **`static`**: Uses static linking for FFmpeg libraries (via `ffmpeg-next/static`).
114//!
115//! ## Relationship to the FFmpeg CLI
116//!
117//! The transcoding pipeline (demux -> decode -> filter -> encode -> mux) is
118//! ported from the FFmpeg CLI sources, `fftools/ffmpeg` of **FFmpeg 7.x**:
119//! function names, timestamp handling and scheduling semantics follow that
120//! release, and code comments cite the corresponding fftools file and line
121//! (line numbers refer to the FFmpeg `n7.1` tag).
122//! If you know `ffmpeg_demux.c` or `ffmpeg_filter.c`, grepping this crate
123//! for the same function names (`ts_fixup`, `video_sync_process`,
124//! `enc_open`, `mux_fixup_ts`, ...) lands in the equivalent Rust.
125//!
126//! Bitstream filters (`-bsf:v/-bsf:a/-bsf:s`) are supported through
127//! [`Output::set_video_bsf`](crate::core::context::output::Output::set_video_bsf)
128//! and its audio/subtitle siblings (single filter or comma-separated chain).
129//!
130//! Not every CLI feature is implemented. Notable gaps: progress/stats
131//! reporting (`-progress`), sub2video (rendering bitmap subtitles into
132//! video), `-fix_sub_duration`, and two-pass encoding. Unsupported paths
133//! fail with explicit errors rather than approximations.
134//!
135//! ## Logging
136//!
137//! FFmpeg's own diagnostics (av_log) are redirected into the Rust `log`
138//! facade under the [`FFMPEG_LOG_TARGET`] target. Without a logger installed
139//! (env_logger, tracing-log, ...) all FFmpeg messages are silently dropped —
140//! including decoder errors that explain a failing job. Use
141//! [`set_ffmpeg_log_level`] to bound the forwarded verbosity and
142//! `Input::set_log_level_offset` to shift it per input.
143//!
144//! ## License Notice
145//!
146//! ez-ffmpeg is licensed under your choice of MIT, Apache-2.0, or MPL-2.0
147//! (matching the `license` field in Cargo.toml).
148//!
149//! **Note:** FFmpeg itself is subject to its own licensing terms. When enabling features that incorporate FFmpeg components,
150//! please ensure that your usage complies with FFmpeg's license.
151
152pub mod core;
153pub mod error;
154pub mod util;
155
156/// Internal RAII wrappers concentrating raw FFmpeg FFI pointers (Rung-2 boundary).
157pub(crate) mod raw;
158
159pub use self::core::analysis;
160pub use self::core::codec;
161pub use self::core::container_info;
162pub use self::core::context::ffmpeg_context::FfmpegContext;
163pub use self::core::context::input::Input;
164pub use self::core::context::output::Output;
165pub use self::core::device;
166pub use self::core::filter;
167pub use self::core::hwaccel;
168pub use self::core::packet_scanner;
169pub use self::core::recipes;
170pub use self::core::scheduler::ffmpeg_scheduler::FfmpegScheduler;
171pub use self::core::stream_info;
172pub use self::core::{set_ffmpeg_log_level, FfmpegLogLevel, FFMPEG_LOG_TARGET};
173
174// ez-ffmpeg is a thin FFmpeg wrapper, so FFmpeg's core types appear in the public
175// API by design (e.g. `StreamInfo` carries an `AVCodecID`, an audio stream an
176// `AVChannelOrder`, and a filter's info carries `filter::Flags`). They are
177// re-exported here so downstream code can name them via `ez_ffmpeg::` without a
178// direct `ffmpeg-next` / `ffmpeg-sys-next` dependency (`AVSampleFormat` no longer
179// appears in builder signatures but custom FrameFilters still probe frame formats
180// with it). Feature-specific ecosystem types (`bytes` for `flv`, `bytemuck`/`glow`
181// for the GPU features) are intentionally left exposed to callers already working
182// in those ecosystems.
183pub use ffmpeg_next::filter::Flags as FilterFlags;
184pub use ffmpeg_next::Frame;
185pub use ffmpeg_sys_next::AVChannelOrder;
186pub use ffmpeg_sys_next::AVCodecID;
187pub use ffmpeg_sys_next::AVHWDeviceType;
188pub use ffmpeg_sys_next::AVMediaType;
189pub use ffmpeg_sys_next::AVRational;
190pub use ffmpeg_sys_next::AVSampleFormat;
191
192#[cfg(feature = "opengl")]
193#[deprecated(
194    since = "0.11.0",
195    note = "the OpenGL filter path is superseded by `wgpu_filter` (feature \"wgpu\"): it needs a \
196            display connection and converts colors on the CPU; see the module docs for migration"
197)]
198pub mod opengl;
199#[cfg(feature = "opengl")]
200use surfman::declare_surfman;
201#[cfg(feature = "opengl")]
202declare_surfman!();
203
204#[cfg(feature = "wgpu")]
205pub mod wgpu_filter;
206
207#[cfg(feature = "rtmp")]
208pub mod rtmp;
209
210#[cfg(feature = "flv")]
211pub mod flv;
212
213#[cfg(feature = "subtitle")]
214pub mod subtitle;