< >
Home » Ubuntu系统入门教程 » Ubuntu系统入门教程-USB摄像头使用

Ubuntu系统入门教程-USB摄像头使用

Ubuntu系统入门教程-USB摄像头使用

说明:

  • 介绍如何在ubuntu下使用usb摄像头

v4l2-ctl使用:

  • 安装V4l2工具包
$ sudo apt install v4l-utils
  • 通过v4l2查看摄像头设备
$ sudo v4l2-ctl --list-devices
EP28WD (usb-fe380000.usb-1.3):
        /dev/video0
        /dev/video1
    
  • 查看当前摄像头支持的视频压缩格式
$ sudo v4l2-ctl -d /dev/video0 --list-formats
        ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'YUYV'
        Name        : YUYV 4:2:2

        Index       : 1
        Type        : Video Capture
        Pixel Format: 'H264' (compressed)
        Name        : H.264

        Index       : 2
        Type        : Video Capture
        Pixel Format: 'MJPG' (compressed)
        Name        : Motion-JPEG
  • 查看摄像头所有参数
$ sudo v4l2-ctl -d  /dev/video0 --all
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : EP28WD
        Bus info      : usb-fe380000.usb-1.3
        Driver version: 4.4.167
        Capabilities  : 0x84A00001
                Video Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
        Width/Height      : 640/480
        Pixel Format      : 'YUYV'
        Field             : None
        Bytes per Line    : 1280
        Size Image        : 614400
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 640, Height 480
        Default     : Left 0, Top 0, Width 640, Height 480
        Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 640, Height 480
Selection: crop_bounds, Left 0, Top 0, Width 640, Height 480
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 25.000 (25/1)
        Read buffers     : 0
                     brightness (int)    : min=-64 max=64 step=1 default=0 value=0
                       contrast (int)    : min=0 max=64 step=1 default=36 value=36
                     saturation (int)    : min=0 max=128 step=1 default=64 value=64
                            hue (int)    : min=-40 max=40 step=1 default=0 value=0
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=72 max=500 step=1 default=100 value=100
                           gain (int)    : min=0 max=100 step=1 default=0 value=0
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=4600 value=4600 flags=inactive
                      sharpness (int)    : min=0 max=6 step=1 default=3 value=3
         backlight_compensation (int)    : min=0 max=2 step=1 default=1 value=1
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=1 max=5000 step=1 default=157 value=157 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
     
  • 查看摄像头所支持的分辨率
 sudo v4l2-ctl --list-framesizes=MJPG -d /dev/video0
 ioctl: VIDIOC_ENUM_FRAMESIZES
    Size: Discrete 640x480   # Width/Height
    Size: Discrete 800x600
    Size: Discrete 1280x720
    Size: Discrete 1280x960
    Size: Discrete 1920x1080
    Size: Discrete 1600x1200
    Size: Discrete 2048x1536
    Size: Discrete 2592x1944
    Size: Discrete 3264x2448
  • v4l2-ctl 命令
$ v4l2-ctl help
unknown arguments: help 

General/Common options:
  --all              display all information available
  -C, --get-ctrl=<ctrl>[,<ctrl>...]
                     get the value of the controls [VIDIOC_G_EXT_CTRLS]
  -c, --set-ctrl=<ctrl>=<val>[,<ctrl>=<val>...]
                     set the value of the controls [VIDIOC_S_EXT_CTRLS]
  -D, --info         show driver info [VIDIOC_QUERYCAP]
  -d, --device=<dev> use device <dev> instead of /dev/video0
                     if <dev> starts with a digit, then /dev/video<dev> is used
  -e, --out-device=<dev> use device <dev> for output streams instead of the
                     default device as set with --device
                     if <dev> starts with a digit, then /dev/video<dev> is used
  -h, --help         display this help message
  --help-all         all options
  --help-io          input/output options
  --help-misc        miscellaneous options
  --help-overlay     overlay format options
  --help-sdr         SDR format options
  --help-selection   crop/selection options
  --help-stds        standards and other video timings options
  --help-streaming   streaming options
  --help-tuner       tuner/modulator options
  --help-vbi         VBI format options
  --help-vidcap      video capture format options
  --help-vidout      vidout output format options
  --help-edid        edid handling options
  -k, --concise      be more concise if possible.
  -l, --list-ctrls   display all controls and their values [VIDIOC_QUERYCTRL]
  -L, --list-ctrls-menus
             display all controls and their menus [VIDIOC_QUERYMENU]
  -r, --subset=<ctrl>[,<offset>,<size>]+
                     the subset of the N-dimensional array to get/set for control <ctrl>,
                     for every dimension an (<offset>, <size>) tuple is given.
  -w, --wrapper      use the libv4l2 wrapper library.
  --list-devices     list all v4l devices
  --log-status       log the board status in the kernel log [VIDIOC_LOG_STATUS]
  --get-priority     query the current access priority [VIDIOC_G_PRIORITY]
  --set-priority=<prio>
                     set the new access priority [VIDIOC_S_PRIORITY]
                     <prio> is 1 (background), 2 (interactive) or 3 (record)
  --silent           only set the result code, do not print any messages
  --sleep=<secs>     sleep <secs>, call QUERYCAP and close the file handle
  --verbose          turn on verbose ioctl status reporting
  • usb摄像头与video匹配
  • linux系统接上usb摄像头会出现/dev/video设备,如果接多个摄像头,通过/dev/video无法与真实摄像头一一对应。
  • 由于有的时候一个usb摄像头会产生两个或多个video(只有一个能拍照),所以通过udev的rule规则也无法固定。
  • 这时可以通过/dev/v4l/by-id/路径的软链接固定。
$ ll /dev/v4l/by-id/
lrwxrwxrwx 1 root root  12 Sep 10 14:32 usb-Generic_HDR_CAMERA_200901010001-video-index0 -> ../../video0
lrwxrwxrwx 1 root root  12 Sep 10 14:32 usb-Generic_HDR_CAMERA_200901010001-video-index1 -> ../../video1
lrwxrwxrwx 1 root root  12 Sep 10 14:24 usb-USB3.0_Camera_8M_USB3.0_Camera_8M_SITN00000000001-video-index0 -> ../../video2
lrwxrwxrwx 1 root root  12 Sep 10 14:24 usb-USB3.0_Camera_8M_USB3.0_Camera_8M_SITN00000000001-video-index1 -> ../../video3

camorama捕捉视频:

  • 安装
sudo apt-get install camorama
camorama
  • 如果使用Xshell进行ssh远程访问,会提醒安装Xmanager软件显示视频流
  • Xmanager是一个运行于MS Windows平台上的高性能的X window服务器。
  • 可以在本地PC上同时运行Unix/Linux和Windows图形应用程序。
  • Xmanager可以将PC变成X Windows工作站,它可以无缝拼接到UNIX 应用程序中。
  • 在UNIX/Linux和Windows网络环境中,Xmanager 是最好的连通解决方案。

cheese捕捉视频:

  • 安装
sudo apt-get install cheese
cheese
# 或
cheese /dev/video0

#不支持ssh远程显示

ffmpeg获取图像:

  • 安装
sudo apt-get install ffmpeg

sudo ffmpeg -i /dev/video0 -vframes 1 -s 640x480 -q:v 2 -f image2 /tmp/image6-vdieo0-test1.jpg
  • 对海康IP摄像头进行拍照、推流和保存视频
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4 \ # base
    -vcodec copy -an -f flv rtmp://172.17.0.1/live/camera0 \    # stream
    -flags +global_header -f stream_segment -segment_time 60 -segment_format_options movflags=+faststart -reset_timestamps 1 -vcodec copy -q:v 4 -an -r 24 -strftime 1 /data/tmpfs/camera/video/%s.flv \    # video
    -r 2/1 -strftime 1 -y /data/tmpfs/camera/capture.jpg  # capture

# 从ip摄像头推流
sudo ffmpeg -rtsp_transport tcp -i rtsp://admin:jiangxing123@10.56.0.17:554/h264/ch1/sub/av_stream -vcodec copy -an -f flv rtmp://10.55.2.185:1935/live/camera0

# 从视频文件推流
sudo ffmpeg -re  -i record.flv -vcodec copy -an -f flv  rtmp://10.55.2.185:1935/live/camera0

# 从usb摄像头推流
sudo ffmpeg -f video4linux2 -qscale 10 -r 12 -s 640x480 -i /dev/video0  -f flv rtmp://10.55.2.185:1935/live/camera0

# ==============test==============
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4  -f flv -c:v libx264 -an rtmp://10.55.2.185:1935/live/camera0
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4  -vcodec copy -an  -f flv  rtmp://10.55.2.185:1935/live/camera0

sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4 -vcodec libx264  -crf 5 -acodec aac -f flv rtmp://10.55.2.185:1935/live/camera0 

sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4   -vcodec libx264 -acodec aac -f flv rtmp://10.55.2.185:1935/live/camera0

gstreamer获取图像

  • 安装
# install gstreamer
apt-get -y install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
  • 连续拍照
gst-launch-1.0 --gst-debug-level=3 v4l2src device=/dev/video0 ! \
        image/jpeg,width=1920,height=1080,framerate=30/1 ! \
        multifilesink location=/data/tmpfs/capture1.jpg max-files=1 post-messages=true
        
  • 单次拍照
gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=1 ! image/jpeg,width=1920,height=1080 ! filesink location=/data/tmpfs/capture2.jpg
  • 对海康IP摄像头进行拍照、推流和保存视频
gst-launch-1.0 -e --gst-debug-level=3 rtspsrc location=rtsp://192.168.1.19:554/mpeg4 ! rtph264depay ! h264parse ! tee name=t \
    t. ! queue ! avdec_h264 ! queue flush-on-eos=true ! videorate ! video/x-raw,framerate=5/1 ! jpegenc ! multifilesink post-messages=true location=/data/tmpfs/camera/capture.jpg max-files=1 \    # capture
    t. ! queue ! flvmux streamable=true ! rtmpsink sync=false location=rtmp://172.17.0.1/live/camera0 \ # stream
    t. ! queue ! splitmuxsink max-size-time=600000000000 location=/data/tmpfs/camera/video/%06d.mp4 # video

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ubuntu系统入门教程