zoukankan      html  css  js  c++  java
  • 基于FFMPEG SDK流媒体开发1---解码媒体文件流信息

    近期项目涉及到流媒体等开发,因为有过开发经验深知其难度所在,没办法仅仅能又一次拾起,最新版的SDK被改的一塌糊涂,只是大体的开发思路都是一样的,看多少书查多少资料都无用,一步一步的编写代码 才是学好的关键。


    我会把每一天的学习经过,更新到博文上,希望能给很多其它想学习的人带来帮助,篇尾附上project     以及最新版本号SDK。

    FFMPEG被大多数的人命令行来使用,事实上在真正的流媒体开发中,要想灵活运用其开发流媒体应用层序,必须使用官方SDK开发  ,实际上我们市面上好多产品

    都是基于FFMPEG,比方 XX影音  。。

     

    FFMPEG官网  http://www.ffmpeg.org/

    API地址       http://www.ffmpeg.org/doxygen/trunk/index.html

    因为在windows下编译很痛苦,所以还是推荐大家去直接下载编译好的二进制文件,注意官网上并没有直接完整的开发包,你须要分别取下载Linux或者windows下的

    共享库 对于windows下还须要下载 .lib导入库,因为我是windows下 这里我就提供windows下载地址

    http://ffmpeg.zeranoe.com/builds/   这个页面能够下载到 动态库和到入库 。

    。由于FFMPEG已经交由别的组织维护了。。

     在以下这个页面找吧 另一点就是 既然用人家的东西 记住一定要遵循LGPL或GPL许可证...别给国人丢脸  

    人家老外都这么说了 

    Donating shows that you benefit from my work and are thankful for the time I spend on it. So if you like my work and would like to see more, feel free to donate, if you can't right now don't worry about it and just enjoy using FFmpeg on Windows. Thank you to everyone who has donated in the past!


    具体不废话了,怎样配置项目啥的,这都是新手级别的问题,我就不具体说明了 直接上代码加凝视     我会提供源码下载 。。。project配置好的 大家下载研究即可

    // ffmpeg_test.cpp : 定义控制台应用程序的入口点。

    // #include "stdafx.h" #include <windows.h> #ifdef _CPPRTTI extern "C" { #endif #include "libavcodec/avcodec.h" //编解码器 #include "libavformat/avformat.h" //格式上下文 #include "libavformat/avio.h" //音视频IO #include "libavutil/file.h" //处理文件 #ifdef _CPPRTTI }; #endif void SetStdClr(WORD wd) { SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),wd ); } int _tmain(int argc, _TCHAR* argv[]) { //注冊全部 编码器 解析器 二进制流过滤器 av_register_all(); avcodec_register_all(); SetStdClr(FOREGROUND_RED | FOREGROUND_GREEN); AVFormatContext *pContext=NULL;//格式上下文 int errNo=0 ; pContext=avformat_alloc_context(); //打开输入文件 新接口 if(0==avformat_open_input(&pContext,".\test.mp4",nullptr,NULL)){ printf("打开文件输入成功! "); }else return 0; //从上下文检索流信息 if(0==avformat_find_stream_info(pContext,NULL)) { printf("获取流信息成功! "); }else return 0 ; //循环多个流 SetStdClr(FOREGROUND_RED | FOREGROUND_BLUE); for (unsigned int i=0;i<pContext->nb_streams;i++) { //媒体流 AVStream *pStream = pContext->streams[i]; //帧率信息 为有理数/无理数 AVRational frame =pStream->r_frame_rate; // 时间比率单位 AVRational timeBase = pStream->time_base; //流的持续时间 比特率 int64_t duration= pStream->duration ; printf("媒体持续时间%d ",duration); //获取编码类型 AVCodecContext *pCodecContext=pStream->codec ; //获取 媒体类型 /************************************************************************/ /* enum AVMediaType { AVMEDIA_TYPE_UNKNOWN = -1, ///< Usually treated as AVMEDIA_TYPE_DATA AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous AVMEDIA_TYPE_SUBTITLE, AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse AVMEDIA_TYPE_NB }; */ /************************************************************************/ AVMediaType avMediaType=pCodecContext->codec_type; //编码器ID AVCodecID codecID=pCodecContext->codec_id ; if(avMediaType == AVMEDIA_TYPE_AUDIO) { //假设是视频 int audioChannels = pCodecContext->channels; int samplerate = pCodecContext->sample_rate; PixelFormat pixelFormat = pCodecContext->pix_fmt; printf("Stream%d音频 ",i); printf("音频採样频率%d/%d ",timeBase.num,timeBase.den); printf("音频时间单位%d/%d ",timeBase.num,timeBase.den); printf("音频通道数%d ",audioChannels); } else if(avMediaType == AVMEDIA_TYPE_VIDEO) { //假设是音频 int videoWidth = pCodecContext->width; int videoHeight = pCodecContext->height; AVSampleFormat sampleFmt = pCodecContext->sample_fmt; printf("Stream%d视频 ",i); printf("帧率帧率%d/%d ",frame.den,frame.num); printf("视频时间单位%d/%d ",timeBase.num,timeBase.den); printf("图像宽度:%d 高度:%d % ",videoWidth,videoHeight); printf("图像宽度:%d 高度:%d % ",videoWidth,videoHeight); } switch(codecID) { case AV_CODEC_ID_AAC: printf("编码器FAAC "); break; case AV_CODEC_ID_H264: printf("编码器H264 "); break; } } //释放上下文环境 if(!pContext) { avformat_close_input(&pContext); } return 0; }


    执行结果例如以下:



    project下载地址

    http://download.csdn.net/detail/yue7603835/8268095



  • 相关阅读:
    Struts2+Hibernate+Spring框架整合实战
    springboot集成log4j2,Spring集成log4j2解决方案整理总结
    在linux上使用less命令查看文件的时候,服务器会提示如下:"catalina.out" may be a binary file. See it anyway?
    Eslint 入门详解教程
    mysql表、视图、索引、函数、触发器相关示例(三)
    Nginx(三) 因user www未设置引发的权限问题
    Nginx(二) nginx.conf配置文件详解
    Nginx(一) Linux详细安装及部署实战
    关于Java中集成mysql(springboot)处理数据创建时间和最后更新时间的总结
    Serializable接口的意义和用法总结
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6905592.html
Copyright © 2011-2022 走看看