Software compression by using codecs
Software compression
The video and audio codecs listed in VideoCompressors / AudioCompressors can be used for compression on the fly or after capture, according to CompressionMode. These lists include the Microsoft and GPU codecs, e.g. "H264 (Auto)", "H264 GPU (Nvidia)", "H264 GPU (Intel)", "H264 CPU (Microsoft)", "MJPEG CPU (Microsoft)", "AAC (Microsoft)", "MP3 (Microsoft)" (the list depends on the GPUs and drivers of the computer).
Video and/or audio codecs can be applied to the video stream and/or the audio stream, according to CompressionType:
- if CompressionType = ct_Video or ct_AudioVideo, a VideoCompressor must be chosen from the VideoCompressors list, which contains VideoCompressorsCount items,
- if CompressionType = ct_Audio or ct_AudioVideo, an AudioCompressor must be chosen from the AudioCompressors list, which contains AudioCompressorsCount items.
VideoCompressorName reports the name of the current VideoCompressor, and AudioCompressorName the name of the current AudioCompressor.
To select a compressor programmatically by its name (as it appears in the VideoCompressors or AudioCompressors list), use the FindIndexInListByName function, e.g.:
procedure TfrmMainForm.Button1Click(Sender: TObject);
var
i: LongInt;
begin
i:= VideoGrabber1.FindIndexInListByName (VideoGrabber1.VideoCompressors, 'H264 GPU (Nvidia)', false, true);
if i > -1 then begin // if this compressor has been found...
VideoGrabber1.VideoCompressor:= i;
end;
end;
Refreshing the compressor lists when installing/removing codecs
When the application starts, the VideoCompressors and AudioCompressors lists are populated with the codecs currently installed.
To refresh the compressor lists after installing/uninstalling codecs without exiting/restarting the application, invoke RefreshDevicesAndCompressorsLists.
General video compression properties
General video compression properties can be set with:
- VideoCompression_Quality (the most common property, taken into account by many codecs),
- VideoCompression_DataRate (in bits per second, e.g. 2000000 for 2 Mb/s),
- VideoCompression_KeyFrameRate (accepted e.g. by MPEG encoders and by the "Microsoft Video 1" codec),
- VideoCompression_PFramesPerKeyFrame (accepted e.g. by MPEG2 and MPEG4 encoders),
- VideoCompression_WindowSize.
See each property for more information.
Each of these properties is active when its value is <> -1. Depending on the video compressor, some of them may not be available.
With the Microsoft and GPU encoders of the VideoCompressors list (e.g. "H264 (Auto)", "H264 GPU (Nvidia)", "AV1 GPU (Intel)"), and with MP4 recording without video compressor (H.264 encoded by the built-in MP4 writer, see MP4 recording):
- VideoCompression_DataRate sets the mean bit rate (VBR); values below 10000 are not applied (warning reported by OnLog),
- VideoCompression_Quality selects a constant quality encoding (quality = value x 100), unless VideoCompression_DataRate is set,
- VideoCompression_KeyFrameRate sets the number of frames per key frame (0: automatic, a key frame every 2 seconds),
- the other settings (rate control VBR / CBR / constant quality, peak bit rate, buffer size, profile, low latency mode, software encoder when the GPU encoder cannot be used) are set with VideoCompression_SetInt, see TVideoCompression_int; during the recording, VideoCompression_GetInt returns the values applied by the encoder (GPU or software encoder, bit rate, key frame interval, profile, frames encoded...),
- the default values of these properties (KeyFrameRate 15, Quality 1.0) are not applied, so that the encoder keeps its own settings; to apply these values, pass them to SetVideoCompressionSettings,
- the values set by the application take precedence over the settings saved with ShowDialog (dlg_VideoCompressor); once a setting is assigned with VideoCompression_SetInt, the settings saved with ShowDialog are not used at all (until SetVideoCompressionDefaults), except those loaded with LoadCompressorSettingsFromDataString / LoadCompressorSettingsFromTextFile,
- VideoCompression_PFramesPerKeyFrame and VideoCompression_WindowSize are not used,
- MJPEG entries (e.g. "MJPEG CPU (Microsoft)"): VideoCompression_Quality sets the JPEG quality, VideoCompression_DataRate is not applied.
SetVideoCompressionSettings assigns the 5 properties at once; SetVideoCompressionDefaults sets them back to their default values and removes the VideoCompression_SetInt settings.
The bit rate of the Microsoft audio encoders of the AudioCompressors list (e.g. "AAC (Microsoft)") and of the audio of the MP4, MP3 and WMA recordings is set with AudioCompression_SetInt (ac_BitRate, in bits per second).
Determining the default values and capabilities for each video compressor
When the video compressor is bound, the OnVideoCompressionSettings event queries it and returns CanKeyFrameRate, CanPFramesPerKeyFrame, CanQuality, CanWindowSize and CanSpecifyDataRate. These parameters return "true" if the corresponding VideoCompression_... property can be specified for this video compressor.
To get the capabilities and default values of the current compressor, proceed as follows:
- Select a video capture device (set VideoDevice = index of a video capture device in the VideoDevices list).
- Identify the index of the video compressor in the VideoCompressors list and assign it to the VideoCompressor property, e.g. with VideoCompressor = VideoCompressorIndex ("compressor name").
- Set RecordingMethod = rm_AVI.
Then, either:
- invoke GetVideoCompressionSettings (), which returns the capabilities and default values of the compressor,
- or create the OnVideoCompressionSettings event (write the code to log the values returned by the event, e.g. display them in a memo, assign them to variables or add ShowMessage...), then invoke StartRecording: when the recording starts, the OnVideoCompressionSettings event occurs and returns the capabilities and default values.
Depending on the codec capabilities, the corresponding "VideoCompression_..." settings can then be set.
The VideoCompression_... settings can be assigned from the OnVideoCompressionSettings event that occurs when StartRecording is invoked.
Recompressing video clips separately
The StartReencoding function recompresses video clips separately. See How to reencode video clips for more information.
Codec dialogs
Most video compressors expose custom dialogs, which are displayed by invoking ShowDialog (dlg_VideoCompressor). TVideoGrabber automatically saves and retrieves the new codec settings.
Note
Not all the video and audio codecs can be used. When a codec cannot be used, invoking StartRecording usually fails.