Recording methods and properties
Video and audio capture devices
- If AudioRecording is disabled, only the VideoDevice video source (chosen in the VideoDevices list) is recorded to the file.
- If AudioRecording is enabled, the current VideoDevice and the current AudioDevice (chosen in the AudioDevices list) are captured.
- If the audio and video capture devices are different and the audio of the recorded video clip is not in sync with the video, enable AudioSyncAdjustmentEnabled and adjust the number of audio samples to delay with AudioSyncAdjustment.
Video format
The video format is determined by the extension of the RecordingFileName, e.g. to record an MP4 video, set .mp4 as the extension; to record a Matroska video, set .mkv as the extension.
If the file name is generated automatically, specify the video format with RecordingMethod, e.g. RecordingMethod = rm_MP4 records a ".mp4" video clip.
Recording by using the Microsoft and GPU codecs
MP4, MKV, AVI, MP3 and WMA recordings use the Microsoft and GPU codecs listed in VideoCompressors / AudioCompressors, e.g. "H264 (Auto)", "H264 GPU (Nvidia)", "H264 GPU (Intel)", "H264 CPU (Microsoft)", "MJPEG CPU (Microsoft)", "AAC (Microsoft)", "MP3 (Microsoft)". See MP4 recording, AVI and MKV recording and Audio recording to MP3 or WAV files.
Select the codec by its name, e.g.:
- set VideoCompressor = VideoCompressorIndex ("H264 GPU (Nvidia)"),
- set CompressionMode = cm_CompressOnTheFly.
Their settings are set with ShowDialog (dlg_VideoCompressor / dlg_AudioCompressor), or programmatically with VideoCompression_SetInt / AudioCompression_SetInt.
Recording by using the FFmpeg codecs
The optional Datastead Multipurpose Encoder adds the FFmpeg codecs and formats, e.g. MOV, MPEG-TS, FLV, WebM, MXF, HEVC (H.265), H.264 (OpenH264 software encoder, NVidia NVENC, Intel QuickSync, AMD AMF), AV1 (GPU encoders), VP8 / VP9, MPEG-4, MPEG-2, ProRes, DNxHD, Opus, AC-3, FLAC..., as well as the network streaming. See the complete list in using the Datastead Encoder, and Recording through the Datastead Encoder.
Starting the recording immediately
Once the settings above are applied, invoke StartRecording.
Starting the recording preview but not writing to the file immediately
Once the settings above are applied:
- set HoldRecording = true,
- invoke StartRecording: this starts the video preview and recording, but does not write to the file yet,
- later, to begin writing to the file, invoke ResumeRecording (at any time once the OnRecordingReadyToStart event has occurred; RecordingCanPause is not required).
Writing to a new file while the recording is running
To stop writing to the current file and start writing to a new file while the recording is running, invoke RecordToNewFileNow.
With the built-in MKV and MP4 writers, and with the Datastead Multipurpose Encoder, the new file is created while the capture goes on. With the other writers (e.g. AVI), the recording is stopped and started again, which leaves a short gap between the files.
Stopping the recording
Invoke Stop.
Playing a video currently being recorded
This is possible when recording in .MP4 or .MKV format, by enabling the VideoPlayableWhileRecording property.
Delayed start/stop
The RecordingTimer property makes it possible to start, stop or periodically restart the recording after an interval of time:
- RecordingTimer = rt_Disabled: normal start/stop,
- RecordingTimer = rt_RecordToNewFile: a new recording file is generated every RecordingTimerInterval seconds,
- RecordingTimer = rt_StopRecording: the recording stops after RecordingTimerInterval seconds,
- RecordingTimer = rt_StartRecording: the recording starts in preview mode only, and the real recording begins after RecordingTimerInterval seconds,
- RecordingTimer = rt_PauseRecording: the recording is paused after RecordingTimerInterval seconds,
- RecordingTimer = rt_FrameCapture: the current frame is captured every RecordingTimerInterval seconds, to the destination set by BurstType.
Creating a new video clip on the fly
While the recording is running, invoke RecordToNewFileNow (NewRecordingFileName, true): a new video clip is created. Either pass the name of the new file to create, or pass an empty string to let TVideoGrabber generate the new file name automatically, according to the AutoFileName settings.
To create small video clips having the same duration:
- set RecordingTimerInterval to the desired period (expressed in seconds),
- set RecordingTimer = rt_RecordToNewFile.
Note
This feature is not compatible with the recompression after recording (be sure to use CompressionMode = cm_NoCompression or cm_CompressOnTheFly).
Specifying a maximum file size
To limit the recording file size, specify a RecordingFileSizeMaxInMB value greater than 0. During the recording, each time the specified size is exceeded, a new file is generated on the fly, according to the auto file name settings.
Holding the beginning of the recording for an accurate recording start time
By default, when invoking StartRecording, the recording starts immediately, but building the recording graph requires a few seconds, so the moment when the recording starts after invoking StartRecording is never accurate.
To avoid this problem, set the HoldRecording property to true before invoking StartRecording (StartRecording itself takes no parameter).
When HoldRecording is enabled, the recording graph is built in advance and already previewing, so the file is ready to be written immediately; nothing is written yet, and the OnRecordingReadyToStart event occurs.
From this moment, invoke ResumeRecording to start writing to the file, at once from the event or later at the exact moment chosen (e.g. from a timer), or StopRecording to cancel. The recording stays held until one of them is invoked. RecordingCanPause is not required.
E.g.:
procedure TForm1.BtnPrepareClick (Sender: TObject);
begin
VideoGrabber1.HoldRecording := True;
VideoGrabber1.StartRecording; /// builds the graph, preview only, nothing written to the file yet
end;
procedure TForm1.VideoGrabber1RecordingReadyToStart (Sender: TObject);
begin
BtnStartWriting.Enabled := True; /// the file can now start without delay
end;
procedure TForm1.BtnStartWritingClick (Sender: TObject); /// or a timer, at the exact moment chosen
begin
VideoGrabber1.ResumeRecording; /// the file starts now
end;
procedure TForm1.BtnCancelClick (Sender: TObject);
begin
VideoGrabber1.StopRecording;
end;
videoGrabber1.HoldRecording = true;
videoGrabber1.OnRecordingReadyToStart += (sender, e) =>
{
// the graph is ready: the file can now start without delay
};
videoGrabber1.StartRecording(); // builds the graph, preview only, nothing written to the file yet
// later, at the exact moment chosen (button, timer...):
videoGrabber1.ResumeRecording(); // the file starts now
Recording file name
The recording file name can be chosen by assigning the RecordingFileName property before invoking StartRecording. If RecordingFileName is left blank, a file name is generated automatically according to the AutoFileName, AutoFileNameDateTimeFormat, AutoFileNameMinDigits, AutoFilePrefix and CaptureFileExt properties.
For example, if the file name is customized as follows:
VideoGrabber.AutoFileName = fn_DateTime;
VideoGrabber.AutoFileNameDateTimeFormat = "dd-mm-yy_hh-mm-ss";
VideoGrabber.AutoFilePrefix = "file_";
the recording file name is file_13-02-13_15-27-45.mp4.
As soon as the recording begins, this file name can be retrieved with Last_Recording_FileName. When the recording ends, this file name is returned by the OnRecordingCompleted event.
Current file size of the video clip being recorded
RecordingKBytesWrittenToDisk reports the current (growing) file size in KB while the recording is running: invoke this function periodically (e.g. every 10 seconds) to retrieve, on the fly, the current file size of the video clip being recorded. When the recording ends, invoke RecordingKBytesWrittenToDisk from the OnRecordingCompleted event to get the final file size of the recorded clip.
Sample code is available in the MainDemo project (search for "RecordingKBytesWrittenToDisk").
Pausing / resuming the AVI recording
See AVI pause/resume during recording.
Events
- If HoldRecording is enabled, when StartRecording is invoked, the OnRecordingReadyToStart event occurs once the recording graph is built, and then ResumeRecording starts the recording, at once or later.
- When the recording really begins, the OnRecordingStarted event occurs.
- When the recording ends, the OnRecordingCompleted event occurs.
Frame capture
Frames can be captured during recording when the frame grabber is enabled. See the Frame Capture chapter.
Frame overlay
Graphics or text overlays can be performed during recording when the frame grabber is enabled. See the Graphics and text overlays chapter.
Quality of the video window during recording
If the quality of the preview is not critical during recording, setting VideoRenderer = vr_RecordingPriority before invoking StartRecording is recommended, to give priority to the recording.
As the CPU load is critical during recording, preventing the video frames from being stretched to the video window size may save CPU in certain cases, especially if the video frame is resized to a size larger than its original size. To prevent the video frames from being resized:
- for the control to be resized automatically, set AdjustPixelAspectRatio = false and Display_AutoSize = true,
- to specify the size of the control, set AdjustPixelAspectRatio = false, Display_AutoSize = false and Display_AspectRatio = ar_NoResize.
Back-timed recording (preroll)
The recording can start with a preroll, a few seconds before an event occurs. E.g. when motion detection is used, this makes it possible to capture the full video sequence, by starting just before the motion detection event occurs. See the Back-timed recording (preroll) chapter.
CPU and disk requirements
To minimize dropped frames, capturing to AVI in real time requires high CPU and disk availability, therefore:
- prefer fast disks / fast CPU platforms,
- prefer NTFS partitions to FAT32 partitions.
If several video clips are recorded at the same time and a lot of dropped frames occur, recording to an NVMe SSD is recommended for better disk performance.
Dropped frames
As video capture is synchronous, frames are dropped when the time required to record a given video frame becomes longer than the time between 2 video frames. An excessive number of dropped frames causes jerky AVI playback and bad audio/video synchronization.
When dropped frame problems occur, act on the following points:
- check that StoragePath saves to a fast disk (avoid networked drives),
- disable the frame grabber on the AVI stream (set FrameGrabber = fg_PreviewStream), or disable it totally (set FrameGrabber = fg_Disabled),
- reduce the video size,
- reduce the frame rate (FrameRate property).