ffmpeg在linux下支持的采集设备的种类比较多。
在操作设备之前,我们可以查看当前系统可以支持的操作设备:
ffmpeg -hide_banner -devices
Devices:
D. = Demuxing supported //输入的设备
.E = Muxing supported //输出的设备
--
DE alsa ALSA audio output
D dv1394 DV1394 A/V grab
DE fbdev Linux framebuffer //为输入输出设备,其他的类似这样判断
D lavfi Libavfilter virtual input device
DE oss OSS (Open Sound System) playback
E sdl SDL output device
E v4l2 Video4Linux2 output device
D video4linux2,v4l2 Video4Linux2 device grab
D x11grab X11 screen capture, using XCB
///
采集设备fbdev的参数:我们能看出fb(FrameBuffer)的操作参数,指定帧率就行(默认值为25).
ffmpeg -h demuxer=fbdev
Demuxer fbdev [Linux framebuffer]:
fbdev indev AVOptions:
-framerate .D...... (default "25")
ffmpeg -framerate 30 -f fbdev -i /dev/fb0 out.mp4
/
v4l2 即就是video4linux2的缩写,也是常见视频设备。获取摄像头的视频图片~
通过ffmpeg -h demuxer=v4l2 查看相关的操作参数
V4L2 indev AVOptions:
-standard .D...... set TV standard, used only by analog frame grabber
-channel .D...... set TV channel, used only by frame grabber (from -1 to INT_MAX) (default -1)
-video_size .D...... set frame size
-pixel_format .D...... set preferred pixel format
-input_format .D...... set preferred pixel format (for raw video) or codec name
-framerate .D...... set frame rate
-list_formats .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 .D...... list supported standards and exit (from 0 to 1) (default 0)
all .D...... show all supported standards
-timestamps .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 .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 .D...... use libv4l2 (v4l-utils) conversion functions (default false)
例子:
获取本电脑的v4l2摄像头所支持的色彩格式和分辨率:
ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
[video4linux2,v4l2 @ 0x9554e0] Raw : yuyv422 : YUV 4:2:2 (YUYV)
: 640x480 320x240 352x288 160x120 176x144 1280x720
[video4linux2,v4l2 @ 0x9554e0] Compressed: mjpeg : MJPEG
: 640x480 320x240 352x288 160x120 176x144 1280x720
接下来可以使用下面的命令行,用摄像头采集视频文件:
ffmpeg -hide_banner -s 1920*1080 -i /dev/video0 out2.avi
对应的输出信息为:
[video4linux2,v4l2 @ 0x15d0540] The V4L2 driver changed the video from 1920x1080 to 1280x720
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 611.836289, bitrate: 110592 kb/s
Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 1280x720, 110592 kb/s, 7.50 fps, 7.50 tbr, 1000k tbn, 1000k tbc
Output #0, avi, to 'out2.avi':
Metadata:
ISFT : Lavf57.25.100
Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 1280x720, q=2-31, 200 kb/s, 7.50 fps, 7.50 tbn, 7.50 tbc
Metadata:
encoder : Lavc57.24.102 mpeg4
Side data:
unknown side data type 10 (24 bytes)
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
因为我的摄像头不支持19201080,默认还原成了前面他所支持的参数 1280720,输出的视频编码采用了avi的默认编码和码率参数。
ffmpeg在linux下桌面图形的时候,通常是用x11grab设备采集桌面图像。
ffmpeg -h demuxer=x11grab 可以获得操作的参数:
Demuxer x11grab [X11 screen capture, using XCB]:
xcbgrab indev AVOptions:
-x .D...... Initial x coordinate. (from 0 to INT_MAX) (default 0)
-y .D...... Initial y coordinate. (from 0 to INT_MAX) (default 0)
-grab_x .D...... Initial x coordinate. (from 0 to INT_MAX) (default 0)
-grab_y .D...... Initial y coordinate. (from 0 to INT_MAX) (default 0)
-video_size .D...... A string describing frame size, such as 640x480 or hd720. (default "vga")
-framerate .D...... (default "ntsc")
-draw_mouse .D...... Draw the mouse pointer. (from 0 to 1) (default 1)
-follow_mouse .D...... Move the grabbing region when the mouse pointer reaches within specified
amount of pixels to the edge of region. (from -1 to INT_MAX) (default 0)
centered .D...... Keep the mouse pointer at the center of grabbing region when following.
-show_region .D...... Show the grabbing region. (from 0 to 1) (default 0)
-region_border .D...... Set the region border thickness. (from 1 to 128) (default 3)
参数包括了,采集分辨率-video_size , 支持鼠标光标 -draw_mouse, 跟中鼠标轨迹 -follow_mouse, 指定采集区域 -region_border等参数。
例子:
1)录制桌面
ffmpeg -f x11grab -framerate 25 -video_size 1366*768 -i :0.0 out.mp4
图像的分辨率,以实际电脑的支持来,我的因为是1366*768,所刚好录制下整个屏幕
我设置的参数为: 帧率为25帧 ,分辨率为 1366*768,采集设备为 0.0,输出的文件为out.mp4
2)带鼠标的录制桌面
ffmpeg -f x11grab -framerate 25 -video_size 1366*768 -follow_mouse 1 -i :0.0 out.mp4
3)指定录制区域
ffmpeg -f x11grab -framerate 25 -video_size 352*288 -i :0.0+300,200 out4.mp4
-i :0.0+300,200 制定了x坐标为300 y坐标为200
这样就可以录制桌面了。