av_read_frame()方法的输入参数

时间:2017-05-02 11:19:34

标签: c++ ffmpeg

我想在没有解码的情况下阅读并打开编码域中的视频。我已经编写了代码到现在,它的工作没有错误。但是方法av_read_frame()的输出只给出了零个数,并且相同的负整数值正在重复。 我不确定我是否正确地将参数传递给方法。请帮忙。

void CFfmpegmethods::VideoRead(){
av_register_all();

    const char *url = "H:\\Sanduni_projects\\ad_1.mp4";
    AVDictionary *options = NULL;
    AVFormatContext *s = avformat_alloc_context(); //NULL;
    //AVFormatContext *avfmt = NULL;
    //avformat_alloc_context();

    AVPacket pkt;

    //AVFormatContext *avformat_alloc_context();
    //AVIOContext *avio_alloc_context();

    //open an input stream and read the header
    int ret = avformat_open_input(&s, url, NULL, NULL); 

    //avformat_find_stream_info(s, &options); //finding the missing information 

    if (ret < 0)
        abort();

    av_dict_set(&options, "video_size", "640x480", 0);
    av_dict_set(&options, "pixel_format", "rgb24", 0);

    if (avformat_open_input(&s, url, NULL, &options) < 0){
        abort();
    }

    av_dict_free(&options);

    AVDictionaryEntry *e;

    if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
        fprintf(stderr, "Option %s not recognized by the demuxer.\n", e->key);
        abort();
    }
    //int i = 0;
    while (1){
        //Split what is stored in the file into frames and return one for each call
        //returns the next frame of the stream
        int frame = av_read_frame(s, &pkt);
        //cout <<i << " " << frame << endl;
        waitKey(30);
        //i++;
    }

    //make the packet free
    av_packet_unref(&pkt);

    //Close the file after reading
    avformat_close_input(&s);

}

2 个答案:

答案 0 :(得分:0)

方法av_read_frame()在读取数据包时输出0,然后输出负值。在我的代码循环中无限运行。因此给出了无穷多的负值。

答案 1 :(得分:0)

这是修改后的代码

while (1){
            //Split what is stored in the file into frames and return one for each call
            //returns the next frame of the stream
            int frame = av_read_frame(s, pkt);

        duration = pkt->duration;
        size = pkt->size;

        total_size = total_size + size;
        total_duration = total_duration + duration;

        i++;
        if (frame < 0) break;

        cout << "frame" << i << " " << size << " "<< duration << endl;
    }