---
title: "Write a compact MP4 sample size table in C#"
url: https://recursiveai.org/onemedia/cookbook/write-mp4-with-compact-sample-sizes/
description: "Set CompactSampleSizes on OneMedia’s MP4 muxer to write an stz2 table when variable sample sizes fit. Copy the audio without re-encoding."
version: "1.0.2"
---

# Write a compact MP4 sample size table 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/write-mp4-with-compact-sample-sizes/write-mp4-with-compact-sample-sizes.zip)

A compact MP4 sample size table (`stz2`) stores each packet length in 1 or 2 bytes instead of 4. The ZIP’s `tone.m4a` is a short AAC tone with variable packet sizes.

```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.IsoBmff;
using var file = Media.Open("tone.m4a");
using var output = File.Create("compact.mp4");
using var mux = new IsoMuxer(output,
    new IsoMuxOptions { CompactSampleSizes = true }
);
mux.AddTrack(file.Tracks[0]);
var reader = file.Demuxer!;
while (reader.TryReadPacket(out var p))
    mux.WritePacket(p.Info, p.Data);
var report = mux.Finish();
Console.WriteLine(report.Tracks[0].Packets);
```

[The example](https://recursiveai.org/onemedia/cookbook/write-mp4-with-compact-sample-sizes/write-mp4-with-compact-sample-sizes.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Writes `compact.mp4` and prints the count of copied samples:

```text
13
```

## The full program

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.IsoBmff;

using var file = Media.Open("tone.m4a");
using var output = File.Create("compact.mp4");
using var mux = new IsoMuxer(output, new IsoMuxOptions
{
    CompactSampleSizes = true
});
var map = file.Tracks.Select(t => mux.AddTrack(t)).ToArray();
var reader = file.Demuxer!;
while (reader.TryReadPacket(out var packet))
{
    var info = packet.Info;
    info = info with { TrackIndex = map[info.TrackIndex] };
    mux.WritePacket(info, packet.Data);
}
var report = mux.Finish();
Console.WriteLine($"Wrote {report.Tracks[0].Packets} samples.");

output.Dispose();
using var source = FileMediaSource.Open("compact.mp4");
var box = new BoxTree(source).TopLevel.Single(b => b.TypeName == "moov");
foreach (string type in new[] { "trak", "mdia", "minf", "stbl" })
    box = box.Children.Single(child => child.TypeName == type);
var sizes = box.Children.Single(b => b.TypeName is "stz2" or "stsz");
Console.WriteLine($"Sample size box: {sizes.TypeName}");
```

```text
Wrote 13 samples.
Sample size box: stz2
```

The top snippet assumes one input track at index zero. The full program maps input track indices to output indices before copying, then reopens the output and prints the size box type for the sample’s single track. Use this direct copy with an MP4-family input whose codec configuration is already suitable for the target. If a track or packet is unsuitable, `IsoMuxer` throws `MuxRefusedException`; only use an output after `Finish()` succeeds.

## Variations

For the usual `stsz` table, replace the muxer declaration in the full program with this (it leaves `CompactSampleSizes` off):

```csharp
using var mux = new IsoMuxer(output, new IsoMuxOptions());
```

## Related recipes

- [Write fragmented MP4](https://recursiveai.org/onemedia/cookbook/write-fragmented-mp4/)
- [Handle an unsupported codec when remuxing video](https://recursiveai.org/onemedia/cookbook/handle-muxer-refusal-errors/)

## The short answer

Set `IsoMuxOptions.CompactSampleSizes = true` on an `IsoMuxer` writing regular MP4. Add the tracks first, copy packets in decode order, and call `Finish()` to write the completed tables. The decision is per track: this version writes `stz2` only for variable sizes that all fit in 8 or 16 bits. Otherwise it uses `stsz` without truncating the sizes. The option is ignored for QuickTime MOV.

## What is the `stz2` box in an MP4 file?

The [MP4 Registration Authority](https://mp4ra.org/registered-types/boxes) lists `stz2` as “compact sample sizes (framing).” Both `stz2` and `stsz` tell a reader how long the individual samples are in bytes. They’re part of the sample table, not the `mdat` box that holds the audio or video bytes. With variable sizes, `stsz` uses four bytes per size. OneMedia’s compact writer chooses one byte when the largest sample in a track is at most 255 bytes, or two bytes when it’s at most 65,535 bytes. One large sample can force the wider field or rule out the compact table for that entire track.

If all samples are the same size, the [muxer uses a single common size in `stsz`](https://github.com/recursive-ai-oss/onemedia-csharp/blob/ec3528e692cf5a9c532f023a15c5b4e1f2ff76d1/src/OneMedia/IsoBmff/Mux/IsoMuxer.Layout.cs#L763-L787). There’s no array of sizes to shrink in that case. The writer emits 8- or 16-bit entries, not 4-bit entries.

## How much space does `stz2` save?

The compact table reduces the bytes spent describing samples, not the bytes that make up the audio or video. Each qualifying variable-size entry saves two or three bytes. A track with thousands of small, variable-length packets can benefit; if the encoded media dominates the file, the total size difference will be small. If you need to reduce the media payload itself, that’s an encoding decision, not a sample-table option. Leave the default off if the reader you need to support doesn’t handle compact sample sizes.
