5

ffmpeg之avformat_alloc_output_context2 - yanghehuanglu

 6 months ago
source link: https://www.cnblogs.com/yhfs/p/18025080
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ffmpeg之avformat_alloc_output_context2

函数原型:

  查找根据format_name或者filename或者oformat输出类型,并且初始化ctx结构。

ctx:      AVFormatContext结构体,ffmpeg核心结构体,会在函数内部给ctx分配AVFormatContext空间并初始化。

oformat:    指定输出格式的 AVOutputFormat 结构体指针。可以使用 NULL,此时将根据 format_name 自动选择输出格式。

format_name:  输出格式名称

filename:    输出文件/URL名称

  成功:0;失败:非0

工作流程:

1. 首先判断oformat是否为NULL;

2. 如果oformat不为NULL;

  直接初始化ctx结构((*ctx)->oformat, 保存filename到(*ctx)->url);

3. 如果oformat为NULL:

  format_name不为NULL:会循环查找muxer_list结构成员的name成员是否与format_name相同;

  filename不为NULL:会循环查找muxer_list结构成员的extensions成员与filename的后缀是否相同;

  找到后初始化ctx结构((*ctx)->oformat, 保存filename到(*ctx)->url); 如果没找到的话释放分配的ctx空间,返回错误码!

  有关muxer_list的信息参考后续代码;

  那么会产生一个问题:如果format_name和filename都不为NULL呢?当然如果format_name = "flv"和filename="file.flv"这是没问题的,肯定找到同一个结构;那么如果format_name = "flv"和filename="file.mp4"呢?到底以谁为准呢?

  其实在avformat_alloc_output_context2函数内部有一个评分机制:format_name是100分;filename是5分;

  如果匹配到filename会获得5分,继续循环匹配;如果又匹配到format_name获得100分;100分大于5分所以以format_name的类型为准;

  也就是以format_name类型优先,mime_type次之(avformat_alloc_output_context2不考虑),filename最后。

muxer_list结构

muxer_list的成员结构(&ff_flv_muxer为例)

mime_type

  MIME类型:是一种标准化的互联网媒体类型,用于标识在互联网上传输的数据的格式。每种文件都有一个唯一的 MIME 类型,它描述了文件的内容和格式。MIME 类型通常由两个部分组成:主类型和子类型,中间由斜杠分隔。avformat_alloc_output_context2函数不作考虑!

  常见的 MIME 类型包括:

  文本文件:text/plain

  HTML 文件:text/html

  图片文件:image/jpeg、image/png

  音频文件:audio/mpeg

  视频文件:video/mp4

  应用程序文件:application/pdf、application/json


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK