Recursive AI

Mux WebM without re-encoding in C#

OneMedia is a free, open-source media library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Copy encoded packets without encoding. WebM accepts VP8, VP9, AV1, Vorbis and Opus. The zip’s blue.webm is a one-second blue square.

Terminal
dotnet add package RecursiveAI.OneMedia

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Matroska;
 
var bytes = File.ReadAllBytes("blue.webm");
using var input = MatroskaDemuxer.Open(
    new MemoryMediaSource(bytes));
using var output = File.Create("out.webm");
using var mux = new MatroskaMuxer(
    output, input.Tracks,
    new() { Profile = MatroskaProfile.WebM });
while (input.TryReadPacket(out var packet))
    mux.WritePacket(packet.Info, packet.Data);
mux.Finish();
Console.WriteLine("Wrote out.webm");

Copies both VP8 packets to out.webm.

Text
Wrote out.webm

Unzip the example, then dotnet run.

Full program

C#
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Matroska;
 
using (var input = MatroskaDemuxer.Open(new MemoryMediaSource(File.ReadAllBytes("blue.webm"))))
using (var output = File.Create("out.webm"))
{
    using var mux = new MatroskaMuxer(output, input.Tracks,
        new MatroskaMuxOptions { Profile = MatroskaProfile.WebM });
    int count = 0;
    while (input.TryReadPacket(out var packet))
    {
        mux.WritePacket(packet.Info, packet.Data);
        count++;
    }
    mux.Finish();
    Console.WriteLine($"Copied {count} packets to out.webm");
}
using var reopened = MatroskaDemuxer.Open(new MemoryMediaSource(File.ReadAllBytes("out.webm")));
Console.WriteLine($"Codec: {reopened.Tracks[0].Codec}");
while (reopened.TryReadPacket(out var packet))
    Console.WriteLine($"PTS: {packet.Info.Pts}");
Text
Copied 2 packets to out.webm
Codec: vp8
PTS: 0/1000
PTS: 500/1000

Unsupported codecs or invalid packets throw MuxRefusedException before the offending track or packet is written.

Variations

Live-mode output

C#
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Matroska;
 
var bytes = File.ReadAllBytes("blue.webm");
using var input = MatroskaDemuxer.Open(
    new MemoryMediaSource(bytes));
using var output = File.Create("live.webm");
using var mux = new MatroskaMuxer(
    output, input.Tracks,
    new() { Profile = MatroskaProfile.WebM, Live = true });
while (input.TryReadPacket(out var packet))
    mux.WritePacket(packet.Info, packet.Data);
mux.Finish();
Console.WriteLine("Wrote live.webm");
Text
Wrote live.webm

The short answer

Use MatroskaMuxer with MatroskaProfile.WebM, declare compatible encoded tracks, write packets in decode order, and call Finish. The muxer copies payload bytes unchanged. It does not turn raw frames into VP8 or turn an unsupported codec into a supported one.

The example reads an existing VP8 WebM, copies its two encoded packets, finishes the new container and reopens it. The reopened track is still VP8, with presentation times of 0 and 500 milliseconds. The input is a generated one-second blue square, not third-party footage.

How do I create a WebM file in C#?

The snippet at the top gives the muxer a stream and the demuxer’s neutral track descriptions. This preserves codec setup data instead of guessing it from the filename. A manually supplied track must likewise describe its codec, time base, dimensions or audio properties, and required configuration.

The muxer documentation specifies AddTrack, WritePacket and Finish. The constructor overload used here declares all input tracks together. Write the packet while its view is still valid: a demuxer can reuse the buffer on its next read. The sample sends each packet immediately rather than storing references to all packet views.

Use Finish before reopening or distributing the file. With seekable output it closes the remaining cluster and patches the segment size, duration and seek structures. Disposing the surrounding file stream is not a substitute for completing the container through the muxer’s API.

Can I convert MP4 to WebM without re-encoding?

Only when the encoded tracks are already compatible with the requested WebM profile and their setup data can be represented. OneMedia’s track rules allow VP8, VP9 and AV1 video, and Vorbis and Opus audio in WebM mode. Other codecs are refused with MuxRefusalReason.UnsupportedCodec naming the codec ID.

Changing the extension from .mp4 to .webm does not solve that restriction. Nor does selecting the broader Matroska profile make an otherwise unsupported WebM track acceptable to WebM consumers. If the input needs a codec change, that is an encoding task outside this packet-copy recipe.

Even with compatible payloads, the complete files will not be byte-identical. Container headers, clusters and cue structures are rewritten. Timestamps may also be rescaled to the output unit. The muxer uses nearest rounding and reports rounding through MuxReport.Notes; preserve and inspect that report when the application needs to account for time-base changes.

Can I use WebM for live streaming?

The variation sets Live = true. This is the mode for a destination that cannot seek. It also works with the ordinary file stream used in the example, which lets you inspect its result locally. It changes the layout, not the codec and not the source of the packets.

In live mode, segments and clusters have unknown sizes, and blocks are written as they arrive. There are no Cues, final Duration, SeekHead patch, lacing or CRC. If frames came from one laced block, they are written separately with their shared timestamp. Choose seekable output when you need the muxer’s final index and duration information.

Packets must still have usable timestamps and nondecreasing DTS within each track. Presentation timestamps determine block times; DTS gates decode order. A typed MuxRefusedException reports invalid tracks, missing configuration, missing timestamps, ordering problems or unsuitable output. A refusal happens before the offending track or packet is written, but it does not mean all previously written bytes are rolled back. Write to a temporary destination if your application must publish only finished files.

All onemedia recipes