我尝试使用libavcodec来编码h264视频,但avcodec_open2返回-22“无效参数”,我不知道为什么。下面是我的代码,它主要是从libavcodec的encode示例复制而来的。
/* find the mpeg1video encoder */
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "Codec '%s' not found\n", "h.264");
exit(1);
}
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (!codecContext) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
AVPacket* pkt = av_packet_alloc();
if (!pkt)
exit(1);
/* put sample parameters */
codecContext->bit_rate = 400000;
/* resolution must be a multiple of two */
codecContext->width = 1920;
codecContext->height = 1080;
/* frames per second */
codecContext->time_base = { 1, 25 };
codecContext->framerate = { 25, 1 };
/* emit one intra frame every ten frames
* check frame pict_type before passing frame
* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
* then gop_size is ignored and the output of encoder
* will always be I frame irrespective to gop_size
*/
codecContext->gop_size = 10;
codecContext->max_b_frames = 1;
codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
if (codec->id == AV_CODEC_ID_H264)
av_opt_set(codecContext->priv_data, "profile", "baseline", 0);
/* open it */
int ret = avcodec_open2(codecContext, codec, nullptr);
if (ret < 0) {
char eb[AV_ERROR_MAX_STRING_SIZE];
fprintf(stderr, "Could not open codec: %s\n", av_make_error_string(eb, AV_ERROR_MAX_STRING_SIZE, ret));
exit(1);
}
有人知道我哪里做错了吗?
1条答案
按热度按时间qqrboqgw1#
事实证明,对于“h264_mf”编解码器,您需要在“基线”中使用大写“B”。