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`**: Makes the [`FfmpegScheduler`] wait method asynchronous (you can `.await` it).
111//! - **`static`**: Uses static linking for FFmpeg libraries (via `ffmpeg-next/static`).
112//!
113//! ## Relationship to the FFmpeg CLI
114//!
115//! The transcoding pipeline (demux -> decode -> filter -> encode -> mux) is
116//! ported from the FFmpeg CLI sources, `fftools/ffmpeg` of **FFmpeg 7.x**:
117//! function names, timestamp handling and scheduling semantics follow that
118//! release, and code comments cite the corresponding fftools file and line
119//! (line numbers refer to the FFmpeg `n7.1` tag).
120//! If you know `ffmpeg_demux.c` or `ffmpeg_filter.c`, grepping this crate
121//! for the same function names (`ts_fixup`, `video_sync_process`,
122//! `enc_open`, `mux_fixup_ts`, ...) lands in the equivalent Rust.
123//!
124//! Not every CLI feature is implemented. Notable gaps: progress/stats
125//! reporting (`-progress`), sub2video (rendering bitmap subtitles into
126//! video), `-shortest` cross-stream sync, bitstream filters (`-bsf`),
127//! keyframe forcing (`-force_key_frames`), `-fix_sub_duration`, two-pass
128//! encoding, and attachments. Unsupported paths fail with explicit errors
129//! rather than approximations.
130//!
131//! ## Logging
132//!
133//! FFmpeg's own diagnostics (av_log) are redirected into the Rust `log`
134//! facade under the [`FFMPEG_LOG_TARGET`] target. Without a logger installed
135//! (env_logger, tracing-log, ...) all FFmpeg messages are silently dropped —
136//! including decoder errors that explain a failing job. Use
137//! [`set_ffmpeg_log_level`] to bound the forwarded verbosity and
138//! `Input::set_log_level_offset` to shift it per input.
139//!
140//! ## License Notice
141//!
142//! ez-ffmpeg is licensed under your choice of MIT, Apache-2.0, or MPL-2.0
143//! (matching the `license` field in Cargo.toml).
144//!
145//! **Note:** FFmpeg itself is subject to its own licensing terms. When enabling features that incorporate FFmpeg components,
146//! please ensure that your usage complies with FFmpeg's license.
147
148pub mod core;
149pub mod util;
150pub mod error;
151
152/// Internal RAII wrappers concentrating raw FFmpeg FFI pointers (Rung-2 boundary).
153pub(crate) mod raw;
154
155pub use self::core::context::ffmpeg_context::FfmpegContext;
156pub use self::core::context::input::Input;
157pub use self::core::context::output::Output;
158pub use self::core::scheduler::ffmpeg_scheduler::FfmpegScheduler;
159pub use self::core::container_info;
160pub use self::core::stream_info;
161pub use self::core::{set_ffmpeg_log_level, FfmpegLogLevel, FFMPEG_LOG_TARGET};
162pub use self::core::packet_scanner;
163pub use self::core::device;
164pub use self::core::hwaccel;
165pub use self::core::codec;
166pub use self::core::filter;
167pub use self::core::analysis;
168pub use self::core::recipes;
169
170pub use ffmpeg_sys_next::AVRational;
171pub use ffmpeg_sys_next::AVMediaType;
172pub use ffmpeg_next::Frame;
173
174
175#[cfg(feature = "opengl")]
176#[deprecated(
177    since = "0.11.0",
178    note = "the OpenGL filter path is superseded by `wgpu_filter` (feature \"wgpu\"): it needs a \
179            display connection and converts colors on the CPU; see the module docs for migration"
180)]
181pub mod opengl;
182#[cfg(feature = "opengl")]
183use surfman::declare_surfman;
184#[cfg(feature = "opengl")]
185declare_surfman!();
186
187#[cfg(feature = "wgpu")]
188pub mod wgpu_filter;
189
190#[cfg(feature = "rtmp")]
191pub mod rtmp;
192
193#[cfg(feature = "flv")]
194pub mod flv;
195
196#[cfg(feature = "subtitle")]
197pub mod subtitle;