FFmpeg采集设备

framerate:设置采集视频的帧率
list_formats:列举输入视频信号的信息
list_standards:列举标准信息(与standard配合使用)
timestamps:设置时间戳类型
ts:设置模拟器分析帧时使用的时间戳
use_libv4l2:使用第三方库libv4l2选项
使用FFmpeg采集Linux下的v4l2设备时,主要用来采集摄像头,而摄像头通常支持多种像素格式,有些还支持直接输出已经编码好的H.264数据。
如,输出输入设备的format信息:

ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
将摄像头采集为视频文件:

ffmpeg -hide_banner -s 1920x1080 -i /dev/video0 output.avi
4、Linux采集设备x11grab参数
ffmpeg -h demuxer=x11grab

draw_mouse:支持绘制鼠标光标
follw_mouse:跟踪鼠标轨迹数据
framerate:输入采集的视频帧率
show_region:获得输入桌面的指定区域
region_border:当show_region为1时,设置输入指定区域的边框的粗细程度
video_size:输入采集视频的分辨率
FFmpeg通过x11grab录制屏幕时,输入设备的设备名规则如下:
[ 主机名 ] : 显示编号 id. 屏幕编号 id [ + 起始 x 轴 , 起始 y 轴 ]
其中,主机名、起始x轴与起始y轴均为可选参数。

(1)桌面录制

ffmpeg -f x11grab -framerate 25 -video_size 1366x768 -i :0.0 out.mp4
设置输入帧率为25,图像分辨率为1366x768,采集的设备为“0.0”,输出文件为out.mp4。

(2)桌面录制指定起始位置

ffmpeg -f x11grab -framerate 25 -video_size 352x288 -i :0.0+300,200 out.mp4
通过参数“:0.0+300,200”指定x坐标为200,y坐标为200,实现指定区域的录制。

(3)桌面录制带鼠标记录的视频

ffmpeg -f x11grab -video_size 1366x768 -follow_mouse 1 -i :0.0 out.mp4
通过“follow_mouse”参数指定视频录制中带鼠标。

FFmpeg中OS X设备操作

在FFmpeg中采集OS X系统的输入输出设备,常规方式采用的是OS X的avfoundation设备进行采集。

ffmpeg -h demuxer=avfoundation
Demuxer avfoundation [AVFoundation input device]:
AVFoundation input device AVOptions:
-list_devices .D… list available devices (from 0 to 1) (default false)
true .D…
false .D…
-video_device_index .D… select video device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
-audio_device_index .D… select audio device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
-pixel_format <pix_fmt> .D… set pixel format (default yuv420p)
-framerate <video_rate> .D… set frame rate (default “ntsc”)
-video_size <image_size> .D… set video size
-capture_cursor .D… capture the screen cursor (from 0 to 1) (default 0)
-capture_mouse_clicks .D… capture the screen mouse clicks (from 0 to 1) (default 0)
1、OS X下查看设备列表
ffmpeg -devices
Devices:
D. = Demuxing supported
.E = Muxing supported

D avfoundation AVFoundation input device
D lavfi Libavfilter virtual input device
E sdl,sdl2 SDL2 output device
如上信息,分为两大部分:

解封装或封装的支持情况
设备列表
2、OS X下设备采集
枚举avfoundation支持的输入设备:

ffmpeg -f avfoundation -list_devices true -i “”
[AVFoundation input device @ 0x7fea535022c0] AVFoundation video devices:
[AVFoundation input device @ 0x7fea535022c0] [0] FaceTime 高清摄像头(内建)
[AVFoundation input device @ 0x7fea535022c0] [1] Capture screen 0
[AVFoundation input device @ 0x7fea535022c0] AVFoundation audio devices:
[AVFoundation input device @ 0x7fea535022c0] [0] Built-in Microphone
视频输入设备:

[0] FaceTime 高清摄像头(内建)
[1] Capture screen 0
音频输入设备:
[0] Built-in Microphone
avfoundation除了枚举了物理摄像头(FaceTime 高清摄像头)之外,还包括了1个虚拟设备,即Capture screen 0设备代表了OS X桌面。

(1)采集内置摄像头

ffmpeg -f avfoundation -i “FaceTime 高清摄像头(内建)” out.mp4
Input #0, avfoundation, from ‘FaceTime 高清摄像头(内建)’:
Duration: N/A, start: 33714.868500, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1280x720, 12 tbr, 1000k tbn, 1000k tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x7fb10b87c600] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fb10b87c600] profile High 4:2:2, level 3.1, 4:2:2 8-bit
[libx264 @ 0x7fb10b87c600] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=12 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to ‘out.mp4’:
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv422p, 1280x720, q=-1–1, 12 fps, 12288 tbn, 12 tbc
Metadata:
encoder : Lavc58.35.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame= 11 fps=0.0 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 18 fps= 16 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 24 fps= 14 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 31 fps= 14 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 37 fps= 13 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 44 fps= 13 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed=
frame= 51 fps= 13 q=26.0 size= 0kB time=-00:00:00.16 bitrate=N/A speed=N
frame= 58 fps= 13 q=26.0 size= 0kB time=00:00:00.41 bitrate= 0.9kbits/
frame= 65 fps= 13 q=26.0 size= 0kB time=00:00:01.00 bitrate= 0.4kbits/
frame= 71 fps= 13 q=26.0 size= 0kB time=00:00:01.50 bitrate= 0.3kbits/frame
frame= 78 fps= 13 q=26.0 size= 0kB time=00:00:02.08 bitrate= 0.2kbits/
frame= 84 fps= 13 q=26.0 size= 256kB time=00:00:02.58 bitrate= 811.9kbits/
frame= 90 fps= 13 q=26.0 size= 256kB time=00:00:03.08 bitrate= 680.3kbits/
frame= 97 fps= 13 q=26.0 size= 512kB time=00:00:03.66 bitrate=1144.0kbits/
frame= 103 fps= 12 q=26.0 size= 512kB time=00:00:04.16 bitrate=1006.7kbits/
frame= 110 fps= 12 q=26.0 size= 768kB time=00:00:04.75 bitrate=1324.6kbits/
frame= 113 fps=9.6 q=-1.0 Lsize= 1950kB time=00:00:09.16 bitrate=1742.6kbits/s speed=0.778x
video:1948kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.110501%

(2)采集OS X桌面

ffmpeg -f avfoundation -i “Capture screen 0” -r:v 30 out1.mp4
Input #0, avfoundation, from ‘Capture screen 0’:
Duration: N/A, start: 34246.268167, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 2560x1600, 1000k tbr, 1000k tbn, 1000k tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x7fea3f2b7400] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fea3f2b7400] profile High 4:2:2, level 5.0, 4:2:2 8-bit
[libx264 @ 0x7fea3f2b7400] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to ‘out1.mp4’:
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv422p, 2560x1600, q=-1–1, 30 fps, 15360 tbn, 30 tbc
Metadata:
encoder : Lavc58.35.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame= 17 fps=0.0 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed= 0
frame= 33 fps= 32 q=0.0 size= 0kB time=00:00:00.00 bitrate=N/A speed= 0
frame= 55 fps= 30 q=29.0 size= 256kB time=00:00:00.06 bitrate=31432.2kbits/
frame= 81 fps= 30 q=29.0 size= 256kB time=00:00:00.93 bitrate=2247.2kbits/s
frame= 98 fps= 30 q=29.0 size= 256kB time=00:00:01.50 bitrate=1398.3kbits/s
frame= 114 fps= 30 q=29.0 size= 256kB time=00:00:02.03 bitrate=1031.5kbits/s
frame= 130 fps= 30 q=29.0 size= 256kB time=00:00:02.56 bitrate= 817.2kbits/s
frame= 148 fps= 30 q=29.0 size= 256kB time=00:00:03.16 bitrate= 662.4kbits/s
frame= 148 fps= 21 q=-1.0 Lsize= 422kB time=00:00:04.83 bitrate= 714.5kbits/
s dup=95 drop=0 speed=0.691x
video:419kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.606895%

参数Capture screen 0指定了桌面0为输入设备,与x11grab的方式类似,我们还可以录制鼠标,在OS X上通过capture_cursor来指定:

ffmpeg -f avfoundation -capture_cursor 1 -i “Capture screen 0” -r:v 30 out2.mp4

(3)采集麦克风

ffmpeg -f avfoundation -i “0:0” out.aac
通过参数“0:0”分别指定第0个视频设备与第0个音频设备,输出信息如下:

Input #0, avfoundation, from ‘0:0’:
Duration: N/A, start: 35022.728322, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1280x720, 23.92 tbr, 1000k tbn, 1000k tbc
Stream #0:1: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (pcm_f32le (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, adts, to ‘out.aac’:
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.35.100 aac
size= 71kB time=00:00:04.50 bitrate= 129.6kbits/s speed=1.05x
video:0kB audio:70kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.896118%
如上信息所示,采集的数据包含了视频rawvideo数据和音频pcm_f32le数据,但是编码输出只有AAC的编码的数据。
除了以上方法,还可以使用设备索引参数指定设备采集:

ffmpeg -f avfoundation -video_deivce_index 0 -i “:0” out.aac
ffmpeg -f avfoundation -video_deivce_index 0 -audio_device_index 0 out.aac

Windows采集设备的主要方式是dshow、vfwcap、gdigrab,其中dshow可以用来抓取摄像头、采集卡、麦克风等,vfwcap主要用来采集摄像头类设备,gdigrab则是抓取Windows窗口程序。

ffmpeg -hide_banner -devices
11.png
1、FFmpeg使用dshow采集音视频设备
(1)使用dshow枚举设备

ffmpeg -f dshow -list_devices true -i dummy
(2)使用dshow展示摄像头

ffmpeg -f dshow -video_size 1280x720 -i video=“Integrated Cmera”
(3)将摄像头数据保存为mp4文件

ffmpeg -f dshow i video=“Integrated Cmera” -f dshow -i audio=“virtual-audio-capturer” out.mp4
2、FFmpeg使用vfwcap采集视频设备
(1)使用vfwcap枚举支持采集的设备

ffmpeg -f vfwcap -i list
(2)使用vfwcap生成mp4文件

ffmpeg -f vfwcap -i 0 -r 25 -vcodec libx264 out.mp4
3、FFmpeg使用gdigrab采集窗口
ffmpeg -h demuxer=gdigrab

(1)使用gdigrab采集整个桌面

ffmpeg -f gdigrab -framerate 6 -i desktop out.mp4

(2)使用gdigrab采集某个窗口

ffmpeg -f gdigrab -framerate 6 -i title=tools out1.mp4

(3)使用gdigrab录制带偏移量的视频

ffmpeg -f gdigrab -framerate 6 -offset_x 50 -offset_y 50 -video_size 400x400 -i title=ffmpeg-4.1 out2.mp4

通过offset_x和offset_y分别指定x和y坐标的偏移,并且需要指定video_size,否则参数无效,另外在录制期间,应该尽量避免调整录制窗口的大小,否则可能会导致画面异常。

作者:Goning
链接:https://www.jianshu.com/p/a70afe5bf229
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值