Recursive AI

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.

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.

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

C#
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):

C#
using var mux = new IsoMuxer(output, new IsoMuxOptions());

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

All onemedia recipes