将FFMPEG帧写入png/jpeg文件

3

我有一个AV_PIX_FMT_YUV420P AVFrame,我想将它写成PNG或JPEG文件,不管里面是什么格式(BGR、BGRA、RGB或RGBA)。我试图将其保存为AV_PIX_FMT_YUV420P,但我得到了三张图片,所以我需要先将其转换为RGB24。我通过以下方式进行转换:

        int destW = decCtx->width;
        int destH = decCtx->height;

        AVFrame * resFrame = av_frame_alloc();
        resFrame->linesize[0] = destW * 3;
        resFrame->data[0] = (uint8_t*)malloc(resFrame->linesize[0] * destH);

        SwsContext * ctx = sws_getContext(decCtx->width,
                                          decCtx->height,
                                          AV_PIX_FMT_YUV420P,
                                          decCtx->width,
                                          decCtx->height,
                                          AV_PIX_FMT_RGB24,
                                          SWS_FAST_BILINEAR, 0, 0, 0);
        sws_scale(ctx, frame->data, frame->linesize, 0,
                  decCtx->height, resFrame->data, resFrame->linesize);
        sws_freeContext(ctx);
        int64_t pts = frame->pts;

        resFrame->format = AV_PIX_FMT_RGB24;
        resFrame->pts = pts;

看起来我已经正确转换了帧,但现在编码器返回给我一个大小为86的数据包,所以我的图像实际上是空白的或接近空白的。我是通过以下方式进行编码的:

    encCodec = avcodec_find_encoder(AV_CODEC_ID_PNG);

    if (!encCodec) {
        return false;
    }

    encCtx = avcodec_alloc_context3(encCodec);
    if (!encCtx) {
        return false;
    }

    encCtx->bit_rate = decCtx->bit_rate;
    encCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    encCtx->thread_count = 1;
    encCtx->width = decCtx->width;
    encCtx->height = decCtx->height;

    int fps = fmtc->streams[iVideoStream]->r_frame_rate.num / fmtc->streams[iVideoStream]->r_frame_rate.den;
    encCtx->time_base = (AVRational){1, fps};
    encCtx->framerate = (AVRational){fps, 1};

    encCtx->gop_size = decCtx->gop_size;
    encCtx->max_b_frames = decCtx->max_b_frames;
    encCtx->pix_fmt = AV_PIX_FMT_RGB24;

    int ret = avcodec_parameters_from_context(fmtc->streams[iVideoStream]->codecpar, encCtx);
    if (ret < 0) {
        return false;
    }

    ret = avcodec_open2(encCtx, encCodec,  nullptr);
    if (ret < 0) {
        return false;
    }

    ret = avcodec_send_frame(encCtx, source);
    if (ret < 0) {
        return false;
    }

    av_packet_unref(pkt);
    av_init_packet(pkt);

    ret = avcodec_receive_packet(encCtx, pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
        return false;
    else if (ret < 0) {
        return false;
    }

    if (ret >= 0)
    {
        FILE * outPng = fopen("./sample.png", "wb");
        fwrite(pkt->data, pkt->size, 1, outPng);
        fclose(outPng);
    }

我在将图像转换为RGB24格式或进行编码时出了什么问题?

2
你看过这个例子了吗?save_gray_frame函数在你的情况下应该适用。你可以尝试将数据包解码为帧,并将帧数据写入图像,就像例子中一样。 - undefined
嗯,你的意思是手动创建一个png/jpeg文件的头部,然后直接写入点数据。这个方法应该可行,谢谢!我会将你的建议标记为解决方案,并牢记在心以备将来之需。不过我们稍微进行了一些研究,发现如果你最初就有mpegPng格式或类似的格式,ffmpeg可以正确地写入文件。在我们的情况下,我们使用了OpenCV的cv::Mat将AVFrame转换为适当的图片格式。 - undefined
1个回答

0

@parthlr在上面的讨论中提供了可接受的解决方案。然而,你也可以阅读我在他的前置词下的评论。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接