Linux USB摄像头使用

一、使用V4l2工具调试摄像头

1.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

2.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

3.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

二、安装应用程序显示摄像头捕捉到的视频

1.使用应用程序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 是最好的连通解决方案。

2.使用应用程序茄子(cheese)

cheese是Ubuntu自带摄像软件,中文名称茄子。

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

FFmpeg常用命令

  • 对海康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获取摄像头图像

gstreamer v4l2src 插件

# 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

五、图片加上文字

https://blog.gtwang.org/linux/imagemagick-image-watermarking-tutorial/

html颜色代码

Ubuntu 添加中文字体

sudo apt-get install imagemagick

# 左下角
convert /data/tmpfs/capture.jpg -gravity SouthWest -pointsize 40 \
   -stroke none   -fill white    -annotate +5+5  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -stroke none    -fill white    -annotate +5+70  "$text"   /data/tmpfs/output.jpg
   
# 左上角
convert /data/tmpfs/capture.jpg -gravity NorthWest -pointsize 40 \
   -stroke none   -fill white    -annotate +5+5  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -stroke '#40DCD7'    -fill white    -annotate +5+70  "$text"   /data/tmpfs/output.jpg
   
 # 有阴影的文字
convert /data/tmpfs/capture.jpg \
  \( -background 'rgba(0, 0, 0, .5)' -fill white \
     -font Helvetica -pointsize 60                \
      label:"`date "+%Y-%m-%d %H:%M:%S"` " -splice 20x20               \
  \) -gravity NorthWest -geometry +40+40 \
  -composite /data/tmpfs/output2.jpg
 
   
# 使用中文字体
text="Hello"
text2="D10.08"
# font对应中文字体路径
convert /data/tmpfs/capture.jpg -font /usr/share/fonts/truetype/wqy/wqy-microhei.ttc  -gravity NorthWest -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -gravity NorthEast -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "$text2" \
   -gravity SouthEast -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "$text"  /data/tmpfs/output.jpg
  • 图片方位

    • NorthWestNorthNorthEastWestCenterEastSouthWestSouthSouthEast
  • 安装中文字体

    sudo apt install -y --force-yes --no-install-recommends fonts-wqy-microhei
    sudo apt install -y --force-yes --no-install-recommends ttf-wqy-zenhei
    # 查看中文字体路径
    $ fc-list :lang=zh-cn
    /usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑,文泉驿微米黑:style=Regular
    /usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑,文泉驿等宽微米黑:style=Regular
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269