Bookmark and Share

FFmpeg on Windows
Video Capture from Webcam
- 2020

ffmpeg_logo.png






FFmpeg Webcam Video Capture

FFmpeg can take input from Directshow devices on our windows computer. So, we're going to use the dshow FFmpeg input source.

We can check what devices are available on our machine using the following command:

ffmpeg -list_devices true -f dshow -i dummy

Then, it will display the list:

C:\TEST>ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version N-56827-g64327aa Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct  2 2013 18:07:56 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avi
....
  libavutil      52. 46.100 / 52. 46.100
  libavcodec     55. 33.101 / 55. 33.101
  libavformat    55. 19.100 / 55. 19.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 88.100 /  3. 88.100
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
[dshow @ 0000000003ba21c0] DirectShow video devices
[dshow @ 0000000003ba21c0]  "HP Truevision HD"
[dshow @ 0000000003ba21c0] DirectShow audio devices
[dshow @ 0000000003ba21c0]  "Microphone (Realtek High Definition Audio)"
[dshow @ 0000000003ba21c0]  "Stereo Mix (Realtek High Definition Audio)"
dummy: Immediate exit requested

We may also need to give our devices certain parameters such as size or frame rate etc. Sometimes the frames may be dropped because ffmpeg wants to keep up the the real time input. We can control by give the proper parameters. So, we need to get more information about the device by using this command:

ffmpeg -f dshow -list_options true -i video="HP Truevision HD"

Note that I used the device name I got from earlier query. Also, note the video format setting parameter -f comes before of -i to indicate it's an input.

Now, we get from the command:

C:\TEST>ffmpeg -f dshow -list_options true -i video="HP Truevision HD"
ffmpeg version N-56827-g64327aa Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct  2 2013 18:07:56 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-

...

[dshow @ 000000000034cbc0] DirectShow video device options
[dshow @ 000000000034cbc0]  Pin "Capture"
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=640x480 fps=30 max s=640x480 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=640x480 fps=30 max s=640x480 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=160x120 fps=30 max s=160x120 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=160x120 fps=30 max s=160x120 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=320x240 fps=30 max s=320x240 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=320x240 fps=30 max s=320x240 fps=30
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=1280x720 fps=10 max s=1280x720 fps=10
[dshow @ 000000000034cbc0]   pixel_format=yuyv422  min s=1280x720 fps=10 max s=1280x720 fps=10
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=640x480 fps=30 max s=640x480 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=640x480 fps=30 max s=640x480 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=160x120 fps=30 max s=160x120 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=160x120 fps=30 max s=160x120 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=320x240 fps=30 max s=320x240 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=320x240 fps=30 max s=320x240 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=1280x720 fps=30 max s=1280x720 fps=30
[dshow @ 000000000034cbc0]   vcodec=mjpeg  min s=1280x720 fps=30 max s=1280x720 fps=30
video=HP Truevision HD: Immediate exit requested

Now, we're ready to receive video stream from our webcam.

Let's do it using the command below;

ffmpeg -f dshow -s 320x240 -r 30 -vcodec mjpeg -i video="HP Truevision HD" output.mp4

Now' it's doing it:

C:\TEST>ffmpeg -f dshow -s 320x240 -r 30 -vcodec mjpeg -i video="HP Truevision HD" output.mp4
ffmpeg version N-56827-g64327aa Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct  2 2013 18:07:56 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzl
...
Input #0, dshow, from 'video=HP Truevision HD':
  Duration: N/A, start: 247157.385000, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj422p(pc), 320x240, 30 tbr, 10000k tbn, 30 tbc
No pixel format specified, yuvj422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 000000000035b300] using cpu capabilities: MMX2 SSE2Slow SSSE3 LZCNT SlowPalignr
[libx264 @ 000000000035b300] profile High 4:2:2, level 1.3, 4:2:2 8-bit
[libx264 @ 000000000035b300] 264 - core 138 r2358 9e941d1 - H.264/MPEG-4 AVC codec - Copyleft 2003-
 cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me
zone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 dec
tra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 k
ad=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf55.19.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuvj422p, 320x240, q=-1--1, 15360
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg -> libx264)
Press [q] to stop, [?] for help
frame=  537 fps= 26 q=-1.0 Lsize=     575kB time=00:00:17.83 bitrate= 264.0kbits/s
video:568kB audio:0kB subtitle:0 global headers:0kB muxing overhead 1.247320%
[libx264 @ 000000000035b300] frame I:3     Avg QP:23.06  size: 10437
[libx264 @ 000000000035b300] frame P:154   Avg QP:24.92  size:  2370
[libx264 @ 000000000035b300] frame B:380   Avg QP:26.59  size:   485
[libx264 @ 000000000035b300] consecutive B-frames:  0.6% 10.4% 14.5% 74.5%
[libx264 @ 000000000035b300] mb I  I16..4: 21.8% 64.7% 13.6%
[libx264 @ 000000000035b300] mb P  I16..4:  0.5%  0.1%  0.0%  P16..4: 66.0% 13.8% 13.6%  0.0%  0.0%
[libx264 @ 000000000035b300] mb B  I16..4:  0.1%  0.1%  0.0%  B16..8: 30.4%  0.1%  0.0%  direct: 6.
[libx264 @ 000000000035b300] 8x8 transform intra:53.9% inter:71.7%
[libx264 @ 000000000035b300] coded y,uvDC,uvAC intra: 44.5% 99.6% 95.0% inter: 4.0% 45.7% 21.1%
[libx264 @ 000000000035b300] i16 v,h,dc,p: 22% 10% 11% 58%
[libx264 @ 000000000035b300] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 16% 33%  3%  7%  6%  7%  4%  7%
[libx264 @ 000000000035b300] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 23% 12%  6%  5%  6%  7%  5%  6%
[libx264 @ 000000000035b300] i8c dc,h,v,p: 57% 14% 13% 16%
[libx264 @ 000000000035b300] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 000000000035b300] ref P L0: 37.1%  7.3% 40.1% 15.4%
[libx264 @ 000000000035b300] ref B L0: 69.6% 25.1%  5.3%
[libx264 @ 000000000035b300] ref B L1: 87.5% 12.5%
[libx264 @ 000000000035b300] kb/s:259.41
C:\TEST>

That's it.

We can take the output.mp4, and play it.





References



Qt FFmpeg dialog


FFmpeg converter

The picture is converting mp4 to webm using ffmpeg.

FFmpeg_Conversion_To_Webm.png




HTML5 Qt Video Player

The video recording below is the player built using HTML5 video tag with QWebView as its canvas. Internally, we have two containers: mp4 and ogv, and both are converted from recorded avi. The very converter of this tutorial was used for the conversion.




Tutorial for the code: