参考雷神的最简单的打印Hello World的程序:

#include <stdio.h>
#include <string.h> extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/dict.h"
}; #pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avcodec.lib") int main()
{
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec;
AVDictionaryEntry *dict = NULL; int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SS
int videoIndex, audioIndex; char *fileName = "bad.mp4";
//char *fileName = "Titanic.ts"; av_register_all();//注册所有组件 if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打开输入视频文件
{
printf("Couldn't open input stream.\n");
return -1;
} if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
} videoIndex = -1;
for (int i = 0; i < pFormatCtx->nb_streams/*视音频流的个数*/; i++)
{
if (pFormatCtx->streams[i]/*视音频流*/->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
{
videoIndex = i;
break;
}
}
if (videoIndex == -1)
{
printf("Couldn't find a video stream.\n");
return -1;
} pCodecCtx = pFormatCtx->streams[videoIndex]->codec; //指向AVCodecContext的指针
pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //指向AVCodec的指针.查找解码器
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
//打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
} audioIndex = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioIndex = i;
break;
}
}
if (audioIndex == -1)
{
printf("Couldn't find a audio stream.\n");
return -1;
} //打印结构体信息 puts("AVFormatContext信息:");
puts("---------------------------------------------");
printf("文件名:%s\n", pFormatCtx->filename);
iTotalSeconds = (int)pFormatCtx->duration/*微秒*/ / 1000000;
iHour = iTotalSeconds / 3600;//小时
iMinute = iTotalSeconds % 3600 / 60;//分钟
iSecond = iTotalSeconds % 60;//秒
printf("持续时间:%02d:%02d:%02d\n", iHour, iMinute, iSecond);
printf("平均混合码率:%d kb/s\n", pFormatCtx->bit_rate / 1000);
printf("视音频个数:%d\n", pFormatCtx->nb_streams);
puts("---------------------------------------------"); puts("AVInputFormat信息:");
puts("---------------------------------------------");
printf("封装格式名称:%s\n", pFormatCtx->iformat->name);
printf("封装格式长名称:%s\n", pFormatCtx->iformat->long_name);
printf("封装格式扩展名:%s\n", pFormatCtx->iformat->extensions);
printf("封装格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);
puts("---------------------------------------------"); puts("AVStream信息:");
puts("---------------------------------------------");
printf("视频流标识符:%d\n", pFormatCtx->streams[videoIndex]->index);
printf("音频流标识符:%d\n", pFormatCtx->streams[audioIndex]->index);
printf("视频流长度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
printf("音频流长度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
puts("---------------------------------------------"); puts("AVCodecContext信息:");
puts("---------------------------------------------");
printf("视频码率:%d kb/s\n", pCodecCtx->bit_rate / 1000);
printf("视频大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);
puts("---------------------------------------------"); puts("AVCodec信息:");
puts("---------------------------------------------");
printf("视频编码格式:%s\n", pCodec->name);
printf("视频编码详细格式:%s\n", pCodec->long_name);
puts("---------------------------------------------"); printf("视频时长:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
printf("音频时长:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
printf("音频采样率:%d\n", pFormatCtx->streams[audioIndex]->codec->sample_rate);
printf("音频信道数目:%d\n", pFormatCtx->streams[audioIndex]->codec->channels); puts("AVFormatContext元数据:");
puts("---------------------------------------------");
while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); puts("AVStream视频元数据:");
puts("---------------------------------------------");
dict = NULL;
while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); puts("AVStream音频元数据:");
puts("---------------------------------------------");
dict = NULL;
while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); av_dump_format(pFormatCtx, -1, fileName, 0);
printf("\n\n编译信息:\n%s\n\n", avcodec_configuration()); avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}

虽然足够的简单,但是还是报了”被声明为已否决”的error

在网上搜索到了解决方案:将VS的SDL检查关闭

这样error被降低为warning C4996

这样的解决方案是很冒险的。根据报错g:\coding\poet\ffmpegstudy\study.cpp(44): warning C4996: 'AVStream::codec': 被声明为已否决

我们定位到所在代码:

if (pFormatCtx->streams[i]/*视音频流*/->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
{
videoIndex = i;
break;
}
#if FF_API_LAVF_AVCTX
/**
* @deprecated use the codecpar struct instead
*/
attribute_deprecated
AVCodecContext *codec;
#endif

AVStream的codec成员不再推荐使用,反而要求使用codecpar。

从而我们知道FFmpeg中所谓的“被声明为已否决”就是因为函数或者结构体属性被标示为attribute_deprecated,很有可能在未来的版本中就删除了。

所以我们最好的解决方案就是使用新的被推荐使用的函数、结构体等。

修改版(开启SDL检查也没有error与warning):

#include <stdio.h>
#include <string.h> extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/dict.h"
}; #pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avcodec.lib") int main()
{
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec;
AVDictionaryEntry *dict = NULL; int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SS
int videoIndex, audioIndex; char *fileName = "bad.mp4";
//char *fileName = "Titanic.ts"; av_register_all();//注册所有组件 if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打开输入视频文件
{
printf("Couldn't open input stream.\n");
return -1;
} if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
} videoIndex = -1;
for (int i = 0; i < pFormatCtx->nb_streams/*视音频流的个数*/; i++)
{
if (pFormatCtx->streams[i]/*视音频流*/->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
{
videoIndex = i;
break;
}
}
if (videoIndex == -1)
{
printf("Couldn't find a video stream.\n");
return -1;
} /**
* 不赞成这样使用
* pCodecCtx = pFormatCtx->streams[videoIndex]->codec; //指向AVCodecContext的指针
*/ pCodecCtx = avcodec_alloc_context3(NULL);
if (pCodecCtx == NULL)
{
printf("Could not allocate AVCodecContext\n");
return -1;
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar); pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //指向AVCodec的指针.查找解码器
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
//打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
} audioIndex = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioIndex = i;
break;
}
}
if (audioIndex == -1)
{
printf("Couldn't find a audio stream.\n");
return -1;
} //打印结构体信息 puts("AVFormatContext信息:");
puts("---------------------------------------------");
printf("文件名:%s\n", pFormatCtx->filename);
iTotalSeconds = (int)pFormatCtx->duration/*微秒*/ / 1000000;
iHour = iTotalSeconds / 3600;//小时
iMinute = iTotalSeconds % 3600 / 60;//分钟
iSecond = iTotalSeconds % 60;//秒
printf("持续时间:%02d:%02d:%02d\n", iHour, iMinute, iSecond);
printf("平均混合码率:%d kb/s\n", pFormatCtx->bit_rate / 1000);
printf("视音频个数:%d\n", pFormatCtx->nb_streams);
puts("---------------------------------------------"); puts("AVInputFormat信息:");
puts("---------------------------------------------");
printf("封装格式名称:%s\n", pFormatCtx->iformat->name);
printf("封装格式长名称:%s\n", pFormatCtx->iformat->long_name);
printf("封装格式扩展名:%s\n", pFormatCtx->iformat->extensions);
printf("封装格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);
puts("---------------------------------------------"); puts("AVStream信息:");
puts("---------------------------------------------");
printf("视频流标识符:%d\n", pFormatCtx->streams[videoIndex]->index);
printf("音频流标识符:%d\n", pFormatCtx->streams[audioIndex]->index);
printf("视频流长度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
printf("音频流长度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
puts("---------------------------------------------"); puts("AVCodecContext信息:");
puts("---------------------------------------------");
printf("视频码率:%d kb/s\n", pCodecCtx->bit_rate / 1000);
printf("视频大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);
puts("---------------------------------------------"); puts("AVCodec信息:");
puts("---------------------------------------------");
printf("视频编码格式:%s\n", pCodec->name);
printf("视频编码详细格式:%s\n", pCodec->long_name);
puts("---------------------------------------------"); printf("视频时长:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
printf("音频时长:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
printf("音频采样率:%d\n", pFormatCtx->streams[audioIndex]->codecpar->sample_rate);
printf("音频信道数目:%d\n", pFormatCtx->streams[audioIndex]->codecpar->channels); puts("AVFormatContext元数据:");
puts("---------------------------------------------");
while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); puts("AVStream视频元数据:");
puts("---------------------------------------------");
dict = NULL;
while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); puts("AVStream音频元数据:");
puts("---------------------------------------------");
dict = NULL;
while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
{
printf("[%s] = %s\n", dict->key, dict->value);
}
puts("---------------------------------------------"); av_dump_format(pFormatCtx, -1, fileName, 0);
printf("\n\n编译信息:\n%s\n\n", avcodec_configuration()); avcodec_free_context(&pCodecCtx);
//avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}

参考自:ffplay.c

FFmpeg被声明为已否决的解决方案的更多相关文章

  1. FFmpeg 被声明为已否决 deprecated(2018 精)

    不用再取消SDL检查,不用再添加#pragma warning(disable :4996),下面才是正确的解决方法!! 以下是一些常见的deprecated问题,遇到下述没有列出的问题,可以打开相应 ...

  2. [转载] FFmpeg 错误 C4996: ‘avcodec_alloc_frame’: 被声明为已否决 解决方法

    在 Visual Studio 2013 下编写 FFmpeg 程序时出错,错误如下: 出错代码如下: 解决方法为:将 avcodec_alloc_frame() 替换为 av_frame_alloc ...

  3. error C4996: 'GetVersionExW': 被声明为已否决

    1.Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉 其他方法:2. ...

  4. vs2013编译过程中,错误 59 error C4996: 'GetVersionExW': 被声明为已否决

    好几次碰到这个错误,必须mark 一下!!!!!Project Properties > Configuration Properties > C/C++ > General > ...

  5. VS提示“项目文件" "已被重命名或已不在解决方案中”的解决办法 .

    多个项目的源码在一个源代码中,其中,有一个源代码废弃不可用了.删除后,再次生成解决方案时出现了问题“项目文件" "已被重命名或已不在解决方案中”. 解决方法是: 1.找到主项目,右 ...

  6. Mac安装文件已勾选“允许任何来源”,还是提示“文件已损坏”的解决方案

    Mac安装文件已勾选"允许任何来源",还是提示"文件已损坏"的解决方案 打开终端,在终端中粘贴下面命令:[sudo xattr -r -d com.apple. ...

  7. Genymotion关于【启动后player.exe已停止运行】解决方案总结

    1. 你硬盘空间不足,或是暂存区不够,请少执行一些程序或关掉一些p2p的程序,或是到控制面板卸载一些不必要的程序.最好的建议是定期进行硬盘清理,确保不浪费多余空间 ---以上来源(http://www ...

  8. vs2012 它已停止工作 - 解决方案

    最近学习<Windows多媒体编程>本课程, 蛋疼, 学校原来是MFC... 然后安装vs2012.   后来又在几个插件.. 在这个问题. 开业,提示 vs2012 它已停止工作. wa ...

  9. vue2.3时使用手机调试,提示媒体已断开的解决方案

    参考链接:http://www.xitonghe.com/jiaocheng/windows7-9623.html 1.在当前版本vue下开发,发现只能在localhost时调试,不能使用电脑的ip, ...

随机推荐

  1. vps远程桌面服务器管理

    vps服务器是没有远程桌面系统的,本地电脑要有远程桌面的组件或者专业的远程桌面管理工具,如果出于安全考虑关闭了3389端口(这是系统自带远程桌面的端口),你可以试试iis7远程桌面管理工具,这个还是很 ...

  2. asp.net core 3.x 通用主机原理及使用

    一.前言 只是讲asp.net core 3.x通用主机的大致原理,这些东西是通过查看源码以及自己根据经验总结得来的,在文章中不会深入源码,因为个人觉得懂原理就晓得扩展点,后期碰到有需求的时候再仔细去 ...

  3. HDU5521 Meeting 题解 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意: 有 \(n\) 个点 \(m\) 个集合,一个点可能处于若干个集合内,属于第 \(i ...

  4. SpringCloudAlibaba通过jib插件打包发布到docker仓库

    序言 在SpringBoot项目部署的时候,我了解到了Jib插件的强大,这个插件可以快速构建镜像发布到我们的镜像仓库当中去.于是我打算在毕设当中加上这个功能,并且整合到github actions中去 ...

  5. Java 几道常见String面试题

    String s1="abc"; String s2="abc"; System.out.println(s1==s2); System.out.println ...

  6. SpringCloud之Eureka(注册中心集群篇)(三)

    一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eurek ...

  7. excel中为什么不显示单引号

    解决:打两个就可以了!因为:一个单引号表示该单元格为文本属性.

  8. 20191216 GXOI 2019模拟赛 逼死强迫症

    题目传送门 分析: sb矩阵加速推一辈子... 想了1个小时,结果好像还和标准答案的方法不一样诶... 标算解法: 老套路,对于新加入的一列,考虑它与目前最后一列的关系 我们可以列出四种方案: 其中前 ...

  9. 第二次作业hzw

    第二次作业 | GIT地址 | 地址链接 | |--|--| |GIT用户名 | BRYANT333 | |学号后五位|24240| |博客地址|我的博客| |作业链接|第二次作业地址| 一.环境配置 ...

  10. django 启动错误:Generator expression must be parenthesized 错误信息:

    错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...