用 VLC 播放 webcam 及用 ffmpeg 直播 YouTube

先列出哪些可以播放的 webcam

root@liho:~# v4l2-ctl --list-devices
UHD4K: UHD4K (usb-0000:00:14.0-7):
	/dev/video0
	/dev/video1
	/dev/media0

查看詳細資訊

root@liho:~# v4l2-ctl --info --device /dev/video0
Driver Info:
	Driver name      : uvcvideo
	Card type        : UHD4K: UHD4K
	Bus info         : usb-0000:00:14.0-7
	Driver version   : 5.15.111
	Capabilities     : 0x84a00001
		Video Capture
		Metadata Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x04200001
		Video Capture
		Streaming
		Extended Pix Format
Media Driver Info:
	Driver name      : uvcvideo
	Model            : UHD4K: UHD4K
	Serial           : 01.00.00
	Bus info         : usb-0000:00:14.0-7
	Media version    : 5.15.111
	Hardware revision: 0x00000001 (1)
	Driver version   : 5.15.111
Interface Info:
	ID               : 0x03000002
	Type             : V4L Video
Entity Info:
	ID               : 0x00000001 (1)
	Name             : UHD4K: UHD4K
	Function         : V4L2 I/O
	Flags         : default
	Pad 0x01000007   : 0: Sink
	  Link 0x02000013: from remote pad 0x100000a of entity 'Extension 4': Data, Enabled, Immutable

root@liho:~# v4l2-ctl --info --device /dev/video1
Driver Info:
	Driver name      : uvcvideo
	Card type        : UHD4K: UHD4K
	Bus info         : usb-0000:00:14.0-7
	Driver version   : 5.15.111
	Capabilities     : 0x84a00001
		Video Capture
		Metadata Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x04a00000
		Metadata Capture
		Streaming
		Extended Pix Format
Media Driver Info:
	Driver name      : uvcvideo
	Model            : UHD4K: UHD4K
	Serial           : 01.00.00
	Bus info         : usb-0000:00:14.0-7
	Media version    : 5.15.111
	Hardware revision: 0x00000001 (1)
	Driver version   : 5.15.111
Interface Info:
	ID               : 0x03000005
	Type             : V4L Video
Entity Info:
	ID               : 0x00000004 (4)
	Name             : UHD4K: UHD4K
	Function         : V4L2 I/O

用 arecord & aplay 指令查看聲音輸入

root@liho:~# arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: UHD4K [UHD4K], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

root@liho:~# aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 10: HDMI 4 [HDMI 4]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 11: HDMI 5 [HDMI 5]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 12: HDMI 6 [HDMI 6]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 13: HDMI 7 [HDMI 7]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 14: HDMI 8 [HDMI 8]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 15: HDMI 9 [HDMI 9]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 16: HDMI 10 [HDMI 10]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 17: HDMI 11 [HDMI 11]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

測試錄音

# ffmpeg -f alsa -i hw:1,0 -t 5 testmic.mp3

用 vlc 命令列直接把 webcam 導入 播放

$ vlc v4l2:///dev/video0

用 ffmpeg 指令直播到 youtube

# ffmpeg -threads 8 -f v4l2 -i /dev/video0 -f alsa -i hw:1,0 -preset ultrafast -tune zerolatency -c:v libx264 -pix_fmt yuv420p -framerate 24 -g 30 -b:v 4500k -c:a aac -b:a 128k -ar 44100 -ac 2 -f flv -flvflags no_duration_filesize rtmp://a.rtmp.youtube.com/live2/xxxxxxxxxx

在 ffmpeg 內要加入時間戳記 多加入 -vf 參數

-vf "drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: text='%{localtime}': fontcolor=white@0.75: fontsize=36: x=10: y=10"