Skip to main content

MP4 file output

Products: Video Capture SDK .Net, Video Edit SDK .Net

MP4 (MPEG-4 Part 14), introduced in 2001, is a digital multimedia container format most commonly used to store video and audio. It also supports subtitles and images. MP4 is known for its high compression and compatibility across various devices and platforms, making it a popular choice for streaming and sharing.

Capturing videos from a webcam and saving them to a file is a common requirement in many applications. One way to achieve this is by using a software development kit (SDK) like VisioForge Video Capture SDK .Net, which provides an easy-to-use API for capturing and processing videos in C#. In this article, we'll explore how to capture a video from a webcam and save it to an MP4 file using the VideoCaptureCore class in VisioForge Video Capture SDK .Net.

The same sample code can be used for Video Edit SDK .Net. Use the VideoEditCore class instead of VideoCaptureCore.

Setting up the Project

The first step is to set up a new project in Visual Studio and install VisioForge Video Capture SDK .Net. You can do this by creating a new C# console application or a Windows Forms/WPF application and then installing the NuGet package for VisioForge Video Capture SDK .Net.

To capture video in MP4 format using Video Capture SDK, you need to configure video output format using one of the classes for MP4 output. You can use several available software and hardware video encoders, including Intel QuickSync, Nvidia NVENC, and AMD/ATI APU.

Use the dialog to set settings in UI or set settings in code.

Set settings using the settings dialog

var mp4Output = new MP4Output();

MP4SettingsDialog mp4SettingsDialog = new MP4SettingsDialog();

mp4SettingsDialog.ShowDialog(this);
mp4SettingsDialog.SaveSettings(ref mp4Output);

Set settings using the code

CPU encoder or Intel QuickSync GPU encoder

Create MP4Output object for MP4 output.

var mp4Output = new MP4Output();

Set MP4 mode to CPU_QSV.

mp4Output.MP4Mode = MP4Mode.CPU_QSV;

Set video settings.

mp4Output.Video.Profile = H264Profile.ProfileMain; // H264 profile
mp4Output.Video.Level = H264Level.Level4; // H264 level
mp4Output.Video.Bitrate = 2000; // bitrate

// optional parameters
mp4Output.Video.MBEncoding = H264MBEncoding.CABAC; //CABAC / CAVLC
mp4Output.Video.BitrateAuto = false; // true to use auto bitrate
mp4Output.Video.RateControl = H264RateControl.VBR; // rate control - CBR or VBR

Set AAC audio settings.

mp4Output.Audio_AAC.Bitrate = 192;
mp4Output.Audio_AAC.Version = AACVersion.MPEG4; // MPEG-4 / MPEG-2
mp4Output.Audio_AAC.Output = AACOutput.RAW; // RAW or ADTS
mp4Output.Audio_AAC.Object = AACObject.Low; // type of AAC

Nvidia NVENC encoder

Create the MP4Output object for MP4 output.

var mp4Output = new MP4Output();

Set MP4 mode to NVENC.

mp4Output.MP4Mode = MP4Mode.NVENC;

Set the video settings.

mp4Output.Video_NVENC.Profile = NVENCVideoEncoderProfile.H264_Main; // H264 profile
mp4Output.Video_NVENC.Level = NVENCEncoderLevel.H264_4; // H264 level
mp4Output.Video_NVENC.Bitrate = 2000; // bitrate

// optional parameters
mp4Output.Video_NVENC.RateControl = NVENCRateControlMode.VBR; // rate control - CBR or VBR

Set the audio settings.

mp4Output.Audio_AAC.Bitrate = 192;
mp4Output.Audio_AAC.Version = AACVersion.MPEG4; // MPEG-4 / MPEG-2
mp4Output.Audio_AAC.Output = AACOutput.RAW; // RAW or ADTS
mp4Output.Audio_AAC.Object = AACObject.Low; // type of AAC

CPU/GPU encoders

Using MP4 HW output, you can use hardware-accelerated encoders by Intel (QuickSync), Nvidia (NVENC), and AMD/ATI.

Create MP4HWOutput object for MP4 HW output.

var mp4Output = new MP4HWOutput();

Get available encoders.

var filtersAvailableInfo = MFTFilterEnum.GetFiltersAvailable(out _);

Depending on available encoders, select video codec.

mp4Output.Video.Codec = MFVideoEncoder.MS_H264; // Microsoft H264
mp4Output.Video.Profile = MFH264Profile.Main; // H264 profile
mp4Output.Video.Level = MFH264Level.Level4; // H264 level
mp4Output.Video.AvgBitrate = 2000; // bitrate

// optional parameters
mp4Output.Video.CABAC = true; // CABAC / CAVLC
mp4Output.Video.RateControl = MFCommonRateControlMode.CBR; // rate control

// many other parameters are available

Set audio settings.

mp4Output.Audio.Bitrate = 192;
mp4Output.Audio.Version = AACVersion.MPEG4; // MPEG-4 / MPEG-2
mp4Output.Audio.Output = AACOutput.RAW; // RAW or ADTS
mp4Output.Audio.Object = AACObject.Low; // type of AAC

Now, we can apply MP4 output settings to the core class (VideoCaptureCore or VideoEditCore) and start video capture or editing.

Apply video capture settings

Set MP4 format settings for output.

core.Output_Format = mp4Output;

Set a video capture mode (or video convert mode if you use Video Edit SDK).

core.Mode = VideoCaptureMode.VideoCapture;

Set a file name (ensure you have to write access rights).

core.Output_Filename = "output.mp4";

Start video capture (convert) to a file.

await VideoCapture1.StartAsync();

Finally, when we're done capturing the video, we need to stop the video capture and release the resources. We can do this by calling the StopAsync method of the VideoCaptureCore class.

Required redists

Please check Deployment


Visit our GitHub page to get more code samples.