gpt4 book ai didi

c++ - avcodec_receive_packet 中的错误(gdi screenshot + ffmpeg)

转载 作者:行者123 更新时间:2023-12-04 23:25:32 26 4
gpt4 key购买 nike

我尝试使用 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 encoding\n");
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 encoding\n");
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 encoding\n");
exit(1);
}

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

最佳答案

派对迟到了。
我也在尝试使用 encode_video.c

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

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

关于c++ - avcodec_receive_packet 中的错误(gdi screenshot + ffmpeg),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50414435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com