Menu Close

How to record a video source to mp4

Skip to main content
You are here:
Print

How to record a video source to mp4

TVideoGrabber records MP4 files in two ways:

  • natively, with the Microsoft and GPU codecs of Windows (H.264 video, AAC audio): nothing else to install;
  • with the Datastead Multipurpose Encoder (optional, FFmpeg codecs: H.264, HEVC, AV1…), when it is installed.

1. MP4 recording with the Microsoft / GPU codecs

The H.264 encoder is selected in the VideoCompressors list, e.g. “H264 (Auto)” (best H.264 encoder of the computer, GPU first), “H264 GPU (Nvidia)”, “H264 GPU (Intel)” or “H264 CPU (Microsoft)”. The list depends on the GPUs and drivers of the computer, so select the encoder by its name. The audio is encoded in AAC.

VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("c922 Pro Stream Webcam"); // (1)
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (C922 Pro Stream Webcam)");

VideoGrabber.VideoCompressor = VideoGrabber.VideoCompressorIndex ("H264 (Auto)"); // (2)
VideoGrabber.CompressionMode = cm_CompressOnTheFly;

VideoGrabber.RecordingMethod = rm_MP4; // the file name is generated automatically (2)
// VideoGrabber.RecordingFileName = "VideoClip.mp4"; // if a file name is specified, it determines the recording method, no need to set RecordingMethod above
VideoGrabber.AudioRecording = true;
VideoGrabber.StartRecording();

In the MainDemo project:
(1) “video source” tab
(2) “recording” tab

Encoder settings (rate control, bit rate, quality, key frame interval, profile…): select the encoder as above, then invoke VideoGrabber.ShowDialog (dlg_VideoCompressor). The settings are saved for this encoder and used for the next recordings. They can also be saved and restored programmatically with SaveCompressorSettingsToDataString / LoadCompressorSettingsFromDataString.

Note: Encoder_SetInt / Encoder_SetStr do not apply to these encoders; they configure the Datastead Multipurpose Encoder only (see below).

To play the MP4 file while it is being recorded, set VideoGrabber.VideoPlayableWhileRecording = true (fragmented MP4, Windows 8 or later).

More information here.

2. MP4 recording with the Datastead Multipurpose Encoder

(this requires the Datastead Multipurpose Encoder to be installed)

The Multipurpose Encoder is selected as video and audio compressor, and configured with Encoder_SetInt / Encoder_SetStr on the encoder with ID 0:

#define ENCODER_RECORDING_ID 0 // the encoder with ID 0 is the TVideoGrabber's embedded encoder, this will never change

VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("c922 Pro Stream Webcam"); // (1)
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (C922 Pro Stream Webcam)");
VideoGrabber.VideoCompressor = VideoGrabber.VideoCompressorIndex ("Datastead Multipurpose Encoder");
VideoGrabber.AudioCompressor = VideoGrabber.AudioCompressorIndex ("Datastead Multipurpose Encoder");

VideoGrabber.Encoder_SetInt (ENCODER_RECORDING_ID, Enc_Video_Bitrate_kb, 2000); // (2)
VideoGrabber.Encoder_SetStr (ENCODER_RECORDING_ID, Enc_Video_Codec, "hevc");
VideoGrabber.Encoder_SetInt (ENCODER_RECORDING_ID, Enc_Video_Thread_Count, 4); // default 1
// VideoGrabber.Encoder_SetInt (ENCODER_RECORDING_ID, Enc_Video_GPU_Encoder, integer (Enc_GPU_NVidia_NVENC)); // GPU encoding through NVidia (Enc_GPU_Auto: any GPU available)
VideoGrabber.Encoder_SetStr (ENCODER_RECORDING_ID, Enc_Audio_Codec, "aac");

VideoGrabber.RecordingMethod = rm_MP4; // the file name is generated automatically (3)
// VideoGrabber.RecordingFileName = "VideoClip.mp4"; // if a file name is specified, it determines the recording method, no need to set RecordingMethod above
VideoGrabber.AudioRecording = true;
VideoGrabber.CompressionMode = cm_CompressOnTheFly;
VideoGrabber.StartRecording();

In the MainDemo project:
(1) “video source” tab
(2) “multipurpose” tab
(3) “recording” tab

More information here.

Pausing, resuming, recording to a new file

These functions work the same way in both cases.

To pause and resume the recording, set VideoGrabber.RecordingCanPause = true before invoking StartRecording, then:

VideoGrabber.PauseRecording();
...
VideoGrabber.ResumeRecording();

To continue the recording in a new file:

VideoGrabber.RecordToNewFileNow ("", true); // the new file name is generated automatically in the StoragePath folder
// or
VideoGrabber.RecordToNewFileNow ("c:/folder/thenewfilename.mp4", true);

To stop the recording:

VideoGrabber.Stop();
Table of Contents