Skip to content

Recording through the Datastead Encoder

The installation and functions overview are described in the Datastead Encoder chapter.

There are two ways to record:

  1. through the TVideoGrabber recording functions, which makes it possible to use the OnRecording... events of TVideoGrabber; in this case TVideoGrabber is started by invoking StartRecording,
  2. through an external Encoder instance (not managed by TVideoGrabber); in this case TVideoGrabber is started by invoking StartPreview, which sends the audio/video samples to all the active Encoder instances.

Sample code: recording using the TVideoGrabber recording functions

The recording is managed by TVideoGrabber, which controls it and raises the corresponding recording events.

Warning

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 "multipurpose" tab of the MainDemo project.

To configure and control the recording through a recorder instance created with Encoders_CreateInstanceForRecording instead, run TVideoGrabber in preview mode only (by invoking StartPreview): it passes the audio/video samples to the instance created, as described in the second sample code below.

Starting the recording

const int 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 = VideoGrabber.VideoCompressorIndex ("Datastead Multipurpose Encoder");
VideoGrabber.AudioCompressor = VideoGrabber.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); // 0 (default): 4 threads
// VideoGrabber.Encoder_SetInt (ENCODER_RECORDING_ID, Enc_Video_GPU_Encoder, (int) 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.RecordingCanPause = true; // required by PauseRecording / ResumeRecording below
VideoGrabber.StartRecording();

Pausing the recording

VideoGrabber.PauseRecording();

Resuming the recording

VideoGrabber.ResumeRecording();

Recording to a new file

VideoGrabber.RecordToNewFileNow ("c:\\folder\\newvideoclip.mp4", true);

Stopping the recording

VideoGrabber.Stop();

Sample code: recording using an external encoder instance

The encoder instance is not managed by TVideoGrabber, which just sends the audio/video samples to the encoder(s).

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); // 0 (default): 4 threads
    // VideoGrabber.Encoder_SetInt (m_EncoderInstanceID, Enc_Video_GPU_Encoder, (int) 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.Encoder_SetInt (m_EncoderInstanceID, Enc_IsActive_bool, 1); // required, otherwise the instance is not used
    VideoGrabber.AudioDeviceRendering = true;
    VideoGrabber.StartPreview();
}

Pausing the recording

VideoGrabber.Encoder_Pause (m_EncoderInstanceID);

Resuming the recording

VideoGrabber.Encoder_Resume (m_EncoderInstanceID);

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);

Stopping the recording only

VideoGrabber.Encoder_NewOutputFile(m_EncoderInstanceID, "c:\\folder\\AnyNewFileName.mp4", true);
// this closes the current file and opens the next one in paused state: nothing is written to it until Encoder_Resume is invoked

Stopping the preview and recording

VideoGrabber.Stop();

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();