【发布时间】:2014-03-26 04:25:20
【问题描述】:
我尝试使用 libav 将原始图像数据编码为 x264:
AVPacket vpkt = { 0 };
av_init_packet(&vpkt);
int got;
int ret = avcodec_encode_video2(vcodec, &vpkt, frameyuv.get(), &got);
if (!ret && got && vpkt.size) {
if (vpkt.pts != AV_NOPTS_VALUE) {
vpkt.pts = av_rescale_q(vpkt.pts, vcodec->time_base, videost->time_base);
}
if (vpkt.dts != AV_NOPTS_VALUE) {
vpkt.dts = av_rescale_q(vpkt.dts, vcodec->time_base, videost->time_base);
}
vpkt.stream_index = videost->index;
if(vcodec->coded_frame->key_frame) {
vpkt.flags |= AV_PKT_FLAG_KEY;
}
/* -> will return -22 if max_b_frames > 0 */
ret = av_interleaved_write_frame(oc, &vpkt);
}
当 vcodec->max_b_frames 设置为 0 时运行良好,但在任何其他值上 av_interleaved_write_frame 返回 -22(无效参数)。
/* will fail */
c->max_b_frames = 3;
/* -> ok*/
c->max_b_frames = 0;
为什么?我错过了什么吗?
编解码器选项是
AVDictionary *opts = NULL;
av_dict_set(&opts, "vprofile", "baseline", 0);
/* ... */
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->bit_rate = 500 * 1000;
c->width = VideoWidth;
c->height = VideoHeight;
c->time_base.den = fps;
c->time_base.num = 1;
c->pix_fmt = AV_PIX_FMT_YUV420P;
容器格式为mp4。
【问题讨论】:
-
很可能它与 PTS/DTS 值有关(当您没有 b 帧/延迟时,这可能是解决方法)。我猜你没有正确设置它们,并且 mp4 muxer 没有 AVFMT_NOTIMESTAMPS 标志,所以它们需要正确。