SharpMP4 0.1.1

dotnet add package SharpMP4 --version 0.1.1
                    
NuGet\Install-Package SharpMP4 -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpMP4" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpMP4" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="SharpMP4" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SharpMP4 --version 0.1.1
                    
#r "nuget: SharpMP4, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SharpMP4@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SharpMP4&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=SharpMP4&version=0.1.1
                    
Install as a Cake Tool

SharpMP4

Simple lightweight mp4/fmp4/mov/m4v reader/writer. Supports H264/H265/H266/AV1 for video and AAC/Opus for audio. No platform dependencies, easily portable cross-platform. It was designed to be a stream-in and stream-out solution for recording streams from IP cameras into MP4 and fragmented MP4.

Supported boxes

The list of all supported boxes, entries and descriptors is here.

Read MP4

To parse an existing mp4/mov/m4v file, first you have to get the stream:

using (Stream inputFileStream = new BufferedStream(new FileStream("frag_bunny.mp4", FileMode.Open, FileAccess.Read, FileShare.Read)))
{
    ...
}

Create a new Container and call Read to get the in-memory representation of all the boxes.

var mp4 = new Container();
mp4.Read(new IsoStream(inputFileStream));    ...

To process this in-memory representation and read the audio/video samples, create a new VideoReader:

VideoReader videoReader = new VideoReader();
videoReader.Parse(mp4);

Now it is possible to get all the tracks from the video:

IEnumerable<ITrack> tracks = videoReader.GetTracks();

To read the first track samples, call:

uint trackID = tracks.First().TrackID;
MediaSample sample = videoReader.ReadSample(trackID);

Where trackID is the ID of the track you want to read.

Build MP4

To write MP4 into a file, you first have to create the file:

using (Stream output = new BufferedStream(new FileStream("bunny_out.mp4", FileMode.Create, FileAccess.Write, FileShare.Read)))
{
    ...
}

Next, create the builder depending upon the output format. Currently, there are two builders - FragmentedMp4Builder and Mp4Builder.

IMp4Builder outputBuilder = new Mp4Builder(new SingleStreamOutput(output));

For fragmented MP4 output, use FragmentedMp4Builder with the fragment duration in miliseconds:

// use fragment duration 2 seconds
IMp4Builder outputBuilder = new FragmentedMp4Builder(new SingleStreamOutput(output), 2000);

Add the H264 video track to the builder instance:

var videoTrack = new H264Track();
outputBuilder.AddTrack(videoTrack);

Add the AAC audio track to the builder instance:

var audioTrack = new AACTrack(2, 44100, 16);
outputBuilder.AddTrack(audioTrack);

Pass the track samples to the builder as follows:

byte[] nalu = ...;
outputBuilder.ProcessTrackSample(videoTrack.TrackID, nalu);
...
byte[] aac = ...;
outputBuilder.ProcessTrackSample(audioTrack.TrackID, aac);

When done, call FinalizeMedia to create the video file:

outputBuilder.FinalizeMedia();

Extensibility

Logging

There is a Log class where you can supply your own delegates for all the actions like:

Log.SinkWarn = (message, exception) => 
{
    ...
};

You can also enable/disable different trace levels like:

Log.WarnEnabled = false;

Credits

Huge inspiration for this project was the mp4parser https://github.com/sannies/mp4parser, thank you very much!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.1 42 8/31/2025
0.1.0 66 8/30/2025
0.0.7 2,172 7/5/2024
0.0.6 244 6/12/2024
0.0.5 141 6/12/2024
0.0.4 136 6/11/2024
0.0.3 138 6/9/2024
0.0.2 142 6/2/2024
0.0.1 131 6/2/2024