Linux USB攝像頭使用

一锄禽、使用V4l2工具調(diào)試攝像頭

1.v4l2-ctl 常用操作

# 安裝V4l2工具包
$ sudo apt install v4l-utils

# 通過v4l2查看攝像頭設(shè)備
$ sudo v4l2-ctl --list-devices
EP28WD (usb-fe380000.usb-1.3):
        /dev/video0
        /dev/video1
        
# 查看當(dāng)前攝像頭支持的視頻壓縮格式
$ 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

# 查看攝像頭所有參數(shù)
$ 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系統(tǒng)接上usb攝像頭會出現(xiàn)/dev/video*設(shè)備镣丑,如果接多個攝像頭,通過/dev/video*無法與真實(shí)攝像頭一一對應(yīng)晤斩。由于有的時候一個usb攝像頭會產(chǎn)生兩個或多個video(只有一個能拍照),所以通過udev的rule規(guī)則也無法固定。這時可以通過/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

二、安裝應(yīng)用程序顯示攝像頭捕捉到的視頻

1.使用應(yīng)用程序camorama

sudo apt-get install camorama
camorama

#如果使用Xshell進(jìn)行ssh遠(yuǎn)程訪問苦掘,會提醒安裝Xmanager軟件顯示視頻流
  • Xmanager是一個運(yùn)行于MS Windows平臺上的高性能的X window服務(wù)器换帜。可以在本地PC上同時運(yùn)行Unix/Linux和Windows圖形應(yīng)用程序鸟蜡。

  • Xmanager可以將PC變成X Windows工作站,它可以無縫拼接到UNIX 應(yīng)用程序中挺邀。在UNIX/Linux和Windows網(wǎng)絡(luò)環(huán)境中揉忘,Xmanager 是最好的連通解決方案。

2.使用應(yīng)用程序茄子(cheese)

cheese是Ubuntu自帶攝像軟件端铛,中文名稱茄子泣矛。

cheese + 設(shè)備號

sudo apt-get install cheese
cheese
# 或
cheese /dev/video0

#不支持ssh遠(yuǎn)程顯示

三、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攝像頭進(jìn)行拍照您朽、推流和保存視頻
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

# 連續(xù)拍照
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攝像頭進(jìn)行拍照哗总、推流和保存視頻
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對應(yīng)中文字體路徑
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
  • 圖片方位

    • NorthWest倍试、North讯屈、NorthEastWest县习、Center涮母、EastSouthWest躁愿、South叛本、SouthEast
  • 安裝中文字體

    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
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末彤钟,一起剝皮案震驚了整個濱河市来候,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逸雹,老刑警劉巖吠勘,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件性芬,死亡現(xiàn)場離奇詭異,居然都是意外死亡剧防,警方通過查閱死者的電腦和手機(jī)植锉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來峭拘,“玉大人俊庇,你說我怎么就攤上這事〖δ樱” “怎么了辉饱?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拣展。 經(jīng)常有香客問我彭沼,道長,這世上最難降的妖魔是什么备埃? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任姓惑,我火速辦了婚禮,結(jié)果婚禮上按脚,老公的妹妹穿的比我還像新娘于毙。我一直安慰自己,他們只是感情好辅搬,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布唯沮。 她就那樣靜靜地躺著,像睡著了一般堪遂。 火紅的嫁衣襯著肌膚如雪介蛉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天溶褪,我揣著相機(jī)與錄音甘耿,去河邊找鬼。 笑死竿滨,一個胖子當(dāng)著我的面吹牛佳恬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播于游,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼毁葱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贰剥?” 一聲冷哼從身側(cè)響起倾剿,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后前痘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凛捏,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年芹缔,在試婚紗的時候發(fā)現(xiàn)自己被綠了坯癣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡最欠,死狀恐怖示罗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芝硬,我是刑警寧澤蚜点,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站拌阴,受9級特大地震影響绍绘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜迟赃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一鞋仍、第九天 我趴在偏房一處隱蔽的房頂上張望惜犀。 院中可真熱鬧浊洞,春花似錦把曼、人聲如沸剪撬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽残黑。三九已至馍佑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間梨水,已是汗流浹背拭荤。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疫诽,地道東北人舅世。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像奇徒,于是被迫代替她去往敵國和親雏亚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355