Encoder

Trait Encoder 

Source
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:

  1. Initialization (init): Set up the encoder with frame dimensions
  2. Encoding (encode_frame): Add frames one at a time
  3. 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§

Source

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.

Source

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
Source

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.

Source

fn extensions(&self) -> &[&str]

Get supported file extensions for this encoder

Provided Methods§

Source

fn supports_extension(&self, ext: &str) -> bool

Check if this encoder supports the given file extension

Implementors§