我有一个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格式或进行编码时出了什么问题?
save_gray_frame
函数在你的情况下应该适用。你可以尝试将数据包解码为帧,并将帧数据写入图像,就像例子中一样。 - undefined