Skip to main content

ez_ffmpeg/
lib.rs

1//! # ez-ffmpeg
2//!
3//! **ez-ffmpeg** provides a safe and ergonomic Rust interface for [FFmpeg](https://ffmpeg.org)
4//! integration. By abstracting away much of the raw C API complexity,
5//! It abstracts the complexity of the raw C API, allowing you to configure media pipelines,
6//! perform transcoding and filtering, and inspect streams with ease.
7//!
8//! ## Crate Layout
9//!
10//! - **`core`**: The foundational module that contains the main building blocks for configuring
11//!   and running FFmpeg pipelines. This includes:
12//!   - `Input` / `Output`: Descriptors for where media data comes from and goes to (files, URLs,
13//!     custom I/O callbacks, etc.).
14//!   - `FilterComplex` and [`FrameFilter`](filter::frame_filter::FrameFilter): Mechanisms for applying FFmpeg filter graphs or
15//!     custom transformations.
16//!   - `container_info`: Utilities to extract information about the container, such as duration and format details.
17//!   - `stream_info`: Utilities to query media metadata (duration, codecs, etc.).
18//!   - `hwaccel`: Helpers for enumerating and configuring hardware-accelerated video codecs
19//!     (CUDA, VAAPI, VideoToolbox, etc.).
20//!   - `codec`: Tools to list and inspect available encoders/decoders.
21//!   - `device`: Utilities to discover system cameras, microphones, and other input devices.
22//!   - `filter`: Query FFmpeg's built-in filters and infrastructure for building custom frame-processing filters.
23//!   - `context`: Houses [`FfmpegContext`] for assembling an FFmpeg job.
24//!   - `scheduler`: Provides [`FfmpegScheduler`] which manages the lifecycle of that job.
25//!
26//! - **`opengl`** (feature `"opengl"`): Offers GPU-accelerated OpenGL filters, letting you
27//!   provide a fragment shader and apply effects to video frames in a high-performance way
28//!   without manually managing the GL context.
29//!
30//! - **`rtmp`** (feature `"rtmp"`): Embedded RTMP server `EmbedRtmpServer` built for production streaming,
31//!   using native epoll/kqueue/WSAPoll via libc FFI (edge-triggered on Linux/macOS, level-triggered on Windows),
32//!   zero-copy GOP fanout with `Arc<[FrameData]>`, and tiered backpressure (1/2/4MB) on a 2-thread model;
33//!   10,000+ conns on Linux/macOS (8,000 on Windows) with in-process ingest (no TCP between FFmpeg and server).
34//!
35//! - **`flv`** (feature `"flv"`): Provides data structures and helpers for handling FLV
36//!   containers, useful if you’re working with RTMP or other FLV-based workflows.
37//!
38//! ## Basic Usage
39//!
40//! For a simple pipeline, you typically do the following:
41//!
42//! 1. Build a [`FfmpegContext`] by specifying at least one [input](Input)
43//!    and one [output](Output). Optionally, add filter descriptions
44//!    (`filter_desc`) or attach [`FrameFilter`](filter::frame_filter::FrameFilter) pipelines at either the input (post-decode)
45//!    or the output (pre-encode) stage.
46//! 2. Create an [`FfmpegScheduler`] from that context, then call `start()` and `wait()` (or `.await`
47//!    if you enable the `"async"` feature) to run the job.
48//!
49//! ```rust,ignore
50//! use ez_ffmpeg::FfmpegContext;
51//! use ez_ffmpeg::FfmpegScheduler;
52//!
53//! fn main() -> Result<(), Box<dyn std::error::Error>> {
54//!     // 1. Build the FFmpeg context
55//!     let context = FfmpegContext::builder()
56//!         .input("input.mp4")
57//!         .filter_desc("hue=s=0") // Example filter: desaturate
58//!         .output("output.mov")
59//!         .build()?;
60//!
61//!     // 2. Run it via FfmpegScheduler (sync mode)
62//!     let result = FfmpegScheduler::new(context)
63//!         .start()?
64//!         .wait();
65//!     result?; // If any error occurred, propagate it
66//!     Ok(())
67//! }
68//! ```
69//!
70//! ## Feature Flags
71//!
72//! **`ez-ffmpeg`** uses Cargo features to provide optional functionality. By default, no optional
73//! features are enabled, allowing you to keep dependencies minimal. You can enable features as needed
74//! in your `Cargo.toml`:
75//!
76//! ```toml
77//! [dependencies.ez-ffmpeg]
78//! version = "*"
79//! features = ["opengl", "rtmp", "flv", "async"]
80//! ```
81//!
82//! ### Core Features
83//!
84//! - **`opengl`**: Enables OpenGL-based filters for GPU-accelerated processing.
85//! - **`rtmp`**: Embedded RTMP server tuned for scale (10,000+ conns on Linux/macOS, 8,000 on Windows),
86//!   native epoll/kqueue/WSAPoll IO (edge-triggered on Linux/macOS), zero-copy GOP, and in-process ingest
87//!   that avoids TCP between FFmpeg and server.
88//! - **`flv`**: Adds FLV container parsing and handling.
89//! - **`async`**: Makes the [`FfmpegScheduler`] wait method asynchronous (you can `.await` it).
90//! - **`static`**: Uses static linking for FFmpeg libraries (via `ffmpeg-next/static`).
91//!
92//! ## License Notice
93//!
94//! ez-ffmpeg is distributed under the WTFPL (Do What The F*ck You Want To Public License).
95//! You are free to use, modify, and distribute this software.
96//!
97//! **Note:** FFmpeg itself is subject to its own licensing terms. When enabling features that incorporate FFmpeg components,
98//! please ensure that your usage complies with FFmpeg's license.
99
100pub mod core;
101pub mod util;
102pub mod error;
103
104pub use self::core::context::ffmpeg_context::FfmpegContext;
105pub use self::core::context::input::Input;
106pub use self::core::context::output::Output;
107pub use self::core::scheduler::ffmpeg_scheduler::FfmpegScheduler;
108pub use self::core::container_info;
109pub use self::core::stream_info;
110pub use self::core::device;
111pub use self::core::hwaccel;
112pub use self::core::codec;
113pub use self::core::filter;
114
115pub use ffmpeg_sys_next::AVRational;
116pub use ffmpeg_sys_next::AVMediaType;
117pub use ffmpeg_next::Frame;
118
119
120#[cfg(feature = "opengl")]
121pub mod opengl;
122#[cfg(feature = "opengl")]
123use surfman::declare_surfman;
124#[cfg(feature = "opengl")]
125declare_surfman!();
126
127#[cfg(feature = "rtmp")]
128pub mod rtmp;
129
130#[cfg(feature = "flv")]
131pub mod flv;