c++, ffmpeg tanscoding: time_base 因容器而异

c++, ffmpeg tanscoding: time_base differs depending on the container

我转码视频(mkv 和 mp4)。 mkv转码为mkv时,输出正常(输出视频fps和时长与输入相同),但如果mkv转码为mp4,输出fps小于输入2倍,输出视频时长大于输入2倍

我只转码视频,音频写入来自输入文件的解码数据包。

这样创建的视频流和上下文:

out_stream = avformat_new_stream(ofmt_ctx, NULL);
avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
out_stream->codecpar->codec_tag = 0;
codec_encode  = avcodec_find_encoder(out_stream->codecpar->codec_id);
context_encode = avcodec_alloc_context3(codec_encode);
context_encode->width       = width;
context_encode->height      = height;
context_encode->pix_fmt     = codec_encode->pix_fmts[0];
context_encode->time_base   = av_inv_q(in_stream->r_frame_rate);
out_stream->time_base       = context_encode->time_base;
out_stream->r_frame_rate    = in_stream->r_frame_rate;

转码(简化):

 int64_t i = 0; 
 while (true) {
        av_read_frame(ifmt_ctx, pkt);
        in_stream = ifmt_ctx->streams[pkt->stream_index];
        pkt->stream_index = stream_mapping[pkt->stream_index];
        pCodecCtx = ifmt_ctx->streams[pkt->stream_index]->codec;
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
        error = avcodec_open2(pCodecCtx, pCodec, nullptr);
        if (pkt->stream_index == AVMEDIA_TYPE_VIDEO) {
             ....
             avcodec_decode_video2(pCodecCtx, frame, &frameFinished, pkt);
             ....
             // manipulate with frame
             ....
             frame->pts = i;
             avcodec_send_frame(context_encode, frame);
             while ((ret = avcodec_receive_packet(context_encode, pkt_encode)) >= 0) {
                  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                       break;
                  av_packet_rescale_ts(pkt_encode, context_encode->time_base, out_stream->time_base);
                  av_interleaved_write_frame(ofmt_ctx, pkt_encode);
                  av_packet_unref(pkt_encode);
             }
             i++;
        }
         else {
            av_packet_rescale_ts(pkt, in_stream->time_base, out_stream->time_base);
            av_interleaved_write_frame(ofmt_ctx, pkt);
        }
        av_packet_unref(pkt);
   }

输出 mkv 转码视频的媒体信息(mkv -> mkv):

输出 mp4 转码视频(mkv -> mp4)的媒体信息:

创建视频上下文时,time_base 值为(mkv -> mp4 和 mkv -> mkv):

FPS input: (24000/1001)
FPS output: (24000/1001)
context_decode->time_base (1001 / 48000)
context_encode->time_base (1001 / 24000)
in_stream->time_base (1 / 1000)
in_stream->codec->time_base (1001 / 48000)
out_stream->time_base (1001 / 24000)
out_stream->codec->time_base (0 / 1)

写入视频帧时,time_base值为(mkv -> mp4):

context_encode->time_base (1001 / 24000)
out_stream->time_base (1 / 48000)

但是如果 mkv->mkv:

context_encode->time_base (1001 / 24000) 
out_stream->time_base (1 / 1000)

ffmpeg av_dump:

Input #0, matroska,webm, from '24fps2.mkv':
 - Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
 - Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp (default)

Output #0, mp4, to 'temp_read.mp4':
 - Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 23.98 tbr, 23.98 tbn
 - Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp

但是如果我手动设置 time_base 等于输入视频的 FPS/2:

AVRational temp;
temp.num = 500;
temp.den = 24001;
context_encode->time_base   = temp;
out_stream->time_base       = context_encode->time_base;
out_stream->r_frame_rate    = in_stream->r_frame_rate;

创建视频流和上下文时,time_base 值为 (mkv -> mp4):

context_encode->time_base (500 / 24001)
out_stream->time_base (500 / 24001)

写入视频帧时,time_base值为(mkv -> mp4):

context_encode->time_base (500 / 24001)
out_stream->time_base (1 / 48000)

并且视频 FPS 和持续时间正确:

在这种情况下,time_base 和 av_packet_rescale 有什么问题,如何解决?

问题出在不同的时基上。音频编码:

av_packet_rescale_ts(pkt, in_stream->time_base, out_stream->time_base);

视频编码:

av_packet_rescale_ts(pkt_encode, context_encode->time_base, out_stream->time_base);

但是 out_stream 变量在两种编码中使用相同。 我用 ofmt_ctx->streams[pkt->stream_index]->time_base 替换了 out_stream->time_base,现在它工作正常。