Skip to main content

Video capture to MP4 file

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.

Products: Video Capture SDK .Net

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.

Video capture to MP4 (DirectShow) - CPU encoder or Intel QuickSync GPU encoder

Create MP4Output object for MP4 output

var mp4Output = new MP4Output();

Set MP4 settings using the settings dialog

MP4SettingsDialog mp4SettingsDialog = new MP4SettingsDialog();

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

Or

Set MP4 settings without using the settings dialog

1. Set MP4 mode

mp4Output.MP4Mode = MP4Mode.CPU_QSV;

2. 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

3. 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

Apply settings

1. Use MP4 format for output

VideoCapture1.Output_Format = mp4Output;

2. Set video capture mode

VideoCapture1.Mode = VideoCaptureMode.VideoCapture;

3. Set file name (be sure that you have to write access rights)

VideoCapture1.Output_Filename = "123.mp4";

4. Start capture

await VideoCapture1.StartAsync();

Video capture to MP4 (DirectShow) - Nvidia NVENC encoder

Create MP4Output object for MP4 output with NVENC encoder

var mp4Output = new MP4Output();

Set MP4 settings using a settings dialog

var mp4SettingsDialog = new MP4SettingsDialog();

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

Or

Set MP4 settings without using a settings dialog

1. Set MP4 mode – NVENC

mp4Output.MP4Mode = MP4Mode.NVENC;

2. 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

3. 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

Apply audio settings

1. Use the MP4 format for output

VideoCapture1.Output_Format = mp4Output;

2. Set the video capture mode

VideoCapture1.Mode = VideoCaptureMode.VideoCapture;

3. Set the file name (be sure that you have to write access rights)

VideoCapture1.Output_Filename = "123.mp4";

4. Start video capture

await VideoCapture1.StartAsync();

Video capture to MP4 (Media Foundation) - 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();

Set the MP4 settings using the settings dialog

var mp4SettingsDialog = new HWEncodersOutputSettingsDialog();

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

Or

Set the MP4 settings without using the settings dialog

1. First step – get available encoders

var filtersAvailableInfo = MFTFilterEnum.GetFiltersAvailable(out _);

2. 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

3. 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

Apply video capture settings

1. Use an MP4 format for output

VideoCapture1.Output_Format = mp4Output;

2. Set a video capture mode

VideoCapture1.Mode = VideoCaptureMode.VideoCapture;

3. Set a file name (be sure that you have to write access rights)

VideoCapture1.Output_Filename = "123.mp4";

4. Start video capture 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.