ios 开发

FFmpeg音视频录制

2022-12-28  本文已影响0人  iOS小洁

一、查看可用设备

ffmpeg -devices

Mac的输出结果如下所示:

Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  avfoundation    AVFoundation input device
 D  lavfi           Libavfilter virtual input device
  E sdl,sdl2        SDL2 output device

二、查看avfoundation支持的设备

# 查看dshow支持的设备
ffmpeg -f avfoundation -list_devices true -i ''

输出结果如下

AVFoundation video devices:
 [0] FaceTime高清摄像头(内建)
 [1] Capture screen 0
AVFoundation audio devices:
 [0] MS-T800
 [1] Edu Audio Device
 [2] MacBook Pro麦克风

三、查看avfoundation参数

通过命令查看一下avfoundation可以使用的参数

# 从ffmpeg -devices命令的结果可以看得出来:avfoundation属于demuxer,而不是muxer
ffmpeg -h demuxer=avfoundation

打印如下

Demuxer avfoundation [AVFoundation input device]:
AVFoundation indev AVOptions:
  -list_devices      <boolean>    .D......... list available devices (default false)
  -video_device_index <int>        .D......... select video device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
  -audio_device_index <int>        .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    <boolean>    .D......... capture the screen cursor (default false)
  -capture_mouse_clicks <boolean>    .D......... capture the screen mouse clicks (default false)
  -capture_raw_data  <boolean>    .D......... capture the raw data from device connection (default false)
  -drop_late_frames  <boolean>    .D......... drop frames that are available later than expected (default true)

四、指定设备进行录音

# 在Mac上通过编号指定设备
ffmpeg -f avfoundation -i :2 out.wav
# :0表示使用0号音频设备
# 0:2表示使用0号视频设备和2号音频设备

五、指定设备进行录制视频

# 使用0号视频设备
ffmpeg -f avfoundation -i 0 out.yuv

然后你可能会遇到一个错误:这个设备(摄像头)不支持29.970030的帧率。

Selected framerate (29.970030) is not supported by the device

重新设置个30的帧率试试。

ffmpeg -f avfoundation -framerate 30 -i 0 out.yuv

然后你会看到以下提示信息。

Selected pixel format (yuv420p) is not supported by the input device.
Supported pixel formats:
  uyvy422
  yuyv422
  nv12
  0rgb
  bgr0
Overriding selected pixel format to use uyvy422 instead.

与此同时,也成功开始采集摄像头的视频数据了。

Input #0, avfoundation, from '0':
    Stream #0:0: Video: rawvideo, uyvy422, 1280x720
 
Output #0, rawvideo, to 'out.yuv':
    Stream #0:0: Video: rawvideo, uyvy422, 1280x720, 30 fps

播放录制好的YUV。

ffplay -video_size 1280x720 -pixel_format uyvy422 -framerate 30 out.yuv
上一篇 下一篇

猜你喜欢

热点阅读