CategoriesTechnology

ffmpeg + Web Camera でさくっと録画する

Linux サーバに USB Web Camera を繋いで決まった時間に決まった長さで録画する設定をしたので備忘録。ffmpeg や font のインストールは割愛。使った Distro は CentOS Stream 8。

まず使うデバイスを確認。Video デバイスは下記コマンドで対象を特定。

$ sudo udevadm info -q all -n /dev/video2
P: /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/video4linux/video2
N: video2
S: v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera-video-index0
S: v4l/by-path/pci-0000:00:14.0-usb-0:4:1.0-video-index0
E: DEVLINKS=/dev/v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera-video-index0 /dev/v4l/by-path/pci-0000:00:14.0-usb-0:4:1.0-video-index0
E: DEVNAME=/dev/video2
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/video4linux/video2
E: ID_BUS=usb
E: ID_FOR_SEAT=video4linux-pci-0000_00_14_0-usb-0_4_1_0
E: ID_MODEL=USB_2.0_Camera
E: ID_MODEL_ENC=USB\x202.0\x20Camera
E: ID_MODEL_ID=6369
E: ID_PATH=pci-0000:00:14.0-usb-0:4:1.0
E: ID_PATH_TAG=pci-0000_00_14_0-usb-0_4_1_0
E: ID_REVISION=0100
E: ID_SERIAL=Sonix_Technology_Co.__Ltd._USB_2.0_Camera
E: ID_TYPE=video
E: ID_USB_DRIVER=uvcvideo
E: ID_USB_INTERFACES=:0e0100:0e0200:
E: ID_USB_INTERFACE_NUM=00
E: ID_V4L_CAPABILITIES=:capture:
E: ID_V4L_PRODUCT=USB 2.0 Camera: USB Camera
E: ID_V4L_VERSION=2
E: ID_VENDOR=Sonix_Technology_Co.__Ltd.
E: ID_VENDOR_ENC=Sonix\x20Technology\x20Co.\x2c\x20Ltd.
E: ID_VENDOR_ID=0c45
E: MAJOR=81
E: MINOR=2
E: SUBSYSTEM=video4linux
E: TAGS=:uaccess:seat:
E: USEC_INITIALIZED=255154700755

ビデオデバイスの対応しているフォーマットは ffmpeg から確認。

sudo ffmpeg -list_formats all -i /dev/video2
ffmpeg version 4.2.8 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 8 (GCC)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --extra-ldflags='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld ' --extra-cflags=' ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-fontconfig --enable-frei0r --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libaom --enable-libdav1d --enable-libass --enable-libbluray --enable-libcdio --enable-libdrm --enable-libjack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libmp3lame --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librsvg --enable-libsrt --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-version3 --enable-vapoursynth --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --enable-libzvbi --enable-avfilter --enable-avresample --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-libmfx --enable-runtime-cpudetect
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[video4linux2,v4l2 @ 0x561572800280] Compressed:       mjpeg :          Motion-JPEG : 1920x1080 640x480 1280x720 640x360 320x240 800x600 960x720 352x288 432x240 320x184 176x144 1280x960 1280x1024 1024x768
[video4linux2,v4l2 @ 0x561572800280] Raw       :     yuyv422 :           YUYV 4:2:2 : 1920x1080 640x480 1280x720 432x240 1024x768 352x288 320x240 176x144 160x120 800x600 1280x720 960x720 1280x960 1280x1024

サウンドデバイスは下記で確認。

$ sudo arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: C615 [HD Webcam C615], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC233 Analog [ALC233 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 2: ALC233 Alt Analog [ALC233 Alt Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

あとは録画用の Shell Script を作成。今回は時刻を焼き込みたかったので下記のようにした。

#!/bin/bash

VIDEO_SIZE="1024x576"

FILE=$(date '+%m-%d-%y.%H:%M:%S')
FONT="/usr/share/fonts/google-noto/NotoMono-Regular.ttf"

CLOCK_TEXT="'%{localtime\:%a %x %X}'"
CLOCK="box=1:boxcolor=white:fontcolor=black:fontfile=${FONT}:text=${CLOCK_TEXT}:x=0:y=0"

ALSA_DEV="hw:0,0,0"
CAMERA_DEV="/dev/video2"

# 11 hours
TIME=39600

ffmpeg -y \
       -hide_banner \
       -re -f alsa -thread_queue_size 16384 -ac 1 -channel_layout mono -ar 8000 -t ${TIME} -i ${ALSA_DEV} \
       -re -f v4l2 -thread_queue_size 16384 -input_format yuyv422 -video_size ${VIDEO_SIZE} -framerate 30 -t ${TIME} -i ${CAMERA_DEV}  \
       -c:v libx264 -b:v 2M -vsync 1 -r 5 -g 10 -sc_threshold -1 -preset veryfast -s ${VIDEO_SIZE} \
       -c:a aac -b:a 64k -ar 8000 \
       -filter_complex "drawtext=${CLOCK}" \
       ${FILE}.mp4

これを適当なパスに配置して cron で指定時刻にトリガーされるように設定すれば OK。

ちなみに Web カメラは Amazon.com で売ってた Night Vision カメラを使っている。夜の暗闇でも結構ばっちり撮れる。基板むき出しだが、他の似たような製品より安上がりなのでインドア用途ならこれで良いと思う。

https://a.co/d/faXmG9w