This project has moved. For the latest updates, please go here.

Stream Duration Discrepancies

Nov 3, 2016 at 6:23 PM
I use nextPVR to record live ATSC (mpeg-2) into .TS. I also have some old .mpg files that were remuxed from dvr-ms files using a special ffmpeg. I'm trying to convert newer .TS and older .mpg into .mp4 (h264) using a handbrake profile. I don't care to cut out commercials; detecting them is nice but not necessary. I like having a subtitle stream in the container for cleanliness and portability (all info contained in one file).

Three years ago when I first tried to convert a large amount of the old .mpg video files, I had many files that players would tell me were, for example 32 min but would only play 30 min. One even showed 36 min but played 30 min. I recently realized that this is related to differences of each individual stream duration and is still a problem. It seems the container would report the longest stream duration as the actual duration of the video. For example, here is a bad one from 3 years ago:
General
Complete name                            : D:\HTPCData\Video\30 Rock\30 Rock_20090305_20312100.mp4
Format                                   : MPEG-4
Format profile                           : Base Media / Version 2
Codec ID                                 : mp42 (mp42/isom/avc1)
File size                                : 1.35 GiB
Duration                                 : 36 min 4 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 5 354 kb/s
Collection                               : 30 Rock
Track name                               : 30 Rock
ContentType                              : TV Show
Encoded date                             : UTC 2013-06-02 19:47:21
Tagged date                              : UTC 2013-06-02 21:12:57
Writing application                      : MCEBuddy

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.1
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 30 min 51 s
---SNIP----
Encoded date                             : UTC 2013-06-02 19:47:29
Tagged date                              : UTC 2013-06-02 21:11:07
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
Menus                                    : 3

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 30 min 51 s
----SNIP----

Menu #1
ID                                       : 3
Codec ID                                 : tx3g
Duration                                 : 36 min 4 s
Title                                    : 30 Rock_20090305_20312100.ttxt:chap - Imported with GPAC 0.5.0-rev4065
Encoded date                             : UTC 2013-06-02 21:11:07
Tagged date                              : UTC 2013-06-02 21:11:07
Menu For                                 : 1
00:00:05.810                             : Chapter 1
00:10:34.830                             : Chapter 2
00:18:20.100                             : Chapter 3
00:20:05.770                             : Chapter 4
00:28:05.280                             : Chapter 5
Bit rate mode                            : VBR

Menu #2
00:00:05.810                             : Chapter 1
00:10:34.830                             : Chapter 2
00:18:20.100                             : Chapter 3
00:20:05.770                             : Chapter 4
00:28:05.280                             : Chapter 5

You can see that Menu #1 (chapters) duration is 36 min 4 s but the video duration is 30 min 51 s.

Working on the same file with MCEBuddy 2.4.5, I get only a 6 second discrepancy:
General
Complete name                            : D:\HTPCData\Misc\testconvert\30 Rock_20090305_20312100.mp4
Format                                   : MPEG-4
Format profile                           : Base Media / Version 2
Codec ID                                 : mp42 (isom/iso2/avc1/mp41)
File size                                : 1.13 GiB
Duration                                 : 30 min 54 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 5 254 kb/s
Collection                               : 30 Rock
Track name                               : 30 Rock
ContentType                              : TV Show
Recorded date                            : UTC 2009-03-06 03:31:00
Encoded date                             : UTC 2016-10-26 01:13:17
Tagged date                              : UTC 2016-10-26 01:23:46
Writing application                      : MCEBuddy

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 4 frames
Format settings, GOP                     : M=3, N=25
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 30 min 48 s
---SNIP---

Text
ID                                       : 3
Format                                   : Timed Text
Muxing mode                              : sbtl
Codec ID                                 : tx3g
Duration                                 : 30 min 54 s
---SNIP---

Menu #1
ID                                       : 4
Codec ID                                 : tx3g
Duration                                 : 30 min 51 s
Title                                    : *ttxt:chap@GPAC0.5.1-DEV-rev4891
---SNIP---
Sometimes it is the Menu stream that is too long, sometimes it is the Text stream, sometimes both. I imagine that the text stream being too long may be related to the need for trimming the first few seconds (-ss) to avoid problems with video corruption. I plan on adjusting my subtitles as they are usually 2 to 8 seconds behind dialogue.

But, what can I do about the chapters (menu)? I would be fine if I could exclude chapters from the container but it seems to get subtitles in the container, one needs to choose chapters too. Can I separate those two? Or is there some other idea like telling the container to use the video stream duration instead of the longest stream to report total length?

I'm not too worried about a few seconds discrepancy but I worry that of the 500+ files I need to convert, some discrepancies may be significantly more.

A side question, I said I see a range of subtitle offset problems (2 to 8 seconds off). Is it usual to see this range and I should just live with some offset as I'm making settings for hundreds of files. Is it possible this offset is entirely related to -ss commands as maybe these don't trim exactly?

I did try to avoid all this by looking into just copying the CC stream directly into the container but that seems to bring in a lot of other trouble like the ability to copying the CC and which programs will actually read a 608/708 stream. So I gave up on that.
Nov 5, 2016 at 10:35 AM
Hmm, it seems there are two places where chapters are written to in the mp4 metadata, one as a stream and one as global data? See this ffprobe output:
ffprobe version 3.1.5 Copyright (c) 2007-2016 the FFmpeg developers
  built with gcc 5.4.0 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-libebur128 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Modern Family_20160316_20002031.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2016-10-26 04:07:56
    encoder         : MCEBuddy
    title           : Modern Family
    show            : Modern Family
    media_type      : 10
    date            : 2016-03-17T02:00:00Z
  Duration: 00:34:58.63, start: 0.000000, bitrate: 3324 kb/s
    Chapter #0:0: start 0.000000, end 124.140000
    Metadata:
      title           : Chapter 1
    Chapter #0:1: start 124.140000, end 758.670000
    Metadata:
      title           : Chapter 2
    Chapter #0:2: start 758.670000, end 1322.890000
    Metadata:
      title           : Chapter 3
    Chapter #0:3: start 1322.890000, end 1929.390000
    Metadata:
      title           : Chapter 4
    Chapter #0:4: start 1929.390000, end 2098.631000
    Metadata:
      title           : Chapter 5
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/bt709), 1280x720 [SAR 1:1 DAR 16:9], 3064 kb/s, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc (default)
    Metadata:
      creation_time   : 2016-10-26 04:07:56
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2016-10-26 04:07:56
      handler_name    : Surround
    Stream #0:2(und): Subtitle: mov_text (tx3g / 0x67337874), 1280x720, 0 kb/s (default)
    Metadata:
      creation_time   : 2016-10-26 04:18:41
      handler_name    : *srt:hdlr=sbtl@GPAC0.5.1-DEV-rev4891
    Stream #0:3(und): Subtitle: mov_text (tx3g / 0x67337874), 400x80
    Metadata:
      creation_time   : 2016-10-26 04:18:41
      handler_name    : *ttxt:chap@GPAC0.5.1-DEV-rev4891
Note the chapters listed initially and then the chapters listed in stream 0:3. I noticed that in the MCEBuddy options are two options that ask for chapters to be written. The first one is in the conversion task "Only detect ads" which I use as I don't want to cut Ads. The second one is in expert settings "add subtitles and chapters" which I use because I want to mux in subtitles. I would guess that the second one is creating the chapter stream 0:3 and thus is the duration mismatch offender.

In the mean time, I have found a program "drax" that will delete the chapter stream with minimal effects on other metadata and thus resolves the issue in a round-about way.
drax.exe /file:"C:\Temp\Movie.m4v" /clear_chapters
It does cause an anomaly that I don't know how serious it is. Below is an ffprobe, note the part "[mov,mp4,m4a,3gp,3g2,mj2 @ 00000000004ca680] Referenced QT chapter track not found".
ffprobe version 3.1.5 Copyright (c) 2007-2016 the FFmpeg developers
  built with gcc 5.4.0 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-libebur128 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 00000000004ca680] Referenced QT chapter track not found
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Modern Family_20160316_20002031-goodstrip.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2016-10-26 04:07:56
    title           : Modern Family
    date            : 2016-03-17T02:00:00Z
    show            : Modern Family
    encoder         : MCEBuddy
    media_type      : 10
  Duration: 00:34:58.63, start: 0.000000, bitrate: 3335 kb/s
    Chapter #0:0: start 0.000000, end 124.140000
    Metadata:
      title           : Chapter 1
    Chapter #0:1: start 124.140000, end 758.670000
    Metadata:
      title           : Chapter 2
    Chapter #0:2: start 758.670000, end 1322.890000
    Metadata:
      title           : Chapter 3
    Chapter #0:3: start 1322.890000, end 1929.390000
    Metadata:
      title           : Chapter 4
    Chapter #0:4: start 1929.390000, end 2098.631000
    Metadata:
      title           : Chapter 5
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/bt709), 1280x720 [SAR 1:1 DAR 16:9], 3064 kb/s, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc (default)
    Metadata:
      creation_time   : 2016-10-26 04:07:56
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2016-10-26 04:07:56
      handler_name    : Surround
    Stream #0:2(und): Subtitle: mov_text (tx3g / 0x67337874), 1280x720, 0 kb/s (default)
    Metadata:
      creation_time   : 2016-10-26 04:18:41
      handler_name    : *srt:hdlr=sbtl@GPAC0.5.1-DEV-rev4891
See that there are only 3 streams now.
Nov 5, 2016 at 6:32 PM
It seems I was wrong to assume that by placing a more negative value for my subtitle offset, the stream itself would shorten. The subtitles are truly shifted forward in time, but the stream containing them still remains longer than the video stream thus again leading to stream duration discrepancies and poor reporting by media players (nextPVR, WMP, MPC-HC) of actual video length. So, even though I can get rid of the chapter stream (using drax) and its effects, I'm not sure how to deal with the subtitle stream that is too long.
Nov 9, 2016 at 12:42 AM
Edited Nov 9, 2016 at 12:44 AM
I think I've found where the two types of chapters are written. I'm still using MCEBuddy 2.4.5, haven't taken the time to go to 2.4.6 yet. Looking through my log, I can see:
MCEBuddy.Engine.ConversionJob --> Adding subtitles and chapters to file
MP4Box --> Process arguments  -add "D:\HTPCData\Misc\MCEBuddy_ConvertSpace\working0\Modern Family_20160316_20002031.srt":hdlr=sbtl -chap "D:\HTPCData\Misc\MCEBuddy_ConvertSpace\working0\Modern Family_20160316_20002031.chap" -add "D:\HTPCData\Misc\MCEBuddy_ConvertSpace\working0\Modern Family_20160316_20002031.ttxt":chap "D:\HTPCData\Misc\MCEBuddy_ConvertSpace\working0\Modern Family_20160316_20002031.mp4"
Here we see:
*adding subtitles stream from the .srt file
*setting chapters from the .chap file
*adding chapters stream from the .ttxt file

The chapters stream is the one that often causes stream duration discrepancies as the -chap command doesn't have a duration like a stream does. I think I would like to not add either types of chapters though because MP4Box states:
-chap chap_file : adds chapter information located in chap_file to the destination file. Chapter extensions have been introduced by Nero and are NOT standard extensions of IsoMedia file format, don’t be surprised if some players don’t understand them.
I'm not sure what they mean by "chapter extensions" but it seems that using -chap may not lead to standard format files and may cause compatibility issues.

Remember, the option to not cut commercial also enables writing chapters and the option to write subtitle in the file also enable writing chapters. So, how do I tell MCEBuddy that I don't want to cut commercials, I do want subtitles written to the file, but I don't want chapters written to the file in any form?
Nov 17, 2016 at 11:52 PM
The issue isn't completely resolved. I was able to get around writing chapters but still some subtitle streams are too long, usually by just a couple of seconds.

To get around writing chapters but still have an EDL written and muxed subtitles, I disabled "ad remover" and disabled the "Add subtitles and chapters" option in expert settings. I then made custom commands to run comskip to make an EDL file and to run Mp4Box to mux the subtitles into the container. I would prefer to have MCEBuddy run comskip as it will also do some cleanup of the EDL file.

I suppose I should consider running CCExtractor in a custom command too so maybe I can shorten the subtitle stream by a few seconds. I disabled all beginning trimming (-ss, etc); I'll take my chances with the possible corruption causing a file to not convert. It seems that if a subtitle stream is 2 seconds too long, the subtitles themselves are delayed by 1-2 seconds in the converted file, but just fine in the original file.