Record/Stream/Merge/Transcode through the Datastead Multipurpose Encoder
The TVideoGrabber SDK supports natively our optional Multipurpose DirectShow Encoder filter, if installed.
This filter is able to record and stream in many formats by using ffmpeg.exe, the well-known command-line encoder.
A 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 like VideoCompressor, AudioCompressor, etc... are meaningless and ignored. All the compression settings are specified through the FFmpeg command line, like in the examples below.
-
this part of the documentation just explains the way to use this filter from TVideoGrabber, please look at the filter's PDF documentation included in the filter package for more explanations about the possible command-line syntaxes and reserved keywords.
-
excepted -i %PIPE% and the filter's reserved keywords like SHOWCONSOLE, the syntax is the normal FFmpeg's command-line syntax.
-
it is recommend to specify input the frame rate, like in the Logitech recording example below ("... -r 30 -i %PIPE%...")
-
the preview and recording are realtime processings, so in these cases the fastest FFmpeg codec's settings should be privileged, to prevent dropping frames in the case the time to process 1 frame would become longer than the time between 2 frames.
MERGING IDENTICAL FILES:
The merging is ultra-fast as it does not require to decode and reencode the video streams.
(specify the real file names instead of the bold values)
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:/YourOutputFolderYourOutputFile.mp4";
string CommandLine = "FFMpegLGPL.exe -i %MERGE_PLAYLIST% -codec copy " + YourOutputFile;
VideoGrabber.EnableMultipurposeEncoder (mpe_Reencoding, true);
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Reencoding, CommandLine);
VideoGrabber.StartReencoding();
At this point the merging 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 we merge the file and reencode them in H264 through the Intel Quicksync (h264_qsv) encoder:
(specify the real file names instead of the bold values)
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:/YourOutputFolderYourOutputFile.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();
At this point the merging runs in the background, connect the OnReencodingCompleted event to be notified of the end of the process.
RECORDING:
The Multipurpose recording is activated very simply as follows (this "movflags" option lets 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);
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 specified, the FFmpeg console is displayed for debugging purpose, otherwise the FFmpeg process runs in the background a non-visible way.
%PIPE% is mandatory, it represents the stream coming out from TVideoGrabber and passed to FFmpeg, it must be written as is (unless needed to make a "classic" FFmpeg transcoding explained later 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.AudioRendering = 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.AudioRendering = true;
VideoGrabber.NetworkStreamingType = nst_AudioVideoStreaming;
VideoGrabber.EnableMultipurposeEncoder(mpe_Recording, true);
CommandLine = "ffmpegLGPL.exe -report -i %PIPE% -an -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);
Streaming using the build-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);
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 by invoking a GPL build of ffmpeg.exe:
(The ffmpegLGPL.exe included with the filter does not include libx264. Pre-compiled GPL builds of FFmpeg.exe can be downloaded here )
(use backslashes for the ffmpeg folder, not slashes, our help compiler does not support the backslashes)
VideoGrabber.VideoSource = vs_VideoCaptureDevice;
VideoGrabber.VideoDevice = VideoGrabber.VideoDeviceIndex ("Logitech HD Pro Webcam C920");
VideoGrabber.AudioDevice = VideoGrabber.AudioDeviceIndex ("Microphone (HD Pro Webcam C920)");
VideoGrabber.AudioRendering = true;
VideoGrabber.NetworkStreamingType = nst_AudioVideoStreaming;
VideoGrabber.EnableMultipurposeEncoder (mpe_Streaming, true);
CommandLine = "c:/MyFFmpegGPL/FFmpeg.exe -i %PIPE% -acodec aac -ab 64k -ac 2 -vcodec libx264 -g 90 -b:v 768k -f flv rtmp://[wowza-address]:1935/live?doPublish=12345/streamname";
VideoGrabber.SetMultipurposeEncoderSettings (mpe_Streaming, CommandLine);
VideoGrabber.StartPreview();
TRANSCODING
In this case the code is direct, do not specify %PIPE%, but instead directly 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();