Recording through the Datastead Encoder
The installation and functions overview are described in the Datastead Encoder chapter.
It is possible:
-
either to record through the Datastead recording functions, allowing to use TVideoGrabber's OnRecording... events, in this case TVideoGrabber is started by invoking StartRecording()
-
either to record through an external Encoder instance (not managed by TVideoGrabber), in this case TVideoGrabber is started by invoking StartPreview(), that will send the audio/video samples to all the active Encoder instances.
SAMPLE CODE
- sample code for a recording using the TVideoGrabber recording functions:
managed by TVideoGrabber, that controls it and raises the corresponding recording events
IMPORTANT NOTE:
The TVideoGrabber recording features (RecordingFileName / CompressionMode / StartRecording / PauseRecording / ResumeRecording / RecordToNewFileNow, etc...) invoke the internal Encoder instance with ID 0.
In this case, do not invoke Encoders_CreateInstanceForRecording to pass the recording file name instead of setting RecordingFileName, this is already controlled internally by TVideoGrabber.
Only Encoder_SetInt and Encoder_SetStr should be invoked with ID 0 to configure the parameters, as shown in the MainDemo project -> "multipurpose" tab.
If preferring to configure and control the recording by configuring a recorder instance with Encoders_CreateInstanceForRecording, run TVideoGrabber in preview mode only (by invoking StartPreview), it will pass the audio/video samples to the instance having created, as described in the paragraph 2. below.
A) STARTING THE RECORDING
#define ENCODER_RECORDING_ID 0 // the encoder with ID 0 is the TVideoGrabber's embedded encoder, this will never change
- // in the MainDemo project, "video source" tab*
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("c922 Pro Stream Webcam");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceindex ("Microphone (C922 Pro Stream Webcam)");
VideoGrabber.VideoCompressor = VideoCompressorIndex ("Datastead Multipurpose Encoder");
VideoGrabber.AudioCompressor = AudioCompressorIndex ("Datastead Multipurpose Encoder");
- // in the MainDemo project, "multipurpose" tab*
VideoGrabber.Encoder_SetInt (ENCODER_RECORDING_ID, Enc_Video_Bitrate_kb, 2000);
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)); to enable the GPU encoding through NVidia (to select any GPU available set Enc_GPU_Auto)
VideoGrabber.Encoder_SetStr (ENCODER_RECORDING_ID, Enc_Audio_Codec, "aac");
- // in the MainDemo project, "recording" tab*
VideoGrabber.RecordingMethod = rm_MP4; // to generate the file name automatically,
// VideoGrabber.RecordingFileName = "VideoClip.mp4"; // if the file name is specified, it determines the recording method, no need to set RecordingMethod above
VideoGrabber.AudioRecording = true;
VideoGrabber.CompressionMode = cm_CompressOnTheFly;
VideoGrabber.StartRecording();
B) PAUSING THE RECORDING
VideoGrabber.PauseRecording();
C) RESUMING THE RECORDING
VideoGrabber.ResumeRecording();
D) RECORDING TO A NEW FILE
VideoGrabber.RecordToNewFileNow ("c:/folder/newvideoclip.mp4");
E) STOPPING THE RECORDING
VideoGrabber.Stop();
- same sample code to recording using an external encoder instance
not managed by TVideoGrabber, that just sends the audio/video samples to the encoder(s)
A) STARTING THE RECORDING
int m_EncoderInstanceID = -1;
- // in the MainDemo project, "video source" tab*
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("c922 Pro Stream Webcam");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceindex ("Microphone (C922 Pro Stream Webcam)");
m_EncoderInstanceID = VideoGrabber.Encoders_CreateInstanceForRecording ("VideoClip.mp4");
if (m_EncoderInstanceID > -1)
{
VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Video_Enabled_bool, 1);
VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Video_Bitrate_kb, 2000);
VideoGrabber.Encoder_SetStr (m_EncoderInstanceID, Enc_Video_Codec, "hevc");
VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Video_Thread_Count, 4); // default 1
// VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Video_GPU_Encoder, integer (Enc_GPU_NVidia_NVENC)); to enable the GPU encoding through NVidia (to select any GPU available set Enc_GPU_Auto)
VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Audio_Enabled_bool, 1);
VideoGrabber.Encoder_SetStr (m_EncoderInstanceID, Enc_Audio_Codec, "aac");
VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Audio_Bitrate_kb, 64);
VideoGrabber.AudioDeviceRendering = true;
VideoGrabber.StartPreview();
}
B) PAUSING THE RECORDING
VideoGrabber.Encoder_Pause (m_EncoderInstanceID);
C) RESUMING THE RECORDING
VideoGrabber.Encoder_Resume (m_EncoderInstanceID);
D) RECORDING TO A NEW FILE
bool OpenInPausedState = false; // to record to the new file immediately, otherwise the current file is closed and the recording to the next file will start when invoking Encoder_Resume
VideoGrabber.Encoder_NewOutputFile(m_EncoderInstanceID, "c:/folder/newvideoclip.mp4", OpenInPausedState);
D) STOPPING THE RECORDING ONLY
VideoGrabber.Encoder_NewOutputFile(m_EncoderInstanceID, "c:/folder/AnyNewFileName.mp4", true); // this closes the current file and prepare for the next file. If Encoder_Resume is never invoked, the next file will
E) STOPPING THE PREVIEW AND RECORDING
VideoGrabber.Stop();
F) SETTING A NEW FILE NAME BEFORE RESTARTING THE VIDEO
bool OpenInPausedState = false; // to record to the new file immediately, otherwise the current file is closed and the recording to the next file will start when invoking Encoder_Resume
VideoGrabber.Encoder_NewOutputFile(m_EncoderInstanceID, "c:/folder/anothervideoclip.mp4", OpenInPausedState);
VideoGrabber.StartPreview();