【问题标题】:how to Implement function just like get_next_frame using ffmpeg?如何使用 ffmpeg 实现类似 get_next_frame 的功能?
【发布时间】:2019-05-06 21:21:46
【问题描述】:

我正在尝试实现一个可以获取下一帧的函数, 但是当我第三次调用这个函数时

int ret = decoder.get_next_frame(mat);
ret = decoder.get_next_frame(mat);
ret = decoder.get_next_frame(mat); // error 

ffmpeg avcodec_send_packet return err, msg 为 [h264 @ 0x8bd2780] NAL 单元大小无效 (10848 > 766)。 [h264 @ 0x8bd2780] 将输入拆分为 NAL 单元时出错。 前缀为 '_' 的 var 都是成员 var

int VideoDecoder::get_next_frame(cv::Mat& mat) {
int ret = 0;
bool got_frame = false;

while (!got_frame && av_read_frame(_p_fmt_ctx, _p_packet) >= 0) {
    if (_p_packet->stream_index == _video_stream_index) {

        ret = avcodec_send_packet(_p_dec_ctx, _p_packet);

        if (ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            break;
        }   

        while (ret >= 0) {
            ret = avcodec_receive_frame(_p_dec_ctx, _p_frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }   

            sws_scale(_p_img_convert_ctx, _p_frame->data, _p_frame->linesize,
                    0, _p_dec_ctx->height, _p_frame_rgb->data, _p_frame_rgb->linesize);
            mat = cv::Mat(_p_frame->height, _p_frame->width, CV_8UC3, _p_frame_rgb->data[0]);
            got_frame = true;
        }   
    }   
    av_packet_unref(_p_packet);
}   

if (got_frame) {
    return _p_dec_ctx->frame_number;
}   

return 0;
}

那么问题出在哪里?

【问题讨论】:

  • 您好堆栈溢出 (SO)。我们在 SO 热心帮助您,但请遵守我们的规则,让我们更轻松。考虑通读Help 并使用Tour。尤其要考虑通读How do I ask a good question?How to create a Minimal, Complete, and Verifiable example
  • 您能否尝试另一个视频,以确保问题不在于正在解码的文件?
  • 视频文件没问题,因为如果我将代码从“while (!got_frame && av_read_frame(_p_fmt_ctx, _p_packet) >= 0)”更改为“while (av_read_frame(_p_fmt_ctx, _p_packet) >= 0)" 所有的帧都可以正确的cv::imwrite
  • 请不要向不相关的语言发送垃圾邮件。你自己知道你在这里写 C++。
  • 删除无用代码

标签: c++ ffmpeg


【解决方案1】:

我重新检查代码,终于发现内存错误,视频数据在其他地方意外释放,所以上面的代码似乎没问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多