SharpMp4 0.0.7

dotnet add package SharpMp4 --version 0.0.7                
NuGet\Install-Package SharpMp4 -Version 0.0.7                
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.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpMp4 --version 0.0.7                
#r "nuget: SharpMp4, 0.0.7"                
#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.
// Install SharpMp4 as a Cake Addin
#addin nuget:?package=SharpMp4&version=0.0.7

// Install SharpMp4 as a Cake Tool
#tool nuget:?package=SharpMp4&version=0.0.7                

SharpMP4

Simple lightweight fragmented mp4 (fmp4) reader/writer. Supports H264/H265 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 fragmented MP4.

Read fragmented MP4

To parse an existing fmp4 file, first you have to get the stream:

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

Pass the stream to the FragmentedMp4.ParseAsync and you will get an in-memory representation of the fmp4:

using (var fmp4 = await FragmentedMp4.ParseAsync(fs))
{
    ...
}

You can examine all the boxes, VPS/SPS/PPS, or parse the MDAT to get NALs and/or audio samples. You should also be able to use it on parts of the fragmented MP4, e.g. to only read the media initialization (MOOV) or media fragments (MOOF + MDAT).

Write fragmented MP4

To write FragmentedMP4 into a file, you first have to create the file and then call:

using (Stream output = new BufferedStream(new FileStream("frag_bunny_out.mp4", FileMode.Create, FileAccess.Write, FileShare.Read)))
{
    await FragmentedMp4.BuildAsync(fmp4, output);
}

This allows you to modify any boxes, add/remove tracks, modify VPS/SPS/PPS and then save the modified file.

Build fragmented MP4

While you could use just the FragmentedMp4 and build all the boxes manually, there is a helper class FragmentedMp4Builder to assist you with this task. First create the output stream, then wrap it inside one of the IMp4Output outputs (more on that later, here we just use the simplest SingleStreamOutput) and pass it to FragmentedMp4Builder:

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

Next you have to specify which tracks your fmp4 is going to have. Currently supported tracks include H264, H265, AAC and Opus. Add H264 track:

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

Add AAC track for AAC-LC 2 channels (stereo), 22050Hz and 16-bit samples:

var audioTrack = new AACTrack(2, 22050, 16);
builder.AddTrack(audioTrack);

Start adding individual NALUs to H264/H265 tracks:

await videoTrack.ProcessSampleAsync(sample);

Start adding AAC frames to AAC track:

await audioTrack.ProcessSampleAsync(frame);

The default setting of the FragmentedMp4Builder is to produce samples of 0.5 seconds and fragments with 8 samples per fragment (4 seconds of play time). Once there is enough samples in both tracks, fragments will be written to the file and you can simultaneously start playing it.

Video analysis

You can use the H264SpsNalUnit, H264PpsNalUnit, H265VpsNalUnit, H265SpsNalUnit and H265PpsNalUnitclasses standalone to just parse and modify VPS/SPS/PPS from your H264/H265 video:

byte[] sps = ...
var parsedSPS = H264SpsNalUnit.Parse(sps);

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;

Temporary Storage

By default the parser is using in-memory temporary storage, which means all parsed data are loaded in RAM. You can easily change this behavior and forward them to a temporary file:

TemporaryStorage.Factory = new TemporaryFileStorageFactory();

Or you can implement ITemporaryStorageFactory interface and create your own temporary storage.

Output

There are multiple ways to output the encoded video:

  • Single file where the output will have MOOV + MOOF + MDAT
new SingleStreamOutput(outputStream);
  • Multiple files, split into the initialization segment (MOOV) and media fragments (MOOF + MDAT)
new MultiStreamFileOutput("C:\\Temp", "output", "mp4");
  • In-memory BLOB with an event notification when a new BLOB is available
var blobOutput = new FragmentedBlobOutput()
blobOutput.OnFragmentReady += (blobEventArgs) => {
    ...
};
  • A custom output by implementing the IMp4Output interface

Custom boxes

It is possible to extend the currently supported boxes by extending Mp4Box class and providing your own implementation of the parser:

public class CustomBox : Mp4Box
{
    public const string TYPE = "cust";
    public CustomBox(uint size, string type, Mp4Box parent) : base(size, type, parent)
    { }

    public static async Task<Mp4Box> ParseAsync(uint size, string type, Mp4Box parent, Stream stream)
    {
        return new CustomBox(size, type, parent);
    }

    public static async Task<uint> BuildAsync(Mp4Box box, Stream stream)
    {
        CustomBox b = (CustomBox)box;
        return 0;
    }

    public override uint CalculateSize()
    {
        return (uint)(base.CalculateSize() + 0);
    }
}

Then you can register the custom box by calling:

SharpMp4.Mp4Parser.RegisterBox("cust", CustomBox.ParseAsync, CustomBox.BuildAsync);

Custom descriptors

Similar as boxes, you can write custom descriptors by extending the DescriptorBase class. Then you can register custom descriptors by calling:

SharpMp4.Mp4Parser.RegisterDescriptor(objectTypeIndication, type, CustomDescriptor.ParseAsync, CustomDescriptor.BuildAsync);

Future development

  • Add support for building MP4
  • Add H265 SPS extensions
  • Add H265 PPS extensions
  • AudioSpecificConfigDescriptor extensions and configuration

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 net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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.0.7 183 7/5/2024
0.0.6 218 6/12/2024
0.0.5 112 6/12/2024
0.0.4 103 6/11/2024
0.0.3 110 6/9/2024
0.0.2 112 6/2/2024
0.0.1 94 6/2/2024