//初始化解封装
    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. Spring框架之什么是IOC的功能?

    1. 什么是IOC的功能? * IoC -- Inverse of Control,控制反转,将对象的创建权反转给Spring!! * 使用IOC可以解决的程序耦合性高的问题!!  

  2. windows常用运行命令总结

    开始→运行→命令集锦 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 ...

  3. 765A Neverending competitions

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  4. Mybatis之拦截器原理(jdk动态代理优化版本)

    在介绍Mybatis拦截器代码之前,我们先研究下jdk自带的动态代理及优化 其实动态代理也是一种设计模式...优于静态代理,同时动态代理我知道的有两种,一种是面向接口的jdk的代理,第二种是基于第三方 ...

  5. java中配置自定义拦截器中exclude-mapping path是什么意思?

    <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/>//过滤全部请求 & ...

  6. 2018.08.18 NOIP模拟 travel(贪心)

    Travel 题目背景 SOURCE:NOIP2015-SHY4 题目描述 小 A 要进行一次旅行.这回他要在序号为 1 到 n 的 n 个城市之间旅行.这 n 个城市之间共有 m 条连接两个城市的单 ...

  7. Cygwin工具的简单使用

    简介 从使用角度来看:Cygwin就是一个windows软件,该软件就是在windows上仿真linux操作系统.简言之,cygwin是一个在windows平台上运行的 linux模拟环境,使用一个D ...

  8. centos yum command

    yum repolist all -- 列出所有仓库 yum list all -- 列出仓库中所有软件包 yum info package-name -- 查看软件包信息 yum install p ...

  9. (匹配)Courses -- hdu --1083

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. (线段树 区间合并更新)Tunnel Warfare --hdu --1540

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://acm.hust.edu.cn/vjudge/contest/view.action ...