Video capture from USB camera
This article describes various modes for capturing video from an USB camera.
1. Video capture using FFmpeg and Video4Linux
1.1 sudo apt install ffmpeg
[setupvars.sh] OpenVINO environment initialized
strong@foreverstrong:~$ ffmpeg
The program 'ffmpeg' is currently not installed. You can install it by typing:
sudo apt install ffmpeg
strong@foreverstrong:~$
strong@foreverstrong:~$ sudo apt install ffmpeg
[sudo] password for strong:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
libllvm4.0
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
libavdevice-ffmpeg56
Suggested packages:
ffmpeg-doc
The following NEW packages will be installed:
ffmpeg libavdevice-ffmpeg56
0 upgraded, 2 newly installed, 0 to remove and 180 not upgraded.
Need to get 1,360 kB of archives.
After this operation, 2,149 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 libavdevice-ffmpeg56 amd64 7:2.8.15-0ubuntu0.16.04.1 [70.7 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 ffmpeg amd64 7:2.8.15-0ubuntu0.16.04.1 [1,289 kB]
Fetched 1,360 kB in 10s (135 kB/s)
Selecting previously unselected package libavdevice-ffmpeg56:amd64.
(Reading database ... 258275 files and directories currently installed.)
Preparing to unpack .../libavdevice-ffmpeg56_7%3a2.8.15-0ubuntu0.16.04.1_amd64.deb ...
Unpacking libavdevice-ffmpeg56:amd64 (7:2.8.15-0ubuntu0.16.04.1) ...
Selecting previously unselected package ffmpeg.
Preparing to unpack .../ffmpeg_7%3a2.8.15-0ubuntu0.16.04.1_amd64.deb ...
Unpacking ffmpeg (7:2.8.15-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
/sbin/ldconfig.real: /opt/intel/common/mdf/lib64/igfxcmrt64.so is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-x11.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-drm.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libmfxhw64.so.1 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libmfx.so.1 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-glx.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libigdgmm.so.1 is not a symbolic link
Processing triggers for man-db (2.7.5-1) ...
Setting up libavdevice-ffmpeg56:amd64 (7:2.8.15-0ubuntu0.16.04.1) ...
Setting up ffmpeg (7:2.8.15-0ubuntu0.16.04.1) ...############################................................................]
Processing triggers for libc-bin (2.23-0ubuntu10) ...
/sbin/ldconfig.real: /opt/intel/common/mdf/lib64/igfxcmrt64.so is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-x11.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-drm.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libmfxhw64.so.1 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libmfx.so.1 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libva-glx.so.2 is not a symbolic link
/sbin/ldconfig.real: /opt/intel/mediasdk/lib64/libigdgmm.so.1 is not a symbolic link
strong@foreverstrong:~$
1.2 Performing the capture
The video stream from the USB camera could be captured and saved to the output_video.avi file using the following command:
ffmpeg -f video4linux2 -r 30 -s 640x480 -i /dev/video0 output_video.avi
assuming that /dev/video0
is the webcam device file.
[setupvars.sh] OpenVINO environment initialized
strong@foreverstrong:~/ffmpeg$ ffmpeg -f video4linux2 -r 30 -s 640x480 -i /dev/video0 output_video.avi
ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 5386.626378, bitrate: 147456 kb/s
Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Output #0, avi, to 'output_video.avi':
Metadata:
ISFT : Lavf56.40.101
Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 640x480, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc
Metadata:
encoder : Lavc56.60.100 mpeg4
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
frame= 215 fps= 15 q=5.3 Lsize= 294kB time=00:00:07.16 bitrate= 336.2kbits/s
video:283kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.772538%
strong@foreverstrong:~/ffmpeg$
strong@foreverstrong:~/ffmpeg$ ll
total 304
drwxrwxr-x 2 strong strong 4096 2月 2 17:40 ./
drwxr-xr-x 29 strong strong 4096 2月 2 17:40 ../
-rw-rw-r-- 1 strong strong 301178 2月 2 17:41 output_video.avi
strong@foreverstrong:~/ffmpeg$