---
title: "Mux WebM without re-encoding in C#"
url: https://recursiveai.org/onemedia/cookbook/mux-packets-to-webm/
description: "Write encoded packets to WebM in C# with OneMedia. Copy VP8 payloads, finish the container and read back its codec and timestamps without re-encoding."
version: "1.0.2"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMedia) · [GitHub](https://github.com/recursive-ai-oss/onemedia-csharp) · [Docs](https://recursiveai.org/onemedia/docs/) · [Download example (.zip)](https://recursiveai.org/onemedia/cookbook/mux-packets-to-webm/mux-packets-to-webm.zip)

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

```shell
dotnet add package RecursiveAI.OneMedia
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemedia/cookbook/mux-packets-to-webm/mux-packets-to-webm.zip), then `dotnet run`.

## Full program

```csharp
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

```csharp
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
```

## Related recipes

- [Extract text subtitles from MKV](https://recursiveai.org/onemedia/cookbook/extract-subtitles-from-mkv/)
- [List video tracks](https://recursiveai.org/onemedia/cookbook/list-video-tracks/)
- [Convert MKV to MP4](https://recursiveai.org/onemedia/cookbook/convert-mkv-to-mp4/)

---

## 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](https://recursiveai.org/onemedia/docs/muxing-matroska/) 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](https://recursiveai.org/onemedia/docs/muxing-matroska/#tracks) 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](https://recursiveai.org/onemedia/docs/muxing-matroska/#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.
