Webcam Stream Capture with Preview

Useful and helpful Windows specific command line samples and examples.
Post Reply
sambul92
Posts: 11
Joined: Tue Mar 31, 2015 4:39 am

Webcam Stream Capture with Preview

Post by sambul92 » Fri Apr 10, 2015 12:46 am

I wanted to compare effectiveness of a few known FFMpeg methods to capture a webcam stream to file and preview it simultaneously, using Logitech C910 USB2.0 webcam as an example with its video & audio module names taken from Windows Device Manager. Given below dshow stream handling CLI working examples and comparison notes can hopefully help webcam users.

List dshow devices

Code: Select all

ffmpeg -list_devices true -f dshow -i dummy
List device options

Code: Select all

ffmpeg -f dshow -list_options true -i video="Logitech HD Pro Webcam C910"
ffmpeg -f dshow -list_options true -i audio="Microphone (HD Pro Webcam C910)"
List device default outputs

Code: Select all

ffmpeg -f dshow -i video="Logitech HD Pro Webcam C910"
ffmpeg -f dshow -i audio="Microphone (HD Pro Webcam C910)"
Webcam compressed HD720P stream T-split copy | pipe to FFPlay (using SDL lib) works OK, but with 1sec delay in Preview, and 15% CPU load by FFPlay on a 4-core PC. Any way to decrease delay and decoding CPU load?

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -c:v mjpeg -c:a pcm_s16le -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -flags +global_header -c copy -copytb 1 -f tee -map 0:v -map 0:a "L:\\Video\\C910.mkv|[f=matroska]udp://192.168.3.35:1234/" | ffplay -f matroska udp://192.168.3.35:1234/
In this example we can save without transcoding muxed by FFMPeg video & audio webcam source streams to a compatible container format, while previewing only video decoded by FFPlay, with no audio to eliminate delay due to buffering & muxing. The same source stream is directed to several outputs.

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -c:v mjpeg -c:a pcm_s16le -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -c copy -y L:\Video\C910.mkv -c:v copy -copytb 1 -an -f mjpeg udp://192.168.3.35:1234/ | ffplay -f mjpeg -probesize 32 udp://192.168.3.35:1234
Webcam hardware compressed HD720P MJPEG stream copy and SDL preview: initial delay 2-3 sec, later no video delay, 23% CPU, non-fatal error "[sdl @ 042e6800] Application provided invalid, non monotonically increasing dts to muxer in stream" (fixable by adding -copytb 1). Any way to optimize MJPEG decoding? Re the error, I found 2 similar FFMpeg bug reports filed #4433 and 4201.

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -c:v mjpeg -c:a pcm_s16le -channels 2 -sample_rate 41100 -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -c copy L:\Video\C910.mkv -pix_fmt yuv420p -an -f sdl -window_size 640x360 "C910 Preview"
Webcam raw HD720P stream compression to H254 & AAC and SDL preview: some delay, 80% CPU load. Ongoing encoder warning "Past duration x.xxxxxx too large". Any way to address or stop printing this warning? Is it this patch related?

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -pix_fmt yuv420p -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -c:a libvo_aacenc L:\Video\C910.mkv -c:v copy -an -f sdl "C910 Preview"

Even simple preview of webcam raw stream with sdl gives 15% CPU load with no delay, and with colorspace conversion required for MJPEG source its 20%. It looks like rawvideo decoder is inefficient and needs optimization for dshow streams. Or possibly some extra options can help?

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -pix_fmt yuv420p -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -c:v copy -an -f sdl "C910 Preview"
How one can listen to webcam audio with SDL preview? I tried to output source audio to -f sndio but nothing comes off the speakers. I think adding sndio output support to FFMpeg Win builds should solve it, as it really essential for Dshow webcams. Or is there any other way to output sound to SDL? Also, FFMpeg docs don't show any options for sndio device, so presumably it outputs to Windows Default Playback device?
Last edited by sambul92 on Thu Apr 07, 2016 8:22 pm, edited 12 times in total.

sambul92
Posts: 11
Joined: Tue Mar 31, 2015 4:39 am

Re: Webcam Stream Capture with Preview

Post by sambul92 » Fri Apr 10, 2015 10:14 pm

To cut on CPU load, I copied latest mplayer.exe from MPlayer package to FFMpeg bin directory. CPU usage dropped to 6% at copy to file + no delay preview. But Mplayer also seems to use FFMpeg (rawvideo) to decode the stream, so why lower CPU load here? Mplayer default colorspace conversion results in darker preview. To change mplayer windows size, add -zoom -x 640 -y 360 -geometry 50%:50% in its options:

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -c:v mjpeg -c:a pcm_s16le -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -c copy -y L:\Video\C910.mkv -flags +global_header -c copy -copytb 1 -async 1 -f matroska tcp://192.168.3.35:1234/ | mplayer -benchmark ffmpeg://tcp://192.168.3.35:1234?listen
If you have VLC player installed, similar stream capture to file + preview can be run with VLC at some delay and 10% CPU load, probably spent by VLC for colorspace conversion:

Code: Select all

ffmpeg -s 1280x720 -framerate 30 -c:v mjpeg -c:a pcm_s16le -rtbufsize 100MB -f dshow -i video="Logitech HD Pro Webcam C910":audio="Microphone (HD Pro Webcam C910)" -map 0:v -map 0:a -flags +global_header -c copy -copytb 1 -f tee "L:\\Video\\C910.mkv|[f=matroska]udp://192.168.3.35:1234/"|"C:\Program Files (x86)\VideoLAN\VLC\vlc" --network-caching=10 udp://@192.168.3.35:1234
What's interesting, VirtualDub shows no delay in "video only" Preview window at recording same stream using DirectShow SampleGrabber filter to split: decode & render to screen, and record to file, giving very low 5% CPU at copy recording + preview, or near 0% CPU at preview only. If ffmpeg buffersink filter is similar to SampleGrabber, is there a way of using it for no delay -c copy recording and preview with low CPU load similar to VirtualDub? Can someone suggest CML for that, since VirtualDub doesn't support DirectShow stream capture to MP4?

The above examples are given for Logitech C910 webcam that supports native MJPEG & PCM compressed stream output. Similar approach should work for newer C920 webcam with FullHD 1080P native webcam compressed H264 & AAC stream output, except -copyinkf option may be needed to capture H264 frames and save to file and/or stream to web without transcoding:

Code: Select all

ffmpeg -s 1920x1080 -rtbufsize 100MB -f dshow -vcodec h264 -acodec aac -i video="Logitech HD Pro Webcam C920":audio="Microphone (HD Pro Webcam C920)" -copyinkf -c copy L:\Video\C920.mp4
Last edited by sambul92 on Thu Apr 23, 2015 12:05 pm, edited 2 times in total.

rogerdpack
Posts: 1872
Joined: Fri Aug 05, 2011 9:56 pm

Re: Webcam Stream Capture with Preview

Post by rogerdpack » Mon Apr 20, 2015 2:31 pm

I have never gotten ffplay to have awesome latency...
mplayer -benchmark maybe...

rogerdpack
Posts: 1872
Joined: Fri Aug 05, 2011 9:56 pm

Re: Webcam Stream Capture with Preview

Post by rogerdpack » Tue May 12, 2015 4:30 am

I wonder if SDL is using a different colorspace thus forcing a conversion? Hmm this is a tricky one...certainly FFmpeg isn't optimized...your research here is about as good as I've done, I've got nothing to add :)

Post Reply