通常情况下,这种错误是由于未正确设置编码器上下文导致的。可以使用以下代码示例中的方法来设置编码器上下文:
AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodecContext* c = avcodec_alloc_context3(codec);
if (!c) {
printf("Could not allocate video codec context\n");
return;
}
c->codec_id = AV_CODEC_ID_H264;
c->width = width;
c->height = height;
// set other required codec context parameters
int ret = avcodec_open2(c, codec, NULL);
if (ret < 0) {
printf("Could not open codec: %s\n", av_err2str(ret));
return;
}
其中的“set other required codec context parameters”是设置其他需要的编码器上下文参数,例如比特率、帧率等。如果正确设置了编码器上下文,就不会出现上述错误。