Skip to content

Record/Stream/Merge/Transcode through the Datastead Multipurpose Encoder

The TVideoGrabber SDK natively supports the optional Multipurpose DirectShow Encoder filter, if installed.

This filter records and streams in many formats by using ffmpeg.exe, the well-known command-line encoder.

An LGPL build of FFmpeg, named ffmpegLGPL.exe, is included with the filter.

Important notes:

  • When recording or streaming with this filter, no DirectShow compressor is required, as FFmpeg includes its own compression codecs. Therefore all the TVideoGrabber codec settings such as VideoCompressor, AudioCompressor, etc. are meaningless and ignored. All the compression settings are specified through the FFmpeg command line, as in the examples below.
  • This part of the documentation only explains how to use this filter from TVideoGrabber: see the PDF documentation of the filter, included in the filter package, for more explanations about the possible command-line syntaxes and reserved keywords.
  • Except for -i %PIPE% and the reserved keywords of the filter such as SHOWCONSOLE, the syntax is the normal FFmpeg command-line syntax.
  • Specifying the input frame rate is recommended, as in the Logitech recording example below ("... -r 30 -i %PIPE%...").
  • The preview and recording are real-time processes, so in these cases the fastest FFmpeg codec settings should be preferred, to prevent dropping frames when the time to process 1 frame becomes longer than the time between 2 frames.

The FFmpeg command-line mode is enabled with EnableMultipurposeEncoder for the recording, the network streaming or the reencoding (mpe_Recording, mpe_Streaming, mpe_Reencoding, see TMultipurposeEncoderInstance). The command line is set with SetMultipurposeEncoderSettings (and returned by GetMultipurposeEncoderSettings) before invoking StartRecording, StartPreview or StartReencoding.

Without a command line, the Datastead Multipurpose Encoder can also be configured with Encoder_SetInt / Encoder_SetStr (encoder mode), see using the Datastead Encoder.

Merging identical files

The merging is ultra-fast, as it does not require decoding and reencoding the video streams.

(Specify the real file names instead of the example ones.)

VideoGrabber.Playlist (pl_Clear, "");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip1.mp4");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip2.mp4");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip3.mp4");
// VideoGrabber.Playlist (pl_Add, ...); add more files if needed
VideoGrabber.Reencoding_SourceVideoClip = "PLAYLIST";
string YourOutputFile = "c:\\YourOutputFolder\\YourOutputFile.mp4";
string CommandLine = "FFMpegLGPL.exe -i %MERGE_PLAYLIST% -codec copy " + YourOutputFile;
VideoGrabber.EnableMultipurposeEncoder (mpe_Reencoding, true);
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Reencoding, CommandLine);
VideoGrabber.StartReencoding();

The merging then runs in the background: connect the OnReencodingCompleted event to be notified of the end of the process.

Merging files encoded in different formats

In this case the streams must be decoded and then reencoded.

In the example below, the files are merged and reencoded in H264 through the Intel Quicksync (h264_qsv) encoder.

(Specify the real file names instead of the example ones.)

VideoGrabber.Playlist (pl_Clear, "");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip1.mp4");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip2.mp4");
VideoGrabber.Playlist (pl_Add, "c:\\YourFolder\\YourVideoClip3.mp4");
// VideoGrabber.Playlist (pl_Add, ...); add more files if needed
VideoGrabber.Reencoding_SourceVideoClip = "PLAYLIST";
string YourOutputFile = "c:\\YourOutputFolder\\YourOutputFile.mp4";
string CommandLine = "FFMpegLGPL.exe -i %MERGE_PLAYLIST% -vcodec h264_qsv -profile:v main -level 4.1 -b:v 3000k -r 25 " + YourOutputFile;
VideoGrabber.EnableMultipurposeEncoder (mpe_Reencoding, true);
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Reencoding, CommandLine);
VideoGrabber.StartReencoding();

The merging then runs in the background: connect the OnReencodingCompleted event to be notified of the end of the process.

Recording

The Multipurpose recording is activated as follows (the "movflags" option makes it possible to play and seek the MP4 clip while it is being recorded):

VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (HD Pro Webcam C920)");
VideoGrabber.AudioRecording = true;
VideoGrabber.EnableMultipurposeEncoder(mpe_Recording, true);
string CommandLine = "SHOWCONSOLE ffmpegLGPL.exe -r 30 -i %PIPE%";
CommandLine = CommandLine + " -acodec aac -ab 128k -ac 2 -strict experimental";
CommandLine = CommandLine + " -vcodec mpeg4 -b:v 1000k -minrate 1000k";
CommandLine = CommandLine + " -movflags frag_keyframe+empty_moov -f mp4 WebcamClip.mp4";
VideoGrabber.SetMultipurposeEncoderSettings(mpe_Recording, CommandLine);
VideoGrabber.StartRecording();

SHOWCONSOLE is a reserved keyword that can be removed. If it is specified, the FFmpeg console is displayed for debugging purposes; otherwise the FFmpeg process runs invisibly in the background.

%PIPE% is mandatory: it represents the stream coming out of TVideoGrabber and passed to FFmpeg, and it must be written as is (except for a "classic" FFmpeg transcoding, as explained below).

Streaming

UDP streaming

Invoke MultipurposeEncoder_QuickConfigure_UDPStreaming_H264, e.g.:

VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (HD Pro Webcam C920)");
VideoGrabber.AudioDeviceRendering = true;
if (VideoGrabber.MultipurposeEncoder_QuickConfigure_UDPStreaming_H264 (false, true, true, "127.0.0.1", 10124, 2000, 128)) {
    VideoGrabber.StartPreview();
}

RTMP push to a media server

E.g.:

VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (HD Pro Webcam C920)");
VideoGrabber.AudioDeviceRendering = true;
VideoGrabber.NetworkStreamingType = nst_AudioVideoStreaming;
VideoGrabber.EnableMultipurposeEncoder(mpe_Streaming, true);
string CommandLine = "ffmpegLGPL.exe -report -i %PIPE% -vcodec h264 -b:v 1000k -minrate 1000k -acodec aac -ab 128k -ac 2 -f flv rtmp://192.168.1.199/LiveApp/722968383194925013449884";
VideoGrabber.SetMultipurposeEncoderSettings(mpe_Streaming, CommandLine);
VideoGrabber.StartPreview();

Streaming using the built-in RTSP server

VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.NetworkStreamingType = nst_VideoStreaming;
VideoGrabber.EnableMultipurposeEncoder (mpe_Streaming, true);
string CommandLine = "SHOWCONSOLE RTSPPORT=12800 UDPPORT=12801 RTSPSESSION=camera1 ffmpegLGPL.exe -re -fflags nobuffer -flags low_delay -r 30 -vsync 1 -i %PIPE% -vcodec h264 -b:v 1000k -minrate 1000k -an -f mpegts udp://127.0.0.1:12801?pkt_size=188&buffer_size=2048";
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Streaming, CommandLine);
VideoGrabber.StartPreview();

The client can connect to this server with:

rtsp://[ipaddress]:12800/camera1>rtsp_transport=udp

Pushing to a Wowza server through the Datastead Encoder (no command line)

VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (HD Pro Webcam C920)");
VideoGrabber.AudioDeviceRendering = true;
int StreamingID = VideoGrabber.Encoders_CreateInstanceForStreaming ("rtmp://[wowza-address]:1935/live?doPublish=12345/streamname");
if (StreamingID > -1) {
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_Video_Enabled_bool, 1);
    VideoGrabber.Encoder_SetStr (StreamingID, Enc_Video_Codec, "h264");
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_Video_GPU_Encoder, (int) Enc_GPU_Auto); // GPU encoder if available, otherwise software encoder
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_Video_BitRate_kb, 768);
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_Audio_Enabled_bool, 1);
    VideoGrabber.Encoder_SetStr (StreamingID, Enc_Audio_Codec, "aac");
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_Audio_BitRate_kb, 64);
    VideoGrabber.Encoder_SetInt (StreamingID, Enc_IsActive_bool, 1); // required, otherwise the instance is not used
    VideoGrabber.StartPreview();
}

See Streaming through the Datastead Encoder.

Transcoding

In this case, do not specify %PIPE%; instead, directly specify the path/name of the clip to transcode, and invoke StartReencoding:

string FFmpegCommandLine = "SHOWCONSOLE ffmpegLGPL.exe -i ClipToReencode.avi -f mp4 ClipReencodedToMP4.mp4";
VideoGrabber.EnableMultipurposeEncoder (mpe_Reencoding, true);
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Reencoding, FFmpegCommandLine);
VideoGrabber.StartReencoding();