av_find_input_format 和 AVInputFormat 的关系

news/2025/3/1 20:00:07/

1. av_find_input_formatAVInputFormat 的关系

av_find_input_format 是 FFmpeg 中的一个函数,用于根据输入格式的名称(如 "mp4""wav""avfoundation" 等)查找对应的输入格式结构体 AVInputFormat

  • AVInputFormat 是一个结构体,表示一种输入格式(如 MP4 文件格式、WAV 文件格式、摄像头输入设备等)。
  • av_find_input_format 是一个工具函数,用于从 FFmpeg 的全局注册表中查找指定名称的 AVInputFormat

2. AVInputFormat 的作用

AVInputFormat 是 FFmpeg 中用于描述输入格式的核心结构体。它定义了输入格式的名称、描述以及如何处理该格式的函数指针。

2.1 AVInputFormat 的定义

以下是 AVInputFormat 的简化定义:

typedef struct AVInputFormat {const char *name;               // 输入格式的名称(如 "mp4"、"wav"、"avfoundation")const char *long_name;          // 输入格式的详细描述(如 "MP4 (MPEG-4 Part 14)")int flags;                      // 格式的标志位const char *extensions;         // 支持的文件扩展名(如 "mp4, mov")const struct AVCodecTag *codec_tag;const AVClass *priv_class;      // 私有选项的类const AVOption *priv_options;   // 私有选项int raw_codec_id;               // 原始数据的默认编解码器int priv_data_size;             // 私有数据的大小// 打开输入文件的回调函数int (*read_probe)(const AVProbeData *);int (*read_header)(struct AVFormatContext *);int (*read_packet)(struct AVFormatContext *, AVPacket *);int (*read_close)(struct AVFormatContext *);int (*read_seek)(struct AVFormatContext *, int, int64_t, int);...
} AVInputFormat;
2.2 AVInputFormat 的关键字段
  • name

    • 输入格式的名称(如 "mp4""wav""avfoundation")。
    • 用于通过 av_find_input_format 查找输入格式。
  • long_name

    • 输入格式的详细描述(如 "MP4 (MPEG-4 Part 14)")。
  • extensions

    • 支持的文件扩展名(如 "mp4, mov")。
  • read_probe

    • 用于检测输入数据是否属于该格式的回调函数。
  • read_header

    • 用于读取输入文件头部信息的回调函数。
  • read_packet

    • 用于读取输入数据包的回调函数。
  • read_close

    • 用于关闭输入文件的回调函数。

3. av_find_input_format 的作用

av_find_input_format 是一个工具函数,用于从 FFmpeg 的全局注册表中查找指定名称的 AVInputFormat

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

4. av_find_input_formatAVInputFormat 的使用

4.1 使用 av_find_input_format 查找输入格式

以下是一个使用 av_find_input_format 查找输入格式的示例:

import Foundation
import FFmpegclass 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
    

4.2 使用 av_find_input_format 打开输入设备

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

import Foundation
import FFmpegclass 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>? = nilif 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()
}

5. 注意事项

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

6. 总结

  • AVInputFormat 的作用

    • 描述输入格式的名称、扩展名、处理函数等信息。
    • 定义如何处理特定的输入格式。
  • av_find_input_format 的作用

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

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

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


http://www.ppmy.cn/news/1575857.html

相关文章

《mysql篇》--JDBC编程

JDBC是什么 JDBC就是Java DataBase Connectivity的缩写&#xff0c;翻译过来就很好理解了&#xff0c;就是java连接数据库。所以顾名思义&#xff0c;JDBC就是一种用于执行SQL语句的JavaApl&#xff0c;是Java中的数据库连接规范。为了可以方便的用Java连接各种数据库&#xff…

本地部署deepseek大模型后使用c# winform调用(可离线)

介于最近deepseek的大火&#xff0c;我就在想能不能用winform也玩一玩本地部署&#xff0c;于是经过查阅资料&#xff0c;然后了解到ollama部署deepseek,最后用ollama sharp NUGet包来实现winform调用ollama 部署的deepseek。 本项目使用Vs2022和.net 8.0开发&#xff0c;ollam…

JAVA多商户家政同城上门服务预约服务抢单派单+自营商城系统支持小程序+APP+公众号+h5

JAVA多商户家政同城上门服务系统&#xff1a;SpringBootUniApp全栈解决方案与行业革新 一、家政行业痛点与数字化升级需求 2025年中国家政市场规模预计突破1.5万亿元&#xff0c;但传统模式仍面临四大核心痛点&#xff1a; 服务匹配低效&#xff1a;30%用户因等待时间过长流…

Docker02 - 深入理解Docker

深入理解Docker 文章目录 深入理解Docker一&#xff1a;Docker镜像原理1&#xff1a;镜像加载原理1.1&#xff1a;unionFS1.2&#xff1a;加载原理 2&#xff1a;分层理解 二&#xff1a;容器数据卷详解1&#xff1a;什么是容器数据卷2&#xff1a;使用数据卷3&#xff1a;具名…

QT和有道词典有冲突,导致内存溢出,闪退。

提示&#xff1a;本文为学习记录&#xff0c;若有疑问&#xff0c;请联系作者。 前言 具体详细查看此博主&#xff1a;原文链接 在使用Qt Designer时&#xff0c;如果开启了有道词典&#xff0c;会导致Qt Designer崩溃。估计应该是把有道词典屏幕取词功能打开后&#xff0c;有…

GPT-4.5震撼登场,AI世界再掀波澜!

开场&#xff1a;科技圈的重磅炸弹 在科技飞速发展的今天&#xff0c;人工智能领域的每一次突破都如一颗投入湖面的石子&#xff0c;激起层层涟漪。而近日&#xff0c;OpenAI 推出 GPT-4.5 的消息&#xff0c;无疑是一颗重磅炸弹&#xff0c;瞬间在全球范围内掀起了惊涛骇浪&am…

黑马头条启动文章微服务时报错Error creating bean with name ‘buildMinioClient‘的原因及解决方案

最近在做黑马的头条项目&#xff0c;做完登录功能后启动文章微服务时就一直报错&#xff0c;查了半天的bug最后发现是引入依赖的问题 报错如图 刚开始觉得我没有引入这个bean啊&#xff0c;后来查看引入的依赖项才知道他在黑马提供的一个依赖jar中&#xff0c;引入miniIO这个b…

windows下适用msvc编译ffmpeg 适用于ffmpeg-7.1

需要的工具: visual studio 2019 (可以是其他版本&#xff0c;只是本人电脑上装的为2019) msys2 ffmpeg-7.1源码 1. 修改msys2_shell.cmd 在msys2目录修改msys2_shell.cmd 打开后找到行set MSYS2_PATH_TYPEinherit 删除开头的rem 2. 运行msys2 运行x64 Native Tools Command …