ISO Base File Format (MP4) Parser and Writer MPEG-2 TS Format Parser and Writer. Released under Apache License. Please read License.md for the license.
MediaParser relies on partial classes annotated with BoxAttribute (and derived attributes) plus a source generator to produce parsing and serialization logic. Every runtime box class derives from Box, FullBox, or ContainerBox, and is decorated with metadata describing its four-character code or UUID.
using Media.ISO;
using Media.ISO.Boxes;
[Box(BoxType.FreeBox)]
public partial class FreeBox : RawBox
{
public string Notes { get; set; } = string.Empty;
[Reserved(4)]
public int ReservedSpace { get; set; }
}[Box(BoxType.FreeBox)]ties the class to thefreeatom soBoxFactorycan instantiate it.- Each property can be annotated with helpers like
[Reserved],[FlagOptional], or[VersionDependentSize]to control how the generator reads and writes the data. - Classes must be
partialso the generator can emit.BoxContent.g.csfiles with the actualParseBoxContent,WriteBoxContent, andContentSizeimplementations.
During build, MP4Parser.SourceGenerators scans for these attributes and writes strongly typed code. The generated ParseBoxContent method automatically tracks the number of bytes remaining in the box and, for the final string property, passes that remaining length to BoxReader.ReadString() to avoid reading past the box boundary.
protected override void ParseBoxContent(BoxReader reader)
{
var __remaining = (int)Math.Max(0L, Size - HeaderSize);
ReservedSpace = reader.ReadInt32();
__remaining -= sizeof(int);
Notes = reader.ReadString(__remaining); // string is last property, so generator passes __remaining
__remaining -= (Notes?.Length ?? 0) + 1;
}BoxWriter code is generated in a similar fashion so the same class can be serialized back out.
If a particular box requires bespoke behavior, simply override ParseBoxContent(BoxReader reader) and/or WriteBoxContent(BoxWriter writer) yourself in the partial class. When these overrides exist the generator skips emitting serialization code for that type, giving you full control without fighting the tooling.
dotnet builddotnet test