ffmpeg 学习: 004-参考文档进行的开发
背景
在学习ffmpeg时,由于文档老旧以及ffmpeg新旧版本对于一些api的改动,导致学习受阻。
本来可以直接下载老的库,使用老的源码进行学习,但本人觉得,一味地守旧并不是一种好的方法。
ffmpeg 文档:
思路
由于对于FFmpeg的了解还比较皮毛,所以我们使用一些旧的例程,将提示过时的方法改为新的方法。
首先,我们查看一份过时的例程
下面的代码基于 ffmpeg v4.0 编译,为了保证编译通过,已经事先做了小部分的更改。
// ffmpeg v4.0
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libswscale/swscale.h>
//#include <libavutil/imgutils.h>
#ifdef __cplusplus
};
#endif
void SaveFrame (AVFrame * pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[32];
int y;
// open file
sprintf(szFilename, "frame%d.ppm", iFrame);
pFile = fopen(szFilename, "wb");
if(pFile == NULL) return;
fprintf(pFile, "p6 \n %d %d\n", width, height);
for (y=0; y< height;y++)
{
fwrite(pFrame->data[0]+ y*pFrame->linesize[0], 1, width*3, pFile);
}
// Close file
fclose(pFile);
}
int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrameRGB;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
if (argc <2 )
{
printf("Please provide a movie file\n");
return -1;
}
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
// Open the media file and read the header
if (avformat_open_input(&pFormatCtx, argv[1], NULL,NULL)!=0)
{
printf("Couldn't open file\n");
return -1;
}
// retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
return -1;
// dump information about file into stand error
av_dump_format(pFormatCtx, -1, argv[1], 0);
// Find the first video stream
videoStream = -1;
for(i =0; i< pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
printf("First stream is [%d]\n", videoStream);
break;
}
}
if (videoStream == -1)
return -1;
// Get a pointer to the codec context for the video stream
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec == NULL)
{
fprintf(stderr, "unsupported codec\n");
return -1;
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
return -1;
// allocate video frame
pFrame = av_frame_alloc();
if (NULL == pFrame) return -1;
// allocate an avframe structure
pFrameRGB = av_frame_alloc();
if(pFrameRGB == NULL) return -1;
// Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
printf("avpicture isze is %d\n", numBytes);
buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
i = 0;
while(av_read_frame(pFormatCtx, &packet) >= 0)
{
if(packet.stream_index ==videoStream)
{
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if(frameFinished)
{
struct SwsContext * img_convert_ctx = NULL;
img_convert_ctx = sws_getCachedContext(img_convert_ctx, pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC,
NULL, NULL, NULL);
if (!img_convert_ctx)
{
fprintf(stderr, "Cannot initalize sws conversion context\n");
exit (-1);
}
sws_scale(img_convert_ctx, pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
if(i ++ < 50)
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
}
}
av_packet_unref(&packet);
}
// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
//av_close_input_file(pFormatCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
编译,除了一堆警告以外,没有什么错误。
开始修改
先将每一个警告消除。
变量类型的不匹配
for(i =0; i< pFormatCtx->nb_streams; i++)出现问题
查阅有关的数据结构发现,是变量类型不匹配,将i改成 uint。成员改名
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)提示codec过气
同样,查阅以后,将codec修改为codecpar处理方式不同
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
之前的版本
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
之后的版本
pCodecCtx = avcodec_alloc_context3(NULL);
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
if (pCodecCtx == NULL)
{
// printf("Could not allocate AVCodecContext\n");
return -1;
}
if (pFormatCtx->streams[i]/*视音频流*/->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
// 处理音频
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar);
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
- 过气函数
avpicture_get_size
官方文档已经在 libavcodec/avcodec.h 说明:
/**
* @deprecated use av_image_get_buffer_size() instead.
*/
attribute_deprecated int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height);
由于没有在文档中很快找到 av_image_get_buffer_size,于是我在 头文件目录下搜索grep -nR av_image_get_buffer_size,找到是 libavutil这里的函数
libavcodec/avcodec.h:5650: * @deprecated use av_image_get_buffer_size() instead.
libavutil/imgutils.h:181:int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align);
libavutil/imgutils.h:186: * av_image_get_buffer_size() can be used to compute the required size
ffmpeg 学习: 004-参考文档进行的开发的更多相关文章
- Mongoose学习参考文档——基础篇
Mongoose学习参考文档 前言:本学习参考文档仅供参考,如有问题,师请雅正 一.快速通道 1.1 名词解释 Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model ...
- [转载]正则表达式参考文档 - Regular Expression Syntax Reference.
正则表达式参考文档 - Regular Expression Syntax Reference. [原创文章,转载请保留或注明出处:http://www.regexlab.com/zh/regref. ...
- MongoDB学习笔记:文档Crud Shell
MongoDB学习笔记:文档Crud Shell 文档插入 一.插入语法 db.collection.insertOne() 将单个文档插入到集合中.db.collection.insertMan ...
- 数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1)
数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1)
- css参考文档; 官方英文说明!! 1 margin padding 百分比参照物 2 margin值为auto时的说明 3 div在div里垂直居中方法 4 dispaly:flex说明
css参考文档 http://css.doyoe.com/ 两篇很好的文章:(下面的css官方英文说明链接 有时间可以研究下 http://www.w3.org/TR/css3-box/ ...
- oracle数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1)
数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1) 文档内容 用途 详细信息 Patchsets ...
- html5 兼容参考文档 与 浏览器hack兼容参考文档
移动端兼容参考文档 http://mobilehtml5.org/ 浏览器hack http://browserhacks.com/ 附上部分截图
- phpmyadmin-您可能正在上传很大的文件,请参考文档来寻找解决方法
phpmyadmin-您可能正在上传很大的文件,请参考文档来寻找解决方法 实这个很简单的只要更改php.ini里三个配置即可.(见下面加粗部分,改成你自己的需求即可) ; Maximum allo ...
- m_Orchestrate learning system---四、多看参考文档很多事情很轻松就解决了
m_Orchestrate learning system---四.多看参考文档很多事情很轻松就解决了 一.总结 一句话总结:多看参考文档啊 1.面包屑导航如何实现? 1 <ol class=& ...
随机推荐
- 树莓派4B踩坑指南 - (10)安装坚果云(更新:暂不支持)
191209更新: 根据坚果云用户支持(helpdesk@nutstore.net)的官方回复,客户端不支持arm,所以本篇后续内容可以不用看了.. 原文如下: "您好,客户端似乎不支持ar ...
- JAVA语言实现简单登录界面
程序设计思想: 使用Math.random()方法循环生成6个97~122之间的随机整数(对应ASCII码值‘a’~‘z’),将其转化为char型变量,连接成为一个6位字符串作为验证码输出,提示用户输 ...
- IDEA快捷键/本文仅供自己参考使用如有侵权立删
好好来学习学习IDEA这款神器,让你的效率飞起!视频来自慕课网 本文转载 更多技巧 代码定位 跳转: 1.IDEA的左侧侧边栏有1:Project.7:Structure和2:Favorities a ...
- oracle连接种类
等连接:连接条件使用等号 非等连接:连接条件使用等号以外的其它符号 内连接:根据指定的连接条件进行连接查询,满足连接条件的数据才会出现在结果集 外连接:在内连接的基础上,将某个连接表不符合连接条件的记 ...
- 都客仿站高手已注册旗舰版V3.1
链接:https://pan.baidu.com/s/1R5ldFDjekuXmEp42-8SQSQ 提取码:gkm9
- 记一次安装体验:pwn工具
几天前删了JDK,结果和VM在一个目录中,结果VM全没了,重安走起 记载一下安装虚拟机出现的问题,官网一个字....慢,于是找了百度网盘,据说win10版本太低没法用,我就下了vw15.5.0(建立在 ...
- ➡️➡️➡️leetcode 需要每天打卡,养成习惯
目录 待完成的 完成的 0204 0203 以前 java 的 ! 的操作 不像 c 那样自由,!不要使用在int 变量上 c ^ 是异或操作 体会:c中,malloc 后的新建的数组,默认不是0(j ...
- 【PAT甲级】1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)
题意: 输入四个正数C,DIS,D,N(C<=100,DIS<=50000,D<=20,N<=500),分别代表油箱容积,杭州到目标城市的距离,每升汽油可以行驶的路程,加油站数 ...
- Mongodb - 解决 ( aggregate聚合管道 ) $match 根据 id 匹配 返回 [ ] 的问题
需要对 id 进行转换 const mongoose = require('mongoose') var ObjectId = mongoose.Types.ObjectId; await Use ...
- 图解JVM--(二)垃圾回收
垃圾回收 1.如何判断对象可以回收 1.1 引用计数 在对象中添加一个引用计数器,每当有一个地方引用它,计数器值就加一,当引用失效时,计数器值就减一,任何时刻计数器为零的对象就不可能再被使用的,就可以 ...