avcodec_receive_packet错误(gdi 屏幕截图 + ffmpeg)

Error in avcodec_receive_packet (gdi screenshot + ffmpeg)

本文关键字:屏幕截图 gdi ffmpeg receive packet 错误 avcodec      更新时间:2023-10-16

我尝试用ffmpeg捕获Windows屏幕。 一切正常,但avcodec_receive_packet返回错误AVERROR(EAGAIN( 我无法理解为什么会发生这种情况。 有人可以给出建议吗?

SwsContext* convertContext = sws_getContext(c->width, c->height, AV_PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
for (int i = 0; i < fps*seconds; i++)
{
fflush(stdout);
/* make sure the frame data is writable */
ret = av_frame_make_writable(frame);
if (ret < 0)
exit(1);
gdi->MakeScreenshoot();
OutFrame->pts = i;
int ret = av_image_fill_arrays(GDIFrame->data, GDIFrame->linesize, gdi->m_bufferGDIBits, AV_PIX_FMT_BGRA, c->width, c->height, 1);
ret = av_image_fill_arrays(OutFrame->data, OutFrame->linesize, outbuffer, c->pix_fmt, c->width, c->height, 1);
GDIFrame->data[0] += GDIFrame->linesize[0] * (c->height - 1);                                                      // flipping frame
GDIFrame->linesize[0] *= -1;
int hslice = sws_scale(convertContext, GDIFrame->data, GDIFrame->linesize, 0, c->height,OutFrame->data, OutFrame->linesize);
/* encode the image */
encode(c, OutFrame, pkt, f);
}
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)
{
int ret = -1;
/* send the frame to the encoder */
/*if (frame)
printf("Send frame %3"PRId64"n", frame->pts);*/
ret = avcodec_send_frame(enc_ctx, frame);
if (ret < 0)
{
fprintf(stderr, "Error sending a frame for encodingn");
exit(1);
}
while (ret >= 0)
{
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR_EOF)
return;
if (ret == AVERROR(EAGAIN))
return;
else
if (ret < 0)
{
fprintf(stderr, "Error during encodingn");
exit(1);
}
//printf("Write packet %3"PRId64" (size=%5d)n", pkt->pts, pkt->size);
fwrite(pkt->data, 1, pkt->size, outfile);
av_packet_unref(pkt);
}
}

如果我发表评论

if (ret == AVERROR_EOF)
return;
if (ret == AVERROR(EAGAIN))
return;
else
if (ret < 0)
{
fprintf(stderr, "Error during encodingn");
exit(1);
}

在编码函数中 - 一切都很好,文件将被写入并且是正确的,但我想解决问题。

派对有点晚了。 我也在用encode_video.c尝试这个

似乎当编码器没有准备好完整的数据包时,avcodec_receive_packet(( 会返回这些"错误"......可能下次调用 avcodec_send_frame(( 将导致一个完整的数据包。

所以继续前进。 使用 NULL 帧对 avcodec_send_frame(( 的最终调用将刷新最终数据包。