Skip to main content

Crate webm

Crate webm 

Source
Expand description

A crate for muxing one or more video/audio streams into a WebM file.

Note that this crate is only for muxing media that has already been encoded with the appropriate codec. Consider a crate such as vpx if you need encoding as well.

Actual writing of muxed data is done through a mux::Writer, which lets you supply your own implementation. This makes it easy to support muxing to files, in-memory buffers, or whatever else you need. Once you have a mux::Writer, you create a mux::SegmentBuilder and add the tracks you need. Finally, you create a mux::Segment with that builder, to which you can add media frames.

In typical usage of this library, where you might mux to a WebM file, you would do:

use std::fs::File;
use webm::mux::{Error, SegmentBuilder, SegmentMode, VideoCodecId, Writer};

fn main() -> Result<(), Error> {
    let file = File::create("./my-cool-file.webm").map_err(|_| Error::Unknown)?;
    let writer = Writer::new(file);

    // Build a segment with a single video track
    let builder = SegmentBuilder::new(writer)?;
    let builder = builder.set_mode(SegmentMode::Live)?; // Set live mode for streaming
    let (builder, video_track) = builder.add_video_track(640, 480, VideoCodecId::VP8, None)?;
    let mut segment = builder.build();

    // Add some video frames
    let encoded_video_frame: &[u8] = &[]; // TODO: Your video data here
    let timestamp_ns = 0;
    let is_keyframe = true;
    segment.add_frame(video_track, encoded_video_frame, timestamp_ns, is_keyframe)?;
    // TODO: More video frames

    // Done writing frames, finish off the file
    segment.finalize(None).map_err(|_| Error::Unknown)?;
    Ok(())
}

Modulesยง

mux