美文网首页
ffmpeg # 为什么remux之后大多数ts文件的第一个时间

ffmpeg # 为什么remux之后大多数ts文件的第一个时间

作者: FlyingPenguin | 来源:发表于2019-11-22 12:50 被阅读0次

    ffmpeg.h

        float mux_max_delay;
    
    

    ffmpeg_opt.c

    static void init_options(OptionsContext *o)
    {
        memset(o, 0, sizeof(*o));
    
        ..
        o->mux_max_delay  = 0.7; // 重点 初始化是 0.7
        ...
    }
    
    static int open_output_file(OptionsContext *o, const char *filename)
    {
        AVFormatContext *oc;
    ...
        oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
    ...
    }
    
    static int mpegts_init(AVFormatContext *s)
    {
    ...
    if (ts->copyts < 1)
                ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
    
    
    
    static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
    {
    ...
           const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2; // 重点 乘以了2倍
    ...
        if (ts->copyts < 1) {
            if (pts != AV_NOPTS_VALUE)
                pts += delay;
            if (dts != AV_NOPTS_VALUE)
                dts += delay;
        }
    ...
    }
    

    计算如下:
    ts文件的time base是 90000

    1.4 * 90000 =  1260000
    

    即:

        pts = 126000
        pts_time = 1.4000
    

    其它说明:

    the splitting and transcoding process, generates a delay(0.7 seconds) on the frame's Play Time Seconds.
    Surprisingly, when we are dealing with .ts files,this delay is doubled, which means the first frame will have a 1.4 seconds delay to play.

    The solution to this problem is also using another property (muxdelay), that sets the maximum demux-decode delay. If we set this property to 0, the 0.7 seconds delay no longer exists.


    控制delay的参数

    另外的参数mark:

    muxdelay

    -muxdelay seconds (output)
    Set the maximum demux-decode delay.

    max_delay

    max_delay integer (input/output)
    Set maximum muxing or demuxing delay in microseconds.

    muxdelay sets the delay in seconds while max_delay sets it in microseconds.
    The end result is the same. You can see the code that sets the underlying value in ffmpeg_opt.c

    output_ts_offset offset (output)
    Set the output time offset.

    offset must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.

    The offset is added by the muxer to the output timestamps.

    Specifying a positive offset means that the corresponding streams are delayed bt the time duration specified in offset. Default value is 0 (meaning that no offset is applied).

    MPEG-TS流中的pts_time的开始时间戳不为零?

    但至于为什么不从0开始呢。
    TS将pts和dts存储为单独的值,而其他容器使用dts + cts来确定pts
    因此,如果您的流有乱序(B)帧,您将遇到必须在时间0之前解码并在之后显示的帧。
    换句话说,您将在流的开头具有负(滚动)dts值。
    为了简化解码器工作,一些值大于最大可能的cts(pts-dts)被添加到pts / dts以使它们在开始时进入正范围。
    这是常见的做法,并由解码器/播放器将逻辑应用于向用户显示的时间。

    MPEG-TS is a streaming format and ffmpeg will offset timestamps to accommodate possible video encoding delay. Setting muxpreload and muxdelay to 0 can avoid that.

    FFmpeg is simply telling you the starting timestamp as parsed from the input.
    MPEG-TS is a transport stream format, used to convey television broadcasts..etc, having no global header, and can be sliced at ~arbitrary points.
    There are timestamp packets (PCR) at regular intervals, and ffmpeg is showing you the timestamp of the earliest packet.

    References:

    https://lists.ffmpeg.org/pipermail/libav-user/2012-May/002034.html
    https://www.w3.org/2013/12/byte-stream-format-registry/mp2t-byte-stream-format.html
    https://tsduck.io/
    https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-February/013778.html
    https://ffmpeg.org/ffmpeg-formats.html
    https://cloud.tencent.com/developer/ask/137010
    https://www.thinbug.com/q/27532571
    https://superuser.com/questions/1109852/ffmpeg-transcode-each-segment-individually
    https://stackoverflow.com/questions/29527882/ffmpeg-copyts-to-preserve-timestamp?answertab=active#tab-top
    https://stackoverflow.com/questions/44565968/while-transcoding-to-mpegts-ffmpeg-creates-the-first-frame-after-1-48-seconds-de
    https://superuser.com/questions/1462997/ffmpeg-spliting-video-but-keeping-the-timestamp/1463051
    https://video.stackexchange.com/questions/26364/how-to-change-start-time-using-ffmpeg
    https://www.thinbug.com/q/44565968

    相关文章

      网友评论

          本文标题:ffmpeg # 为什么remux之后大多数ts文件的第一个时间

          本文链接:https://www.haomeiwen.com/subject/zlyfwctx.html