我正在尝试使用ffmpeg库从图像创建视频.图像的大小为1920x1080,应该使用.mkv容器用H.264编码.我遇到了各种各样的问题,以为我越来越接近解决方案了,但是这个我真的被困在了.使用我使用的设置,我的视频中的前X帧(大约40帧,取决于我用于视频的图像数量)不会被编码.avcodec_encode_video2不会返回任何错误(返回值为0),其中got_picture_ptr = 0.结果是视频实际上看起来像预期的那样,但是前几秒是奇怪的跳跃.
这就是我创建视频文件的方式:
// m_codecContext is an instance variable of type AVCodecContext *
// m_formatCtx is an instance variable of type AVFormatContext *
// outputFileName is a valid filename ending with .mkv
AVOutputFormat *oformat = av_guess_format(NULL, outputFileName, NULL);
if (oformat == NULL)
{
oformat = av_guess_format("mpeg", NULL, NULL);
}
// oformat->video_codec is AV_CODEC_ID_H264
AVCodec *codec = avcodec_find_encoder(oformat->video_codec);
m_codecContext = avcodec_alloc_context3(codec);
m_codecContext->codec_id = oformat->video_codec;
m_codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
m_codecContext->gop_size = 30;
m_codecContext->bit_rate = width * height * 4
m_codecContext->width = width; …
Run Code Online (Sandbox Code Playgroud)