//初始化解封装
    av_register_all();
    avformat_network_init();
    avcodec_register_all();
    //封装文件的上下文
    AVFormatContext *ic = NULL;
    char path[] = "sdcard/shape.mp4";
    //打开文件并且解析
    int re = avformat_open_input(&ic, path, NULL, NULL);
    if(re != 0)
    {
        Logw("avformat_open_input %s failed", av_err2str(re));
        return env->NewStringUTF(hello.c_str());
    }
    
    Logw("duration = %lld  nb_stream = %d", ic->duration , ic->nb_streams);
    //找到流的信息
    int re1 = avformat_find_stream_info(ic, NULL);
    if(re1!=0)
    {
        Logw("avformat_find_stream_info failed");
    }
    Logw("duration = %lld nb_stream = %d" , ic->duration , ic->nb_streams);

//找流的信息有两种方案
    //1.遍历整个信息
    int fps = 0 ;
    int wigth = 0;
    int height = 0;
    int videoStream = 0;
    int audioStream = 0;
    for (int i = 0; i < ic->nb_streams; i++)
    {
        AVStream *as = ic->streams[i];
        if(as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            Logw("视频数据");
            videoStream = i;
            fps = r2d(as->avg_frame_rate);
            Logw("fps = %d width = %d height = %d codeid = %d" , fps , as->codecpar->width ,
                 as->codecpar->height , as->codecpar->codec_id);
            Logw("tag = %d  format = %d" , as->codecpar->codec_tag , as->codecpar->format);
        }
        else if(as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            Logw("音频数据");
            audioStream = i;
            Logw("sample_rate = %d channel = %d format = %d" , as->codecpar->sample_rate ,
                  as->codecpar->channels , as->codecpar->format);
        }
    }

//av_find_best_stream()来找到对应的流
    audioStream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    Logw("av_find_best_stream audioStream = %d " , audioStream);
    Logw("sample_rate = %d channel = %d format = %d" , ic->streams[audioStream]->codecpar->sample_rate ,
         ic->streams[audioStream]->codecpar->channels , ic->streams[audioStream]->codecpar->format);

/*****************打开视频解码器*********************************/
    //找到软解码器
    AVCodec *codec = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id);
    //找到硬解码器
    codec = avcodec_find_decoder_by_name("h264_mediacodec");

//解码器初始化
    AVCodecContext *vc = avcodec_alloc_context3(codec);
    avcodec_parameters_to_context(vc, ic->streams[videoStream]->codecpar);
    vc->thread_count = 4;

//打开解码器
    re  = avcodec_open2(vc, 0, 0);
    if(re != 0)
    {
        Logw("avcodec open failed");
        return env->NewStringUTF(hello.c_str());
    }

/*****************打开音频解码器*********************************/
    AVCodec *avCodec = avcodec_find_decoder(ic->streams[audioStream]->codecpar->codec_id);

//初始化软解码器
    AVCodecContext *av = avcodec_alloc_context3(avCodec);
    avcodec_parameters_to_context(av, ic->streams[audioStream]->codecpar);
    av->thread_count = 4;

//打开音视频解码器
    re = avcodec_open2(av, 0, 0);
    if(re != 0)
    {
        Logw("avcodec open failed");
        return env->NewStringUTF(hello.c_str());
    }

//读取帧数据
    AVPacket *pkt = av_packet_alloc();
    AVFrame *frame = av_frame_alloc();
    long long start = GetNowMs();
    int frameCount = 0;
    for(;;)
    {
        //时间超过3秒
        if(GetNowMs() - start >= 3000)
        {
            Logw("now decodec fps is %d", frameCount / 3);
            start = GetNowMs();
            frameCount = 0;
        }
        int re = av_read_frame(ic, pkt);
        if(re != 0)
        {
            Logw("读取到结尾处");
//            int pos = 10 * r2d(ic->streams[videoStream]->time_base);
//            av_seek_frame(ic, videoStream, pos,
//                          AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
            break;
        }

AVCodecContext *cc = vc;
        if(pkt->stream_index == audioStream) {
            cc = av;
        }
        //把数据发送到数据缓冲空间去
        re = avcodec_send_packet(cc, pkt);
        av_packet_unref(pkt);
        if(re != 0)
        {
            Logw("avcodec_send_packet failed");
            continue;
        }

for(;;)
        {
            re = avcodec_receive_frame(cc, frame);
            if(re != 0)
            {
                //Logw("avcodec_receive_frame failed");
                break;
            }
            //Logw("avcodec_receive_frame %lld " , frame->pts);
            //如果是视频帧
            if(cc == vc)
            {
                frameCount++;
            }

}
    }
    avformat_close_input(&ic);
    return env->NewStringUTF(hello.c_str());
}

FFmpeg编写的代码的更多相关文章

  1. [转]通过Visual Studio为Linux编写C++代码

    Build 2016大会上Microsoft首次公布的Visual Studio 2015扩展提供了在VS2015中编写C++代码,随后通过Linux/UNIX计算机进行编译和执行的能力.这种想法非常 ...

  2. 基于CkEditor实现.net在线开发之路(2)编写C#代码,怎么调用它。

    上一章简约的介绍了CkEditor编辑器,可以编辑js逻辑代码,css,html,C#代码,这章我根据实际例子,讲解怎么编写C#代码和怎么调用它. 大家都还记得刚刚接触程序编时的hello Word吧 ...

  3. 解决VS2012编写JQuery代码不能智能提示的问题(其他js库的代码提示设置估计类似)

    VS默认设置下编写jQuery代码是这样的: 解决办法: 1.在项目的"管理NuGet程序包"中安装JQuery: 2.打开:工具 -> 选项 -> 文本编辑器 -&g ...

  4. JNI技术基础(2)——从零开始编写JNI代码

    书接上文: <JNI技术基础(1)——从零开始编写JNI代码> 2.编译源程序HelloWorld.java并生成HelloWorld.class 3.生成头文件HelloWorld.h ...

  5. 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】

    原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...

  6. AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码

    AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码 添加Watch应用对象时新增内容介绍 Watch应用对象添加到创建的项目中后,会包含两个部分:Watch App 和 Wa ...

  7. mvn编写主代码与测试代码

    maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...

  8. [JS进阶] 编写可维护性代码 (1)

    今天的web应用大至成千上万行的javascript代码,执行各种复杂的过程,这种演化让我们开发者必须得对可维护性有一定的认识!编写可维护性代码很重要,很多情况下我们是以他人的工作成果为基础,确保代码 ...

  9. Java认证:JavaRunnable线程编写接口代码

    Java认证:JavaRunnable线程编写接口代码.JavaRunnable线程如何才能更好的适应目前的编程环境呢?下面我们就看看如何才能更好的进行相关环境.希望下面的文章对大家有所帮助.Java ...

随机推荐

  1. SSH三大框架需要的配置文件

    1. Struts2框架 * 在web.xml中配置核心的过滤器 <filter> <filter-name>struts2</filter-name> <f ...

  2. Spring框架的AOP的底层实现之JDK的动态代理(代码了解,理解原理)

    1.创建接口UserDao: package com.huida.demo1; public interface UserDao { public void save(); public void u ...

  3. php调试利器之phpdbg

    信海龙的博客 php调试利器之phpdbg 简介 PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境. PHPDBG的目标是成为一个轻量级.强大.易用的 ...

  4. Python和JavaScript间代码转换4个工具-乾颐堂

    Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势.谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案.立足于浏览器平台,如果放弃 ...

  5. qt基本类

    多firstpage secondpage thirdpage fouthpage  实现 多页面 xml解析 实现 按钮 和 slot实现 mysql数据库访问实现

  6. jquery报.live() is not a function的解决方法

    jquery报.live() is not a function的解决方法: jquery中的live()方法在jquery1.9及以上的版本中已被废弃了,如果使用,会抛出TypeError: $(. ...

  7. 2018.09.26 bzoj4326: NOIP2015 运输计划(二分+树上差分)

    传送门 简单树上操作. 先转边权为点权. 显然所有的询问操作对应的路径会有一些交点,那么我们可以直接二分答案,对于所有大于二分值的询问用树上差分维护,最后dfs一遍每个点被覆盖了几次,当前情况合法当且 ...

  8. 【Unity】1.1 安装Unity 5.3.4 开发环境

    分类:Unity.C#.VS2015 创建日期:2016-03-23 一.简介 Unity分个人版(Personal)和专业版(Pro).个人版是免费的(部分高级功能受限,但初学者也用不到它),Pro ...

  9. HDU1065 I Think I Need a Houseboat 2016-07-24 13:59 101人阅读 评论(0) 收藏

    I Think I Need a Houseboat Problem Description Fred Mapper is considering purchasing some land in Lo ...

  10. hdu 1300 Deck

    题目 分析:对于n张卡片的最佳摆法,我们只需要在n-1张卡片的摆法下面加一张边缘与桌檐重合的卡片,并将所有卡片一起向桌檐外移动.对于一种最佳摆法,其中心一定在桌檐上,所以一定符合杠杆原理,支点是桌檐. ...