c++, ffmpeg 转码 : time_base differs depending on the container

标签 c++ video ffmpeg frame-rate transcoding

我转码视频(mkv 和 mp4)。 mkv转码为mkv时,输出很好(输出视频fps和duration与输入相同),但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 转码视频的 mediainfo (mkv -> mkv):
  • 帧率:23.976 (24000/1001) FPS

  • 输出 mp4 转码视频的 mediainfo (mkv -> mp4):
  • 帧率:11.988 (12000/1001) FPS
  • 原始帧率:23.976 (24000/1001) FPS

  • 创建视频上下文时,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 和持续时间是正确的:
  • 帧率:23.976 (24000/1001) 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 变量对两种编码都使用了相同的方法。
    我替换了out_stream->time_baseofmt_ctx->streams[pkt->stream_index]->time_base现在它工作正常。

    关于c++, ffmpeg 转码 : time_base differs depending on the container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71980146/

    相关文章:

    c++ - 如何使用 IDirectManipulationViewport::SetViewportRect?

    bash - 在 bash 中启动 n 个子进程并等待一切完成

    video - FFMPEG 中的 2 个源视频...如何映射音频?

    python - 从 Python 将 wav 转换为 wav ulaw

    c++ - 如何在使用QSplashScreen时显示任务栏图标

    c++ - 为什么我不能在派生类重写函数中调用 protected 虚拟基类函数?

    c# - 纯 C# Silverlight 视频编码库?

    image - 将图像/视频存储到 Hadoop HDFS 中

    HTML5 视频/视频海报结束

    C++检查特定进程是否正在运行