ffmpeg av_find_input_format的作用

1. av_find_input_format 的作用

av_find_input_format 是 FFmpeg 中的一个函数,用于根据输入格式的名称(如 "mp4"、"wav"、"avfoundation" 等)查找对应的输入格式结构体(AVInputFormat)。它的主要作用是帮助 FFmpeg 确定如何处理特定的输入源(文件或设备)。


1.1 主要功能

  1. 查找输入格式:

    • 根据输入格式的名称(字符串)查找对应的 AVInputFormat。
    • 例如,"mp4" 对应 MP4 文件格式,"wav" 对应 WAV 文件格式,"avfoundation" 对应 macOS/iOS 的音视频设备。
  2. 返回输入格式的结构体:

    • 如果找到对应的输入格式,返回一个指向 AVInputFormat 的指针。
    • 如果未找到对应的输入格式,返回 NULL。
  3. 用于打开输入设备或文件:

    • 在使用 avformat_open_input 打开输入文件或设备时,可以通过 AVInputFormat 指定输入格式。

1.2 函数签名

c 复制代码
const AVInputFormat *av_find_input_format(const char *short_name);
参数
  • short_name :
    • 输入格式的名称(字符串)。
    • 例如:"mp4"、"wav"、"avfoundation"、"dshow" 等。
返回值
  • 成功 :
    • 返回一个指向 AVInputFormat 的指针。
  • 失败 :
    • 如果未找到对应的输入格式,返回 NULL。

2. 使用场景

2.1 指定输入格式

在某些情况下,FFmpeg 无法自动检测输入格式(例如,使用设备作为输入时),需要显式指定输入格式。这时可以使用 av_find_input_format 查找输入格式。

2.2 打开输入设备

当使用音视频设备(如摄像头、麦克风、屏幕捕获等)作为输入时,需要通过 av_find_input_format 查找设备的输入格式。


3. Swift 示例代码

以下是使用 Swift 调用 FFmpeg 的 av_find_input_format 的示例代码。


3.1 查找输入格式
swift 复制代码
import Foundation
import FFmpeg

class FFmpegInputFormatManager {
    static func findInputFormat(formatName: String) {
        // 查找输入格式
        guard let inputFormat = av_find_input_format(formatName) else {
            print("Input format '\(formatName)' not found")
            return
        }

        // 打印输入格式信息
        if let name = inputFormat.pointee.name, let longName = inputFormat.pointee.long_name {
            print("Found input format: \(String(cString: name)) (\(String(cString: longName)))")
        }
    }
}

// 调用示例
FFmpegInputFormatManager.findInputFormat(formatName: "avfoundation") // macOS 的音视频设备
FFmpegInputFormatManager.findInputFormat(formatName: "wav")          // WAV 文件格式
FFmpegInputFormatManager.findInputFormat(formatName: "invalid")      // 无效格式
输出示例
  1. 如果找到输入格式:

    复制代码
    Found input format: avfoundation (AVFoundation input device)
  2. 如果未找到输入格式:

    复制代码
    Input format 'invalid' not found

3.2 使用设备录制音频

以下是一个使用 av_find_input_format 和 avfoundation 设备录制音频的完整示例(适用于 macOS):

swift 复制代码
import Foundation
import FFmpeg

class AudioRecorder {
    private var formatContext: UnsafeMutablePointer<AVFormatContext>?

    func startRecording() {
        // 注册所有设备
        avdevice_register_all()

        // 查找输入格式
        guard let inputFormat = av_find_input_format("avfoundation") else {
            print("avfoundation not found")
            return
        }

        // 打开音频设备
        var formatContext: UnsafeMutablePointer<AVFormatContext>? = nil
        if avformat_open_input(&formatContext, ":0", inputFormat, nil) < 0 {
            print("Failed to open input device")
            return
        }

        self.formatContext = formatContext

        // 打印设备信息
        av_dump_format(formatContext, 0, ":0", 0)

        print("Recording started...")
    }

    func stopRecording() {
        guard let formatContext = formatContext else { return }

        // 释放资源
        avformat_close_input(&formatContext)
        print("Recording stopped.")
    }
}

// 调用示例
let recorder = AudioRecorder()
recorder.startRecording()

// 停止录音(可以在适当的时机调用)
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
    recorder.stopRecording()
}
代码说明
  1. avdevice_register_all() :
    • 注册所有设备。
  2. av_find_input_format("avfoundation") :
    • 查找 avfoundation 输入格式,用于访问 macOS 的音视频设备。
  3. avformat_open_input :
    • 打开音频设备 :0(第一个音频输入设备)。
  4. av_dump_format :
    • 打印设备的详细信息。

4. 注意事项

4.1 输入格式名称
  • 输入格式名称是区分大小写的。例如,"mp4" 和 "MP4" 是不同的。
  • 常见的输入格式名称包括:
    • 文件格式:"mp4"、"wav"、"flv" 等。
    • 设备格式:
      • macOS/iOS:"avfoundation"
      • Windows:"dshow"(DirectShow)
      • Linux:"v4l2"(Video4Linux2)
4.2 平台相关性
  • 某些输入格式是平台相关的。例如:
    • avfoundation 仅适用于 macOS/iOS。
    • dshow 仅适用于 Windows。
    • v4l2 仅适用于 Linux。
4.3 错误处理
  • 如果 av_find_input_format 返回 NULL,说明输入格式名称无效或不支持。
  • 在调用 avformat_open_input 时,传递无效的 AVInputFormat 可能会导致程序崩溃。

5. 总结

  • av_find_input_format 的作用:

    • 根据输入格式名称查找对应的 AVInputFormat。
    • 用于指定输入格式,特别是在使用设备作为输入时。
  • 常见使用场景:

    • 打开音视频设备(如摄像头、麦克风、屏幕捕获等)。
    • 指定文件格式(如 MP4、WAV 等)。
  • 注意事项:

    • 输入格式名称是区分大小写的。
    • 某些输入格式是平台相关的。

通过 av_find_input_format,你可以轻松查找和使用 FFmpeg 支持的输入格式。

相关推荐
程序员老陆3 小时前
FFmpeg 日志级别深度解析:从 AV_LOG_ERROR 到 AV_LOG_TRACE 的完整指南
ffmpeg·音视频
用户21816970493016 小时前
FFmpeg (一)使用opencv实现马赛克
ffmpeg
cpp_learner16 小时前
FFmpeg RTSP播放时钟实现揭秘
ffmpeg
程序员老陆8 天前
WHIP 与 WHEP:WebRTC 直播的标准化入口与出口
ffmpeg·音视频·webrtc·whip·whep
可乐鸡翅yeah_9 天前
混合内容 Mixed‑Content 安全策略,HLS HTTPS 页面加载 HTTP 资源排错实战
运维·ffmpeg·音视频·媒体·m3u8
刘广睿10 天前
视频导出为什么发灰?色彩空间与色彩范围(BT.601/709/2020)的 ffmpeg 转换实战
ffmpeg·音视频·视频处理·色彩空间·素材管理
yivifu10 天前
精选FFmpeg 实用音频视频转换命令集
ffmpeg·音视频转换
风哥2号14 天前
数据库教程FGMT02‑生产环境Linux+Oracle19c安装配置与项目实战
linux·数据库·ffmpeg
Bruce_Liuxiaowei14 天前
录屏片段无损合并:从 ffmpeg concat 原理到 Python 自动化脚本
python·ffmpeg·自动化
工具派16 天前
口播视频怎么自动剪掉冷场?视频静音删除的阈值实测
ffmpeg·音视频·视频