我正在尝试将图像编码为 H264 MP4 视频。我遇到的问题是某些图像被跳过或在视频末尾完全丢失。我需要视频来播放我编码的每个图像,因为它是动画。
如能正确设置编码器,我们将不胜感激!
编码器设置:
AVCodecContext *c;
...
c->codec_id = AV_CODEC_ID_H264;
c->bit_rate = mOutputWidth*mOutputHeight*4;//400000;
/* Resolution must be a multiple of two. */
c->width = mOutputWidth;
c->height = mOutputHeight;
/* timebase: This is the fundamental unit of time (in seconds) in terms
* of which frame timestamps are represented. For fixed-fps content,
* timebase should be 1/framerate and timestamp increments should be
* identical to 1. */
c->time_base.den = mFps;
c->time_base.num = 1;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = AV_PIX_FMT_YUV420P;
...
av_dict_set(&pOptions, "preset", "medium", 0);
av_dict_set(&pOptions, "tune", "animation", 0);
/* open the codec */
ret = avcodec_open2(c, codec, &pOptions);
if (ret < 0) {
LOGE("Could not open video codec: %s", av_err2str(ret));
return -1;
}
2013 年 7 月 24 日更新:
通过设置 gop_size=FPS
并重复写入最后一个视频帧 FPS+1
次,我能够获得更好的视频似乎可以解决所有问题。对我来说,这样做似乎很奇怪,但可能是视频编码领域的标准?对此有任何提示反馈吗?
最佳答案
据我了解,您有一组图像,并且想用它们制作视频。如果是这种情况并且您不关心视频的大小,则可以尝试禁用帧间预测。也许编码器发现某些图像不是必需的并跳过它们。
可以通过将 gop_size
设置为 0 来禁用帧间预测。
关于c++ - 编码为 H264 视频的图像被跳过和/或丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17811987/