zoukankan      html  css  js  c++  java
  • FFmpeg编写的代码

        //初始化解封装
        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());
    }

  • 相关阅读:
    【转】想成为为一名架构师,应该掌握哪些技术呢?
    【转】每个好架构师都是一位出色的程序员
    [LeetCode] 597. Friend Requests I: Overall Acceptance Rate 朋友请求 I: 全部的接受率
    [LeetCode]577. Employee Bonus 员工奖金
    570. Managers with at Least 5 Direct Reports 至少有5个直接汇报员工的经理
    [LeetCode] 529. Minesweeper 扫雷
    Generate Maximum revenue by selling K tickets from N windows
    [LeetCode] 311. Sparse Matrix Multiplication 稀疏矩阵相乘
    [LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
    [LeetCode] 403. Frog Jump 青蛙跳
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9270528.html
Copyright © 2011-2022 走看看