# MP4 output

Media Blocks SDK .Net

MP4 is the most popular video format available for all platforms. Typically, AAC is used to encode the audio stream, and H264 or HEVC codecs are used to encode the video stream.

Use the MP4SinkSettings class to set the parameters.

# Block info

Name: MP4SinkBlock.

Pin direction Media type Pins count
Input audio audio/mpeg one or more
audio/x-ac3
audio/x-alac
audio/x-opus
Input video video/mpeg one or more
video/x-divx
video/x-h264
video/x-h265
video/x-av1
Input subtitle text/utf8 one or more

# The sample pipeline

graph LR;
    UniversalSourceBlock-->H264EncoderBlock;
    UniversalSourceBlock-->AACEncoderBlock;
    H264EncoderBlock-->MP4SinkBlock;
    AACEncoderBlock-->MP4SinkBlock;

# Sample code

var pipeline = new MediaBlocksPipeline();

var filename = "test.mp4";
var fileSource = new UniversalSourceBlock(await UniversalSourceSettings.CreateAsync(new Uri(filename)));

var aacEncoderBlock = new AACEncoderBlock();
pipeline.Connect(fileSource.AudioOutput, aacEncoderBlock.Input);

var h264EncoderBlock = new H264EncoderBlock();
pipeline.Connect(fileSource.VideoOutput, h264EncoderBlock.Input);

var mp4SinkBlock = new MP4SinkBlock(new MP4SinkSettings("output.mp4"));
pipeline.Connect(h264EncoderBlock.Output, mp4SinkBlock.CreateNewInput(MediaBlockPadMediaType.Video));
pipeline.Connect(aacEncoderBlock.Output, mp4SinkBlock.CreateNewInput(MediaBlockPadMediaType.Audio));

await pipeline.StartAsync();

To create fragmented MP4 file (with split by size or duration), use the MP4SplitSinkSettings class.

# Sample applications

# Platforms

Windows, macOS, Linux, iOS, Android.