FFmpeg 常用API
一、通用API
1.1 av_register_all()
初始化 libavformat 和注册所有的复用器、解复用器和协议处理器。如果不调用这个函数,可以调用下面的三个函数来选择支持的格式。
- 注册复用器的函数是
av_register_output_format()。 - 注册解复用器的函数是
av_register_input_format()。 - 注册协议处理器的函数是
ffurl_register_protocol()。
注:FFmpeg4.0 以上的版本,这个函数已经被废弃。
1.2 内存的分配和释放(av_malloc()、av_free()等)
av_malloc() 和 av_free() 都是简单的封装了系统函数 malloc() 和free(),并做了一些错误检查工作。同理的还有 av_realloc()。
1.3 avcodec_find_encoder() 和 avcodec_find_decoder()
avcodec_find_encoder() 用于查找 FFmpeg 的编码器,avcodec_find_decoder() 用于查找 FFmpeg 的解码器,声明都位于 libavcodec\avcodec.h。其原型如下:
// 函数的参数是一个编码器的ID,返回查找到的编码器(没有找到就返回NULL)。
AVCodec *avcodec_find_encoder(enum AVCodecID id);
// 函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。
AVCodec *avcodec_find_decoder(enum AVCodecID id);
1.4 avcodec_open2()
用于初始化一个视音频编解码器的 AVCodecContext,声明位于 libavcodec\utils.c。其原型如下:
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
- avctx:需要初始化的 AVCodecContext。
- codec:输入的AVCodec。
- options:一些选项。例如使用libx264编码的时候,“preset”,“tune”等都可以通过该参数设置。
1.5 avcodec_close()
用于关闭编码器,声明位于 libavcodec\utils.c。其原型如下:
int avcodec_close(AVCodecContext *avctx)
该函数只有一个参数,就是需要关闭的编码器的 AVCodecContext。
二、解码API
下面介绍解码需要用到的几个函数,声明都位于 libavformat\avformat.h。
2.1 avformat_open_input()
打开输出的流和读取头信息。其原型如下:
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
- ps:函数调用成功之后处理过的 AVFormatContext 结构体。
- url:打开的视音频流的 URL。
- fmt:强制指定 AVFormatContext 中 AVInputFormat 的。这个参数一般情况下可以设置为 NULL,这样 FFmpeg 可以自动检测 AVInputFormat。
- options:附加的一些选项,一般情况下可以设置为 NULL。
函数执行成功的话,其返回值大于等于 0。
2.2 avformat_find_stream_info()
读取音视频数据来获取一些相关的信息。其原型如下:
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2.3 av_read_frame
读取码流中的音频若干帧或者视频一帧。例如,解码视频的时候,每解码一个视频帧,需要先调用 av_read_frame() 获得一帧视频的压缩数据,然后才能对该数据进行解码。其原型如下:
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
2.4 avformat_close_input()
关闭打开的流。其原型如下:
void avformat_close_input(AVFormatContext **s)
三、编码API
在基于 FFmpeg 的音视频编码器程序中,avformat_alloc_output_context2() 函数通常是第一个调用的函数(除了组件注册函数 av_register_all())。另外介绍 FFmpeg 的写文件用到的 3 个函数,声明都位于 libavformat\avformat.h:
- av_write_frame() 用于写视频数据;
- avformat_write_header() 用于写视频文件头;
- av_write_trailer() -用于写视频文件尾。
3.1 avformat_alloc_output_context2()
初始化一个用于输出的 AVFormatContext 结构体。其原型如下:
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat * oformat, const char * format_name, const char * filename)
3.2 avformat_write_header()
分配一个 stream 的私有数据而且写 stream 的 header 到一个输出的媒体文件。其原型如下:
int avformat_write_header(AVFormatContext *s, AVDictionary ** options)
3.3 av_write_frame()
用于输出一帧视音频数据。其原型如下:
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
参数 s 为用于输出的 AVFormatContext,参数 pkt 为等待输出的 AVPacket。
3.4 av_write_trailer()
用于输出文件尾。其原型如下:
int av_write_trailer(AVFormatContext *s)
它只需要指定一个参数,即用于输出的 AVFormatContext,函数正常执行后返回值等于 0。
四、图像处理API
libswscale 是一个主要用于处理图片像素数据的类库。可以完成图片像素格式的转换,图片的拉伸等工作。libswscale 常用的函数数量很少,一般情况下就3个,声明都位于 libswscale\swscale.h:
sws_getContext():分配和返回一个SwsContext。
sws_scale():处理图像数据。
sws_freeContext():释放一个SwsContext。
其中 sws_getContext() 也可以用 sws_getCachedContext() 取代。
4.1 sws_getContext()
分配和返回一个 SwsContext。其原型如下:
struct SwsContext* sws_getContext ( int srcW,
int srcH,
enum AVPixelFormat srcFormat,
int dstW,
int dstH,
enum AVPixelFormat dstFormat,
int flags,
SwsFilter * srcFilter,
SwsFilter * dstFilter,
const double * param
)
4.2 sws_scale()
处理图像数据。其原型如下:
int sws_scale(struct SwsContext *c,
const uint8_t * const srcSlice[],
const int srcStride[], int srcSliceY,
int srcSliceH, uint8_t *const dst[],
const int dstStride[]) )
4.3 sws_freeContext()
释放一个 SwsContext。其原型如下:
void sws_freeContext(struct SwsContext *swsContext)
五、其它API
5.1 日志输出系统(av_log()等)
av_log() 是 FFmpeg 中输出日志的函数。一般情况下 FFmpeg 类库的源代码中是不允许使用 printf() 这种的函数的,所有的输出一律使用 av_log()。av_log() 的声明位于 libavutil\log.h,其原型如下:
void av_log(void* avcl, int level, const char *fmt, ...)
函数最后一个参数是 “…”。
在 C 语言中,在函数参数数量不确定的情况下使用 “…” 来代表参数。例如 printf() 的原型定义为:int printf (const char*, ...)。
参考:
雷霄骅大神的FFmpeg的库函数源代码分析文章列表:
【通用】
FFmpeg 源代码简单分析:av_register_all()
FFmpeg 源代码简单分析:avcodec_register_all()
FFmpeg 源代码简单分析:内存的分配和释放(av_malloc()、av_free()等)
FFmpeg 源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)
FFmpeg 源代码简单分析:av_find_decoder()和av_find_encoder()
FFmpeg 源代码简单分析:avcodec_open2()
FFmpeg 源代码简单分析:avcodec_close()
【解码】
图解FFMPEG打开媒体的函数avformat_open_input
FFmpeg 源代码简单分析:avformat_open_input()
FFmpeg 源代码简单分析:avformat_find_stream_info()
FFmpeg 源代码简单分析:av_read_frame()
FFmpeg 源代码简单分析:avcodec_decode_video2()
FFmpeg 源代码简单分析:avformat_close_input()
【编码】
FFmpeg 源代码简单分析:avformat_alloc_output_context2()
FFmpeg 源代码简单分析:avformat_write_header()
FFmpeg 源代码简单分析:avcodec_encode_video()
FFmpeg 源代码简单分析:av_write_frame()
FFmpeg 源代码简单分析:av_write_trailer()
【其它】
FFmpeg源代码简单分析:日志输出系统(av_log()等)
FFmpeg源代码简单分析:结构体成员管理系统-AVClass
FFmpeg源代码简单分析:结构体成员管理系统-AVOption
FFmpeg源代码简单分析:libswscale的sws_getContext()
FFmpeg源代码简单分析:libswscale的sws_scale()
FFmpeg源代码简单分析:libavdevice的avdevice_register_all()
FFmpeg源代码简单分析:libavdevice的gdigrab
FFmpeg 常用API的更多相关文章
- javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?
通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...
- < python音频库:Windows下pydub安装配置、过程出现的问题及常用API >
< python音频库:Windows下pydub安装配置.过程出现的问题及常用API > 背景 刚从B站上看过倒放挑战之后也想体验下,心血来潮一个晚上完成了基本的实现.其中倒放与播放部分 ...
- html5 canvas常用api总结(一)
1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...
- compass General 常用api学习[Sass和compass学习笔记]
compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...
- java基础3.0:Java常用API
本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...
- C++ 中超类化和子类化常用API
在windows平台上,使用C++实现子类化和超类化常用的API并不多,由于这些API函数的详解和使用方法,网上一大把.本文仅作为笔记,简单的记录一下. 子类化:SetWindowLong,GetWi ...
- node.js整理 02文件操作-常用API
NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...
- js的常用api
JavaScript常用API总结 原创 2016-10-02 story JavaScript 下面是我整理的一些JavaScript常用的API清单. 目录 元素查找 class操作 节点操作 属 ...
- ffmpeg常用基本命令(转)
[FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...
随机推荐
- Web自动化测试Selenium 学习笔记(一)
1.Web自动化测试简介自动化基础:自动化用例编写.Selenium优势及原理.自动化环境搭建Selenium基础:常见8大元素定位(表格).常见元素处理.下拉框元素处理.不同窗口切换.元素进阶.元素 ...
- 1+x 证书 Web 前端开发中级理论考试(试卷 7 ) 答案
1+x 证书 Web 前端开发中级理论考试(试卷 7 ) 答案 转载请注明来源:妙笔生花个人博客http://blog.zh66.club/index.php/archives/438/ 官方QQ群 ...
- 【MySQL报错】ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot exec ...
- 使用paramiko模块进行封装,远程操作linux主机
import time import paramiko class HandleParamiko: ''' 定义一个linux处理类 ''' def __init__(self, hostname, ...
- mysql中的ifnull()函数判断空值
我们知道,在不同的数据库引擎中,内置函数的实现.命名都是存在差异的,如果经常切换使用这几个数据库引擎的话,很容易会将这些函数弄混淆. 比如说判断空值的函数,在Oracle中是NVL()函数.NVL2( ...
- Requests库主要方法解析以及Requests库入门需要掌握的框架
Requests库主要方法解析以及Requests库入门 1.requests.request(method,url,**kwargs) **kwargs:控制访问的参数,均为可选项 params:字 ...
- 前端之jquery2
jquery属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').htm ...
- C#反射调用类的私有方法
void Main() { var type = typeof(StockClass); type.Dump(); var fields=type.GetFields(BindingFlags.Ins ...
- Flask 教程 第四章:数据库
本文翻译自 The Flask Mega-Tutorial Part IV: Database 在Flask Mega-Tutorial系列的第四部分,我将告诉你如何使用数据库. 本章的主题是重中之重 ...
- LED 控制卡 单元板 接口引脚定义
LED 12接口 使能 <--- OE A ---> 行选择信号 N B ---> 行选择信号 N C ---> 行选择信号 N CLK ---> 时钟信号 N LAT/ ...