当前位置: 首页 > 工具软件 > ffmpeg-sharp > 使用案例 >

ffmpeg-linux录音录像

邹博裕
2023-12-01

查看ffmpeg支持的设备格式

leadcom@leadcom:~$ ffmpeg -hide_banner -devices
Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 DE alsa            ALSA audio output
  E caca            caca (color ASCII art) output device
 DE fbdev           Linux framebuffer
 D  iec61883        libiec61883 (new DV1394) A/V input device
 D  jack            JACK Audio Connection Kit
 D  kmsgrab         KMS screen capture
 D  lavfi           Libavfilter virtual input device
 D  libcdio          
 D  libdc1394       dc1394 v.2 A/V grab
 D  openal          OpenAL audio capture device
  E opengl          OpenGL output
 DE oss             OSS (Open Sound System) playback
 DE pulse           Pulse audio output
  E sdl,sdl2        SDL2 output device
 DE sndio           sndio audio playback
 DE video4linux2,v4l2 Video4Linux2 output device
 D  x11grab         X11 screen capture, using XCB
  E xv              XV (XVideo) output device

音频输入设备

ffmpeg支持alsa格式的音频输入设备

查看ffmpeg支持的alsa格式的音频输入参数选项

leadcom@leadcom:~$ ffmpeg -hide_banner -h demuxer=alsa
Demuxer alsa [ALSA audio input]:
ALSA demuxer AVOptions:
  -sample_rate       <int>        .D.......  (from 1 to INT_MAX) (default 48000)
  -channels          <int>        .D.......  (from 1 to INT_MAX) (default 2)

可以看到只有采样率sample_rate、声道数channels这两个参数。

查看音频输入设备

leadcom@leadcom:~$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC3227 Analog [ALC3227 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Camera [Full HD 1080P PC Camera], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

可以看到当前电脑有两个音频输入,其中Camera [Full HD 1080P PC Camera]是我自己的USB摄像头中的麦克风设备。

查看音频输入设备的具体参数

查看上面USB摄像头中的麦克风设备的具体参数

leadcom@leadcom:~$ arecord -D hw:1,0 --dump-hw-params
Recording WAVE 'stdin' : Unsigned 8 bit, Rate 8000 Hz, Mono
HW Params of device "hw:1,0":
--------------------
ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: [44100 48000]
PERIOD_TIME: [1000 2972155)
PERIOD_SIZE: [45 131072]
PERIOD_BYTES: [180 524288]
PERIODS: [2 1024]
BUFFER_TIME: [1875 5944309)
BUFFER_SIZE: [90 262144]
BUFFER_BYTES: [360 1048576]
TICK_TIME: ALL
--------------------
arecord: set_params:1339: Sample format non available
Available formats:
- S16_LE

这里通过指定声卡名称获取其硬件参数信息,可以看到channels通道数和rate即sample_rate采样率。
采样率是一个区间,但并意味着区间内所有值都有效,一般只有几个特定值可以。
例如44100/48000/192000,其他的值将会变成最接近上面三个值中的一个,这其中的验证方法可以通过ffmpeg命令行来试验一下,如下:
ffmpeg -f alsa -sample_rate 采样率 -i hw:1,0 -t 10 test.wav -v trace
查看命令行的输出结果,可以看到最终可用的采样率。

视频输入设备

ffmpeg支持v4l2格式的视频输入设备

查看ffmpeg支持的v4l2格式的视频输入参数选项

leadcom@leadcom:~$ ffmpeg -hide_banner -h demuxer=v4l2
Demuxer video4linux2,v4l2 [Video4Linux2 device grab]:
V4L2 indev AVOptions:
  -standard          <string>     .D....... set TV standard, used only by analog frame grabber
  -channel           <int>        .D....... set TV channel, used only by frame grabber (from -1 to INT_MAX) (default -1)
  -video_size        <image_size> .D....... set frame size
  -pixel_format      <string>     .D....... set preferred pixel format
  -input_format      <string>     .D....... set preferred pixel format (for raw video) or codec name
  -framerate         <string>     .D....... set frame rate
  -list_formats      <int>        .D....... list available formats and exit (from 0 to INT_MAX) (default 0)
     all                          .D....... show all available formats
     raw                          .D....... show only non-compressed formats
     compressed                   .D....... show only compressed formats
  -list_standards    <int>        .D....... list supported standards and exit (from 0 to 1) (default 0)
     all                          .D....... show all supported standards
  -timestamps        <int>        .D....... set type of timestamps for grabbed frames (from 0 to 2) (default default)
     default                      .D....... use timestamps from the kernel
     abs                          .D....... use absolute timestamps (wall clock)
     mono2abs                     .D....... force conversion from monotonic to absolute timestamps
  -ts                <int>        .D....... set type of timestamps for grabbed frames (from 0 to 2) (default default)
     default                      .D....... use timestamps from the kernel
     abs                          .D....... use absolute timestamps (wall clock)
     mono2abs                     .D....... force conversion from monotonic to absolute timestamps
  -use_libv4l2       <boolean>    .D....... use libv4l2 (v4l-utils) conversion functions (default false

查看视频输入设备

leadcom@leadcom:~$ ffmpeg -hide_banner -sources v4l2
Auto-detected sources for video4linux2,v4l2:
  /dev/video3 [HP TrueVision HD Camera: HP Tru]
  /dev/video2 [HP TrueVision HD Camera: HP Tru]
  /dev/video1 [Full HD 1080P PC Camera: Full H]
  /dev/video0 [Full HD 1080P PC Camera: Full H]

或者使用v4l2-ctl工具(apt install v4l-utils)

leadcom@leadcom:~$ v4l2-ctl --list-devices
Full HD 1080P PC Camera: Full H (usb-0000:00:14.0-1):
	/dev/video0
	/dev/video1

HP TrueVision HD Camera: HP Tru (usb-0000:00:14.0-3):
	/dev/video2
	/dev/video3

可以看到摄像头有两个video目录,其中一个是图像/视频采集,一个是metadata采集。
如何区别他们两个呢?

查看视频输入设备的具体参数

leadcom@leadcom:~$ ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
[video4linux2,v4l2 @ 0x55ccd5f848c0] Compressed:       mjpeg :          Motion-JPEG : 1920x1080 352x288 1712x960 1600x896 1280x960 800x600 432x240 1280x720 640x480 640x360 848x480
[video4linux2,v4l2 @ 0x55ccd5f848c0] Raw       :     yuyv422 :           YUYV 4:2:2 : 1920x1080 352x288 1712x960 1600x896 1280x960 800x600 432x240 1280x720 640x480 640x360 848x480
/dev/video0: Immediate exit requested

或者使用v4l2-ctl查看

# 查看当前摄像头支持的视频压缩格式
leadcom@leadcom:~$ v4l2-ctl -d /dev/video0 --list-formats
ioctl: VIDIOC_ENUM_FMT
	Type: Video Capture

	[0]: 'MJPG' (Motion-JPEG, compressed)
	[1]: 'YUYV' (YUYV 4:2:2)
# 查看摄像头所有参数
leadcom@leadcom:~$ v4l2-ctl -d  /dev/video0 --all
Driver Info:
	Driver name      : uvcvideo
	Card type        : Full HD 1080P PC Camera: Full H
	Bus info         : usb-0000:00:14.0-1
	Driver version   : 5.10.50
	Capabilities     : 0x84a00001
		Video Capture
		Metadata 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      : 1920/1080
	Pixel Format      : 'YUYV' (YUYV 4:2:2)
	Field             : None
	Bytes per Line    : 3840
	Size Image        : 4147200
	Colorspace        : Default
	Transfer Function : Default (maps to Rec. 709)
	YCbCr/HSV Encoding: Default (maps to ITU-R 601)
	Quantization      : Default (maps to Limited Range)
	Flags             : 
Crop Capability Video Capture:
	Bounds      : Left 0, Top 0, Width 1920, Height 1080
	Default     : Left 0, Top 0, Width 1920, Height 1080
	Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 1920, Height 1080, Flags: 
Selection: crop_bounds, Left 0, Top 0, Width 1920, Height 1080, Flags: 
Streaming Parameters Video Capture:
	Capabilities     : timeperframe
	Frames per second: 5.000 (5/1)
	Read buffers     : 0
                     brightness 0x00980900 (int)    : min=-10 max=10 step=1 default=2 value=2
                       contrast 0x00980901 (int)    : min=1 max=32 step=1 default=17 value=17
                     saturation 0x00980902 (int)    : min=0 max=32 step=1 default=17 value=17
                            hue 0x00980903 (int)    : min=-5 max=5 step=1 default=0 value=0
 white_balance_temperature_auto 0x0098090c (bool)   : default=1 value=1
                          gamma 0x00980910 (int)    : min=100 max=200 step=1 default=150 value=150
           power_line_frequency 0x00980918 (menu)   : min=0 max=2 default=1 value=0
      white_balance_temperature 0x0098091a (int)    : min=2800 max=6500 step=1850 default=6500 value=6500 flags=inactive
                      sharpness 0x0098091b (int)    : min=0 max=10 step=1 default=6 value=6
                  exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute 0x009a0902 (int)    : min=156 max=5000 step=1 default=512 value=200 flags=inactive
# 查看摄像头所支持的分辨率
leadcom@leadcom:~$ v4l2-ctl --list-framesizes=MJPG -d /dev/video0
ioctl: VIDIOC_ENUM_FRAMESIZES
	Size: Discrete 1920x1080
	Size: Discrete 352x288
	Size: Discrete 1712x960
	Size: Discrete 1600x896
	Size: Discrete 1280x960
	Size: Discrete 800x600
	Size: Discrete 432x240
	Size: Discrete 1280x720
	Size: Discrete 640x480
	Size: Discrete 640x360
	Size: Discrete 848x480
 类似资料: