ffmpeg解码音视频过程(附代码)
0. 引言
最近一直在使用和学习ffmpeg. 工作中需要拉流解码, 获取音频和视频数据. 这些都是使用ffmpeg处理.
因为对ffmpeg接触不多, 用的不深, 在使用的过程中经常遇到不太懂的地方, 就会花费很多时间去查阅资料. 所以自己对用到的知识点总结一下, 方便自己以后再重复用到时能够方便找到.
环境: ubuntu16.04, 已安装ffmpeg依赖库. gcc编译工具.
ffmpeg解码过程中用到了两个很重要的结构体, 这两个结构体比较复杂, 用到的次数也非常多, 以后我单独写一篇进行总结.
- AVPacket 保存未解码的数据.
- AVFrame 保存解码后的数据.
1. 解码流程

2. 代码
//***************************************************************
// @file: test.c
// @author: dingfang
// @date 2019-07-24 18:55:16
//*************************************************************** #include <stdio.h> #ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif int openCodecContext(const AVFormatContext *pFormatCtx, int *pStreamIndex, enum AVMediaType type, AVCodecContext **ppCodecCtx)
{
int streamIdx = -;
// 获取流下标
for (int i = ; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == type)
{
streamIdx = i;
break;
}
}
if (streamIdx == -)
{
printf("find video stream failed!\n");
exit(-);
}
// 寻找解码器
AVCodecContext *pCodecCtx = pFormatCtx->streams[streamIdx]->codec;
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (NULL == pCodec)
{
printf("avcode find decoder failed!\n");
exit(-);
} //打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < )
{
printf("avcode open failed!\n");
exit(-);
}
*ppCodecCtx = pCodecCtx;
*pStreamIndex = streamIdx; return ;
} int main(void)
{
AVFormatContext *pInFormatCtx = NULL;
AVCodecContext *pVideoCodecCtx = NULL;
AVCodecContext *pAudioCodecCtx = NULL;
AVPacket *pPacket = NULL;
AVFrame *pFrame = NULL;
int ret;
/* 支持本地文件和网络url */
const char streamUrl[] = "./test.flv"; /* 1. 注册 */
av_register_all(); pInFormatCtx = avformat_alloc_context(); /* 2. 打开流 */
if(avformat_open_input(&pInFormatCtx, streamUrl, NULL, NULL) != )
{
printf("Couldn't open input stream.\n");
return -;
} /* 3. 获取流的信息 */
if(avformat_find_stream_info(pInFormatCtx, NULL) < )
{
printf("Couldn't find stream information.\n");
return -;
} int videoStreamIdx = -;
int audioStreamIdx = -;
/* 4. 寻找并打开解码器 */
openCodecContext(pInFormatCtx, &videoStreamIdx, AVMEDIA_TYPE_VIDEO, &pVideoCodecCtx);
openCodecContext(pInFormatCtx, &audioStreamIdx, AVMEDIA_TYPE_AUDIO, &pAudioCodecCtx); pPacket = av_packet_alloc();
pFrame = av_frame_alloc(); int cnt = ;
while (cnt--)
{
/* 5. 读流数据, 未解码的数据存放于pPacket */
ret = av_read_frame(pInFormatCtx, pPacket);
if (ret < )
{
printf("av_read_frame error\n");
break;
} /* 6. 解码, 解码后的数据存放于pFrame */
/* 视频解码 */
if (pPacket->stream_index == videoStreamIdx)
{
avcodec_decode_video2(pVideoCodecCtx, pFrame, &ret, pPacket);
if (ret == )
{
printf("video decodec error!\n");
continue;
}
printf("* * * * * * video * * * * * * * * *\n");
printf("___height: [%d]\n", pFrame->height);
printf("____width: [%d]\n", pFrame->width);
printf("pict_type: [%d]\n", pFrame->pict_type);
printf("___format: [%d]\n", pFrame->format);
printf("* * * * * * * * * * * * * * * * * * *\n\n");
} /* 音频解码 */
if (pPacket->stream_index == audioStreamIdx)
{
avcodec_decode_audio4(pAudioCodecCtx, pFrame, &ret, pPacket);
if (ret < )
{
printf("audio decodec error!\n");
continue;
}
printf("* * * * * * audio * * * * * * * * * *\n");
printf("____nb_samples: [%d]\n", pFrame->nb_samples);
printf("__samples_rate: [%d]\n", pFrame->sample_rate);
printf("channel_layout: [%lu]\n", pFrame->channel_layout);
printf("________format: [%d]\n", pFrame->format);
printf("* * * * * * * * * * * * * * * * * * *\n\n");
}
av_packet_unref(pPacket);
} av_frame_free(&pFrame);
av_packet_free(&pPacket);
avcodec_close(pVideoCodecCtx);
avcodec_close(pAudioCodecCtx);
avformat_close_input(&pInFormatCtx); return ;
}
该代码不能直接编译, 编译需要依赖ffmpeg库. 包含ffmpeg动态库和makefile文件的压缩包地址: 点我下载
解压后, 进入目录, 使用make命令即可编译.
3. 函数说明
这里就几个比较重要的函数简单介绍一下.
av_register_all() /* 使用ffmpeg几乎都要调用这一个函数, 注册ffmpeg各种编解码器, 复用器等. */
avformat_open_input() /* 该函数用于打开本地多媒体文件或者网络流媒体url */
avformat_find_stream_info() /* 该函数用于读取一部分音视频数据并且获得一些相关的信息 */
avcodec_find_decoder() /* 由codec_id或者解码器名称来寻找对应的解码器 */
avcodec_open2() /* 初始化解码器 */
av_read_frame() /* 读流数据, 读出来的是压缩数据, 存放于AVPacket */
avcodec_decode_video2() /* 视频解码 解码后数据为原始数据, 存放于AVFrame */
avcodec_decode_audio4() /* 音频解码 解码后数据为原始数据, 存放于AVFrame */
ffmpeg解码音视频过程(附代码)的更多相关文章
- javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?
通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...
- FFmpeg处理音视频流程学习笔记
原文作者:一叶知秋0830 链接:https://www.jianshu.com/p/1b715966af50 FFmpeg处理音视频完整流程包括5个阶段(输入文件—>编码数据包—>解码后 ...
- FFmpeg开发实战(五):FFmpeg 抽取音视频的视频数据
如何使用FFmpeg抽取音视频的视频数据,代码如下: // FFmpegTest.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include ...
- FFmpeg开发实战(四):FFmpeg 抽取音视频的音频数据
如何使用FFmpeg抽取音视频的音频数据,代码如下: void adts_header(char *szAdtsHeader, int dataLen); // 使用FFmpeg从视频中抽取音频 vo ...
- iPhone调用ffmpeg2.0.2解码h264视频的示例代码
iPhone调用ffmpeg2.0.2解码h264视频的示例代码 h264demo.zip 关于怎么在MAC下编译iOS下的ffmpeg请看 编译最新ffmpeg2.0.1(ffmpeg2.0.2)到 ...
- C#进程调用FFmpeg操作音视频
项目背景 因为公司需要对音视频做一些操作,比如说对系统用户的发音和背景视频进行合成,以及对多个音视频之间进行合成,还有就是在指定的源背景音频中按照对应的规则在视频的多少秒钟内插入一段客户发音等一些复杂 ...
- FFmpeg Android 学习(一):Android 如何调用 FFMPEG 编辑音视频
一.概述 在Android开发中,我们对一些音视频的处理比较无力,特别是编辑音视频这部分.而且在Android上对视频编辑方面,几乎没有任何API做支持,MediaCodec(硬编码)也没有做支持.那 ...
- ffmpeg 如何音视频同步
转自:http://blog.csdn.net/yangzhiloveyou/article/details/8832516 output_example.c 中AV同步的代码如下(我的代码有些修改) ...
- 使用X264编码yuv格式的视频帧使用ffmpeg解码h264视频帧
前面一篇博客介绍在centos上搭建点击打开链接ffmpeg及x264开发环境.以下就来问个样例: 1.利用x264库将YUV格式视频文件编码为h264格式视频文件 2.利用ffmpeh库将h264格 ...
随机推荐
- MySQL数据分析-(12)表操作补充:字段属性
大家好,我是jacky朱元禄,很高兴继续跟大家学习MySQL数据分析实战,今天我们分享的主题是表操作补充之字段属性,依照惯例第一部分,jacky先跟大家分享本课时的学习逻辑 (一)学习逻辑 我们说创建 ...
- League of Leesins
C - League of Leesins 首先找到每一串数字的头和尾两个数字,这两个数字有一个特点,就是它们在输入数据的时候都只会出现一次.我们在输出的时候用头和尾做第一数都可以. 然后第二个数只会 ...
- allowMultiQueries=true mybatis 要多行sql执行,一定要注意
allowMultiQueries=true 这个配置已经出现多次问题了,这次由于切换时多数据源,搞配置的同志不知道从哪里copy的配置,只换了我们的链接,我们之前配置了好多配置都丢失了,我的代码中有 ...
- 解析配置文件 redis.conf
1.units单位 2.INCLUDES包含 3.GENERAL通用 1).daemonize daemonize yes 启用后台守护进程运行模式 2).pidfile pidfile /var/r ...
- Jmeter Web 性能测试入门 (三):Jmeter 常用组件说明
线程组:用来设置并发的数量和模式.是用来模拟用户并发的组件.JMeter 的每个任务都是用线程来处理的. 线程数:要并发的请求数量. Ramp-Up Period: 在多次时间内把这些并发的请求发送完 ...
- js返回函数, 函数名后带多个括号的用法及join()的注意事项
内容 题目描述 输入 输出 题目描述 实现函数 functionFunction,调用之后满足如下条件: 1.返回值为一个函数 f 2.调用返回的函数 f,返回值为按照调用顺序的参数拼接,拼接字符为英 ...
- 以太坊geth区块链私链建立
以太坊geth区块链私链建立 geth的github https://github.com/ethereum/go-ethereum 下载最新(1.8)的geth,windows下安装很简单 关于 ...
- 彻底搞清楚javascript中的require、import和export(js模块加载规范的前世今生)
为什么有模块概念 理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 但是,Javascript不是一种模块化编程语言,在es6以前,它是不支持”类”(class),所以也 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- OpenStack Manila发展动态系列--Austin峰会
1 Manila Mitaka版本概述 在Austin峰会上介绍到,Manila Mitaka发布版本Driver个数达到了18个, M版本新加入14家公司(中国公司继华为之后又有99cloud等公司 ...