USB摄像头
操作环境:
- Newman USB Camera camera-NM13-4K
- LPA3588
- Ubuntu20.04
查找USB摄像头文件名
插入USB摄像头后,内核日志如下:
neardi@LPA3588:~$ dmesg
[ 2740.549384] usb 7-1.2: new high-speed USB device number 6 using xhci-hcd
[ 2740.705306] usb 7-1.2: New USB device found, idVendor=1bcf, idProduct=0215, bcdDevice=34.07
[ 2740.705328] usb 7-1.2: New USB device strings, Mfr=1, Product=2, SerialNumber=0
[ 2740.705341] usb 7-1.2: Product: 4K AutoFocus Webcam
[ 2740.705353] usb 7-1.2: Manufacturer: 4K AutoFocus Webcam
[ 2740.775878] uvcvideo: Found UVC 1.00 device 4K AutoFocus Webcam (1bcf:0215)
[ 2741.037436] usb 7-1.2: Warning! Unlikely big volume range (=4096), cval->res is probably wrong.
[ 2741.037459] usb 7-1.2: [7] FU [Mic Capture Volume] ch = 1, val = 0/4096/1
您也可以使用lsusb命令列出所有USB��设备:
neardi@LPA3588:~$ lsusb
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 008 Device 003: ID 0bda:9210 Realtek Semiconductor Corp. Ugreen Storage Device
Bus 008 Device 002: ID 05e3:0620 Genesys Logic, Inc. USB3.2 Hub
Bus 008 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 007 Device 005: ID 046d:c534 Logitech, Inc. Unifying Receiver
Bus 007 Device 006: ID 1bcf:0215 Sunplus Innovation Technology Inc. 4K AutoFocus Webcam
Bus 007 Device 002: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
ID为1bcf:0215的设备就是4K Newman USB摄像头。
使用v4l2-ctl命令列出所有视频设备:
neardi@LPA3588:~$ v4l2-ctl --list-devices
......
4K AutoFocus Webcam: 4K AutoFoc (usb-xhci-hcd.6.auto-1.2):
/dev/video23
/dev/video24
/dev/media2
这表明ID为1bcf:0215的USB摄像头的设备文件是/dev/video23和/dev/video24。
打开摄像头
查看摄像头支持的格式、分辨率和帧率:
neardi@LPA3588:~/samba$ v4l2-ctl -d /dev/video23 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
......
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 1600x900
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
.....
使用GStreamer(推荐)
实时视频预览
neardi@LPA3588:~$ gst-launch-1.0 v4l2src device=/dev/video23 ! image/jpeg,width=640,height=480,framerate=30/1 ! mppjpegdec ! videoconvert ! fpsdisplaysink
编码并保存为H.264文件
neardi@LPA3588:~$ neardi@LPA3588:~$ gst-launch-1.0 v4l2src device=/dev/video23 ! image/jpeg,width=640,height=480,framerate=30/1 ! mppjpegdec ! videoconvert ! mpph264enc ! h264parse ! filesink location=output.h264
将视频流编码为H.264并保存到output.h264。
使用V4l2工具
neardi@LPA3588:~$ sudo apt update
neardi@LPA3588:~$ sudo apt install qv4l2
neardi@LPA3588:~$ qv4l2 -d /dev/video23
使用ffplay
neardi@LPA3588:~$ ffplay /dev/video23
常见问题
4K60FPS丢帧故障排除和优化
当捕获高分辨率和高帧率视频(例如4K@60fps)时,您可能会遇到丢帧(报告为dropped buffers)。例如:
neardi@LPA3588:~# v4l2-ctl -d /dev/video41 --set-fmt-video=width=1280,height=720,pixelformat=YUYV --stream-mmap --stream-count=1000 --stream-skip=3
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 1
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 5
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 1
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 2
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 1
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 15
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 59.95 fps, dropped buffers: 1
解决方案:启用性能模式
为了减少由CPU频率调节引起的丢帧,将系统切换到performance模式:
sudo -i;
echo performance | tee $(find /sys/ -name *governor);