Linux之多媒体应用接口v4l-utils及摄像头简单使用

简述

V4L

V4L(Video For Linux, or Video4Linux):

The V4L API is essentially a kernel interface for analog video capture and output drivers, and is applicable to most video streams that are not related to DVB devices (see explanation below) or graphics cards – though, the V4L API also entails a few oddities such as analog radio and RDS services. Examples of devices which fall within the scope of V4L are, amongst others, webcams, MPEG encoders/decoders, digital video streamers, analog TV tuners and video capture devices.

v4l-utils

v4l-utils 是一系列多媒体设备处理包的集合。
官方仓库托管在:https://git.linuxtv.org/v4l-utils.git
很多系统发行版都已集成,包括Ubuntu等等。

v4l-utils 组成

v4l-utils 提供了一系列库和实用工具,用于控制各种多媒体设备。它不仅可以控制我们常见的视频输入输出,还包括红外控制,CEC的控制,无线电控制等等。

v4l-utils 主要包含下面3个库:

  • libv4l 库: 帮助 V4L2 应用处理不同的视频格式, i包括一些比较特殊的webcam模块的格式;
  • libdvbv5库:帮助开发使用 Linux [DVB version 5 API]的DVB 应用;
  • libv4l2rds 库: 帮助开发 RDS 无线电应用;

实用工具

v4l-utils 包含下面一系列实用程序:

  • DVBv5_Tools: tools to scan, zap and do other neat things with DVB devices;
  • ir-keytable: Dump, Load or Modify ir receiver input tables;
  • ir-ctl: A swiss-knife tool to handle raw IR and to set lirc options;
  • media-ctl: Tool to handle media controller devices;
  • qv4l2: QT v4l2 control panel application;
  • v4l2-compliance: Tool to test v4l2 API compliance of drivers;
  • v4l2-ctl: tool to control v4l2 controls from the cmdline;
  • v4l2-dbg: tool to directly get and set registers of v4l2 devices;
  • v4l2-sysfs-path: checks the media devices installed on a machine and the corresponding device nodes;
  • rds-ctl: tool to handle RDS radio devices;
  • cec-ctl: tool to control CEC devices from the command line;
  • cec-follower: tool used to emulate CEC followers;
  • cec-compliance: tool to test CEC API compliance of drivers and remote CEC devices;
  • xc3028-firmware: Xceive XC2028/3028 tuner module firmware manipulation tool;
  • cx18-ctl: tool to handle cx18 based devices (deprecated in favor of v4l2-ctl);
  • ivtv-ctl: tool to handle ivtv based devices (deprecated in favor of v4l2-ctl);
  • decode_tm6000: ancillary tool to decodes tm6000 proprietary format streams;

与摄像头相关的简单使用

命令行方式

  1. 安装

    sudo apt-get install v4l-utils
  2. 列出camera设备

    v4l2-ctl --list-devices
  3. 列出camera支持的配置参数
    查看设备所支持的参数,包括参数的默认值、当前值、最小值、最大值、步长等。

    #对应[VIDIOC_QUERYCTRL]
    v4l2-ctl -d <videoName> --list-ctrls
    v4l2-ctl -d <videoName> -l
    
    #对应[VIDIOC_QUERYMENU]
    v4l2-ctl -d <videoName> --list-ctrls-menus
    v4l2-ctl -d <videoName> -L
  4. 获取某个配置参数的值

    #对应[VIDIOC_G_EXT_CTRLS]
    v4l2-ctl -d <videoName> --get-ctrl=<name>
    v4l2-ctl -d <videoName> -C <name>
  5. 设置某个配置参数的值

    #对应[VIDIOC_S_EXT_CTRLS]
    v4l2-ctl -d <videoName> --set-ctrl=<name>=<value>
    v4l2-ctl -d <videoName> -c <name>=<value>
  6. 列出camera支持的格式、分辨率、帧率

    #对应[VIDIOC_ENUM_FMT]
    v4l2-ctl -d <videoName> --list-formats-ext
  7. 简单拍摄
    需要根据具体的摄像头调整有关参数。

    v4l2-ctl --verbose -d /dev/video0 --set-fmt-video=width=1920,height=1080,pixelformat='NV12' --stream-mmap=4 --stream-skip=4 --set-selection=target=crop,flags=0,top=0,left=0,width=1920,height=1080 --stream-to=/data/out.yuv --stream-count=60 --stream-poll
  8. v4l2-ctl 其他功能
    可以通过 v4l2-ctl -h 查看工具的其它参数

  9. 打印 Media Control 拓扑结构

    media-ctl –d /dev/mediaX –p

C接口

主要是通过ioctl去操作。

Linux API version 2 (V4L2 API):https://linuxtv.org/downloads/v4l-dvb-apis/userspace-api/v4l/v4l2.html

具体的一些流程和操作可见以前的另外一篇博文:https://notes.z-dd.online/2019/05/21/V4L2%E9%87%87%E9%9B%86%E8%A7%86%E9%A2%91/

扩展

V4L 和 V4L2的差异:

The Video For Linux API was first introduced in Linux 2.1 to unify and replace various TV and radio device related interfaces, developed independently by driver writers in prior years. Starting with Linux 2.5 the much improved V4L2 API replaces the V4L API. The support for the old V4L calls were removed from Kernel, but the library Libv4l Userspace Library supports the conversion of a V4L API system call into a V4L2 one.

详情可见官方说明:https://linuxtv.org/downloads/v4l-dvb-apis/userspace-api/v4l/diff-v4l.html?highlight=vidiocgcap

参考