码迷,mamicode.com
首页 > 其他好文 > 详细

FFmpeg编写的代码

时间:2018-07-05 23:24:06      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:mat   pen   音视频   reg   方案   register   %s   idt   ecc   

    //初始化解封装
    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编写的代码

标签:mat   pen   音视频   reg   方案   register   %s   idt   ecc   

原文地址:https://www.cnblogs.com/liunx1109/p/9270528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!