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

标签 c++ c winapi ffmpeg

我尝试使用 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/

相关文章:

java - (将 c# 转换为 Java JNA) - 从 hwnd GetModuleFileName

winapi - Windows 7 - 任务栏 - 固定或取消固定程序链接

c - 选择项目后 Win32 菜单位图透明度

C++ STL for_each() 和仿函数,它在哪里创建新对象

c# - 外部应用程序使用共享内存接收消息但没有包含数据

c++ - 在 C++ 中创建一个对象数组

c++ - 插入排序C实现

c - 这种类型的 yacc 代码有效吗?

c - HC-05 ⸮ 串行不起作用

c++ - std::copy_n 是否适用于重叠范围?