pub trait Encoder: Send {
// Required methods
fn init(&mut self, width: u32, height: u32) -> Result<()>;
fn encode_frame(&mut self, rgb_data: &[u8], timestamp_ms: u64) -> Result<()>;
fn finalize(self: Box<Self>) -> Result<()>;
fn extensions(&self) -> &[&str];
// Provided method
fn supports_extension(&self, ext: &str) -> bool { ... }
}Expand description
Trait for video/animation encoders
Implementors of this trait can encode frames into video files. The encoding process has three phases:
- Initialization (
init): Set up the encoder with frame dimensions - Encoding (
encode_frame): Add frames one at a time - Finalization (
finalize): Flush buffers and close the file
§Example Implementation
ⓘ
struct MyEncoder { /* ... */ }
impl Encoder for MyEncoder {
fn init(&mut self, width: u32, height: u32) -> Result<()> {
// Set up encoder for given dimensions
Ok(())
}
fn encode_frame(&mut self, rgb_data: &[u8], timestamp_ms: u64) -> Result<()> {
// Encode one frame
Ok(())
}
fn finalize(self: Box<Self>) -> Result<()> {
// Finish encoding and write file
Ok(())
}
fn extensions(&self) -> &[&str] {
&["mp4", "webm"]
}
}Required Methods§
Sourcefn init(&mut self, width: u32, height: u32) -> Result<()>
fn init(&mut self, width: u32, height: u32) -> Result<()>
Initialize the encoder with frame dimensions
Must be called before encode_frame. The width and height
must remain constant for all frames.
Sourcefn encode_frame(&mut self, rgb_data: &[u8], timestamp_ms: u64) -> Result<()>
fn encode_frame(&mut self, rgb_data: &[u8], timestamp_ms: u64) -> Result<()>
Encode a single frame
§Arguments
rgb_data- Raw RGB pixel data (width * height * 3 bytes)timestamp_ms- Frame timestamp in milliseconds
Sourcefn finalize(self: Box<Self>) -> Result<()>
fn finalize(self: Box<Self>) -> Result<()>
Finalize encoding and write the output file
This consumes the encoder and must be called to produce valid output.
Sourcefn extensions(&self) -> &[&str]
fn extensions(&self) -> &[&str]
Get supported file extensions for this encoder
Provided Methods§
Sourcefn supports_extension(&self, ext: &str) -> bool
fn supports_extension(&self, ext: &str) -> bool
Check if this encoder supports the given file extension