MacOS摄像头媒体采集 - Mr.Ding

MacOS摄像头媒体采集

天天记事 656 / 2021-10-12 10:05:54

采集步骤如下

1. 下载安装ffmpeg

下载地址:https://www.ffmpeg.org/download.html#build-mac

或者通过mac命令安装(略)


2. 查看采集MAC视频音频设备

ffmpeg -hide_banner -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


3. 列出设备清单

ffmpeg -hide_banner -f avfoundation -list_devices true -i " "

执行结果

[AVFoundation input device @ 0x7f8780c2d080] AVFoundation video devices:
[AVFoundation input device @ 0x7f8780c2d080] [0] FaceTime高清摄像头(内建)
[AVFoundation input device @ 0x7f8780c2d080] [1] Capture screen 0
[AVFoundation input device @ 0x7f8780c2d080] AVFoundation audio devices:
[AVFoundation input device @ 0x7f8780c2d080] [0] 外置麦克风
[AVFoundation input device @ 0x7f8780c2d080] [1] MacBook Pro麦克风

解释

识别并列出两种类型设备,视频(video)设备和音频(audio)设备,注意编号(这样[0],[1]);视频的有两个设备,编号为0的是屏幕上方的摄像头,编号为1的是桌面;音频也有两个设备,一个是外置麦克风(我插这耳机),另外一个是MAC自带内置的麦克风,如果拔掉耳机,那么编号为0的将仅仅是内置麦克风。

值得注意的是,有人可能安装了录屏软件,这些录屏软件安装时可能会安装上一些其它虚拟设备,注意区分。


4. 查看视频采集分辨率

ffmpeg -hide_banner -f avfoundation -i 0

解释

-hide_banner 隐藏ffmpeg编译信息输出,-f 表示格式,后面的avfoundation表示av设备, -i 表示输入(采集来源),后面的0表示视频设备编号(通过查阅ffmpeg的api也可以使用设备名称)

输出结果:(非常多,这里列出部分)

[avfoundation @ 0x7fca5b808200] Selected framerate (29.970030) is not supported by the device.
[avfoundation @ 0x7fca5b808200] Supported modes:
[avfoundation @ 0x7fca5b808200] 640x480@[30.000030 30.000030]fps
[avfoundation @ 0x7fca5b808200] 640x480@[29.000049 29.000049]fps
[avfoundation @ 0x7fca5b808200] 640x480@[28.000067 28.000067]fps
...
[avfoundation @ 0x7fca5b808200] 640x480@[5.000000 5.000000]fps
[avfoundation @ 0x7fca5b808200] 640x480@[4.000000 4.000000]fps
[avfoundation @ 0x7fca5b808200] 640x480@[3.000000 3.000000]fps
[avfoundation @ 0x7fca5b808200] 640x480@[2.000000 2.000000]fps
[avfoundation @ 0x7fca5b808200] 640x480@[1.000000 1.000000]fps
...
[avfoundation @ 0x7fca5b808200] 1280x720@[6.000002 6.000002]fps
[avfoundation @ 0x7fca5b808200] 1280x720@[5.000000 5.000000]fps
[avfoundation @ 0x7fca5b808200] 1280x720@[4.000000 4.000000]fps
[avfoundation @ 0x7fca5b808200] 1280x720@[3.000000 3.000000]fps
[avfoundation @ 0x7fca5b808200] 1280x720@[2.000000 2.000000]fps
[avfoundation @ 0x7fca5b808200] 1280x720@[1.000000 1.000000]fps

5. 采集命令

采集为Mp4格式:

ffmpeg -hide_banner -f avfoundation -video_size 1280x720 -framerate 30 -i "0:0" -pix_fmt yuv420p -c:v libx264 -vf "scale=640:-1" -strict -2 out.mp4

解释

-video_size 1280x720,采集尺寸,注意这个是来设备源提供的支持的尺寸,不可以随意写,否则会提示设备不支持该尺寸。

-framerate 帧率,需要和上面的video_size 对应的值在设备清单里面能找到

-i 输入设备,注意了,如果仅配置为0也可,这样会采集不到声音,格式含义是 <视频设备ID>:<音频设备ID>,再次提醒,设备ID不一定是0,需要通过之前的命令预先查查(如果是可拔插设备,如摄像头,麦克风之类的,拔插后的设备序号可能发生变化,注意甄别)

-pix_fmt转换成目标后的像素格式,ffmpeg支持好几种,需要注意的是使用yuv420p可能会支持绝大多数设备,比如MacOS使用此配置就可以直接预览采集到的媒体了。

-c:v 转换后的视频部分编码(c表示codec,v表示video),这里采用libx264

-vf 转换后的视频尺寸,scale=640:-1,表示输出宽为640,高度自动匹配。注意,当自动算出来的数值不支持时(可能是奇数),需要我们手动修改为最接近的偶数值。如果想输出媒体尺寸和源媒体一致,可删除此配置选项

后面的自己查资料了


采集为m3u8格式:

ffmpeg -hide_banner -f avfoundation -video_size 1280x720 -framerate 30 -i "0:0" -pix_fmt yuv420p -c:v libx264 -hls_list_size 0 -hls_wrap 0 -strict -2 -vbsf h264_mp4toannexb -absf aac_adtstoasc -f hls index.m3u8

ffmpeg -hide_banner -f avfoundation -video_size 1280x720 -framerate 30 -i "0:0" -pix_fmt yuv420p -c:v libx264 -hls_enc true -hls_enc_key 21232f297a57a5a743894a0e4a801fc3 -hls_enc_key_url key.key -hls_list_size 0 -hls_wrap 0 -strict -2 -vbsf h264_mp4toannexb -absf aac_adtstoasc -f hls index-%06d.m3u8


两者都能采集到m3u8和ts文件,只是后者会对ts加密,加密算法和秘钥会保存在m3u8和key文件中,这里不做具体解释,给出部分有用的hls参数供参考,如下:


hls muxer AVOptions:
-start_number <int64> E........ set first number in the sequence (from 0 to I64_MAX) (default 0)
-hls_time <float> E........ set segment length in seconds (from 0 to FLT_MAX) (default 2)
-hls_init_time <float> E........ set segment length in seconds at init list (from 0 to FLT_MAX) (default 0)
-hls_list_size <int> E........ set maximum number of playlist entries (from 0 to INT_MAX) (default 5)
-hls_delete_threshold <int> E........ set number of unreferenced segments to keep before deleting (from 1 to INT_MAX) (default 1)
-hls_ts_options <string> E........ set hls mpegts list of options for the container format used for hls
-hls_vtt_options <string> E........ set hls vtt list of options for the container format used for hls
-hls_wrap <int> E........ set number after which the index wraps (will be deprecated) (from 0 to INT_MAX) (default 0)
-hls_allow_cache <int> E........ explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments (from INT_MIN to INT_MAX) (default -1)
-hls_base_url <string> E........ url to prepend to each playlist entry
-hls_segment_filename <string> E........ filename template for segment files
-hls_segment_size <int> E........ maximum size per segment file, (in bytes) (from 0 to INT_MAX) (default 0)
-hls_key_info_file <string> E........ file with key URI and key file path
-hls_enc <boolean> E........ enable AES128 encryption support (default false)
-hls_enc_key <string> E........ hex-coded 16 byte key to encrypt the segments
-hls_enc_key_url <string> E........ url to access the key to decrypt the segments
-hls_enc_iv <string> E........ hex-coded 16 byte initialization vector
-hls_subtitle_path <string> E........ set path of hls subtitles
-hls_segment_type <int> E........ set hls segment files type (from 0 to 1) (default mpegts)
mpegts E........ make segment file to mpegts files in m3u8
fmp4 E........ make segment file to fragment mp4 files in m3u8
-hls_fmp4_init_filename <string> E........ set fragment mp4 file init filename (default "init.mp4")
-hls_flags <flags> E........ set flags affecting HLS playlist and media file generation (default 0)
single_file E........ generate a single media file indexed with byte ranges
temp_file E........ write segment and playlist to temporary file and rename when complete
delete_segments E........ delete segment files that are no longer part of the playlist
round_durations E........ round durations in m3u8 to whole numbers
discont_start E........ start the playlist with a discontinuity tag
omit_endlist E........ Do not append an endlist when ending stream
split_by_time E........ split the hls segment by time which user set by hls_time
append_list E........ append the new segments into old hls segment list
program_date_time E........ add EXT-X-PROGRAM-DATE-TIME
second_level_segment_index E........ include segment index in segment filenames when use_localtime
second_level_segment_duration E........ include segment duration in segment filenames when use_localtime
second_level_segment_size E........ include segment size in segment filenames when use_localtime
periodic_rekey E........ reload keyinfo file periodically for re-keying
independent_segments E........ add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable
iframes_only E........ add EXT-X-I-FRAMES-ONLY, whenever applicable
-use_localtime <boolean> E........ set filename expansion with strftime at segment creation(will be deprecated ) (default false)
-strftime <boolean> E........ set filename expansion with strftime at segment creation (default false)
-use_localtime_mkdir <boolean> E........ create last directory component in strftime-generated filename(will be deprecated) (default false)
-strftime_mkdir <boolean> E........ create last directory component in strftime-generated filename (default false)
-hls_playlist_type <int> E........ set the HLS playlist type (from 0 to 2) (default 0)
event E........ EVENT playlist
vod E........ VOD playlist
-method <string> E........ set the HTTP method(default: PUT)
-hls_start_number_source <int> E........ set source of first number in sequence (from 0 to 2) (default generic)
generic E........ start_number value (default)
epoch E........ seconds since epoch
datetime E........ current datetime as YYYYMMDDhhmmss
-http_user_agent <string> E........ override User-Agent field in HTTP header
-var_stream_map <string> E........ Variant stream map string
-cc_stream_map <string> E........ Closed captions stream map string
-master_pl_name <string> E........ Create HLS master playlist with this name
-master_pl_publish_rate <int> E........ Publish master play list every after this many segment intervals (from 0 to UINT32_MAX) (default 0)
-http_persistent <boolean> E........ Use persistent HTTP connections (default false)
-timeout <duration> E........ set timeout for socket I/O operations (default -0.000001)
-ignore_io_errors <boolean> E........ Ignore IO errors for stable long-duration runs with network output (default false)
-headers <string> E........ set custom HTTP headers, can override built in default headers

就这样了,好运