Opened 7 years ago

Last modified 7 years ago

#6703 new defect

ffmpeg rounds duration_ts and duration

Reported by: mucikiki Owned by:
Priority: normal Component: avformat
Version: git-master Keywords: mov edts duration regression
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:

if you use NTSC videos (29.97fps), duration_ts and duration are rounded, so codec_time_base is wrong.

For example, if input video has duration=74.574500, it become duration=74.575000 (note the last 3 zeros), if duration=60.093367 it become duration=60.094000.

How to reproduce:

/usr/local/bin/ffmpeg -i input.mp4 -r ntsc -c:v libx264 -profile:v main -level:v 3.1 -crf 18 output.mp4

ffmpeg version N-85647-g59d219b
built with gcc 4.9.2 (Debian 4.9.2-10)

Attachments (4)

test1.mp4 (36.1 KB ) - added by mucikiki 7 years ago.
test2.mp4 (13.8 KB ) - added by mucikiki 7 years ago.
test1concat.mp4 (51.0 KB ) - added by mucikiki 7 years ago.
test2concat.mp4 (26.7 KB ) - added by mucikiki 7 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Carl Eugen Hoyos, 7 years ago

Keywords: ntsc duration duration_ts codec_time_base removed

Please provide the command line you tested together with the complete, uncut console output and explain how we can reproduce the issue you see. Please attach an input sample.

by mucikiki, 7 years ago

Attachment: test1.mp4 added

by mucikiki, 7 years ago

Attachment: test2.mp4 added

comment:2 by mucikiki, 7 years ago

Yes, this is the command, with the complete output:

/usr/local/bin/ffmpeg -i /var/www/html/1/download/test1.mp4 -c:v libx264 -profile:v main -level:v 3.1 -crf 18 /var/www/html/1/download/test2.mp4
ffmpeg version N-85647-g59d219b Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10)
  configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
  libavutil      55. 61.100 / 55. 61.100
  libavcodec     57. 93.100 / 57. 93.100
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 87.100 /  6. 87.100
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/html/1/download/test1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    creation_time   : 2017-09-30T12:49:58.000000Z
  Duration: 00:00:06.24, start: 0.000000, bitrate: 47 kb/s
    Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 30 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      creation_time   : 2017-09-30T12:49:58.000000Z
      handler_name    : Alias Data Handler
      encoder         : AVC Coding
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x3c858a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x3c858a0] profile Main, level 3.1
[libx264 @ 0x3c858a0] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=18.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/var/www/html/1/download/test2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    encoder         : Lavf57.72.101
    Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1, 29.97 fps, 30k tbn, 29.97 tbc (default)
    Metadata:
      creation_time   : 2017-09-30T12:49:58.000000Z
      handler_name    : Alias Data Handler
      encoder         : Lavc57.93.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=  187 fps=126 q=-1.0 Lsize=      14kB time=00:00:06.13 bitrate=  18.3kbits/s speed=4.13x    
video:11kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 27.948391%
[libx264 @ 0x3c858a0] frame I:1     Avg QP: 5.78  size:  1351
[libx264 @ 0x3c858a0] frame P:47    Avg QP:10.86  size:    63
[libx264 @ 0x3c858a0] frame B:139   Avg QP:10.70  size:    43
[libx264 @ 0x3c858a0] consecutive B-frames:  0.5%  1.1%  0.0% 98.4%
[libx264 @ 0x3c858a0] mb I  I16..4: 99.3%  0.0%  0.7%
[libx264 @ 0x3c858a0] mb P  I16..4:  0.1%  0.0%  0.0%  P16..4:  0.1%  0.0%  0.0%  0.0%  0.0%    skip:99.7%
[libx264 @ 0x3c858a0] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.1%  0.0%  0.0%  direct: 0.0%  skip:99.9%  L0:29.0% L1:71.0% BI: 0.0%
[libx264 @ 0x3c858a0] coded y,uvDC,uvAC intra: 0.3% 3.8% 0.9% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x3c858a0] i16 v,h,dc,p: 93%  6%  2%  0%
[libx264 @ 0x3c858a0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 22% 42%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x3c858a0] i8c dc,h,v,p: 76%  6% 18%  0%
[libx264 @ 0x3c858a0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x3c858a0] ref P L0: 81.7%  0.6% 17.1%  0.6%
[libx264 @ 0x3c858a0] ref B L0: 51.6% 48.4%
[libx264 @ 0x3c858a0] ref B L1: 99.6%  0.4%
[libx264 @ 0x3c858a0] kb/s:13.25

And these are both durations (obtained via ffprobe):

ffprobe -show_streams -i "/var/www/html/1/download/test1.mp4"

duration=6.239567

ffprobe -show_streams -i "/var/wwwt/html/1/download/test2.mp4"

duration=6.240000

You can find both videos attached.

comment:3 by mucikiki, 7 years ago

You can also see the duration in this way:

root@Debian2:/home/t2s4e0p# ffprobe /var/www/html/1/download/test2.mp4 -show_entries format=duration -v 0

[FORMAT]
duration=6.240000
[/FORMAT]

root@Debian2:/home/t2s4e0p# ffprobe /var/www/html/1/download/test1.mp4 -show_entries format=duration -v 0

[FORMAT]
duration=6.239567
[/FORMAT]

comment:4 by Carl Eugen Hoyos, 7 years ago

Component: undeterminedavformat
Keywords: mov edts duration regression added
Version: unspecifiedgit-master

As described above, this is a regression since a53201879ca36af6fcc8a0d4bcc1fa6d759b67ec but I don't know if the file has an issue or if the shown duration is wrong (or if there was a bug that is fixed now).

comment:5 by mucikiki, 7 years ago

I try to concat test1.mp4 with themself, and to concat test2.mp4 with themself, and I check both videos with MediaInfo (to have a different point of view).

With test1concat.mp4, the frame rate mode is still Costant, but with test2concat.mp4 is now Variable:

Frame rate : 29.970 (29970/1000) FPS
Minimum frame rate : 29.586 FPS
Maximum frame rate : 29.970 FPS

So I think this is an issue of the file.

by mucikiki, 7 years ago

Attachment: test1concat.mp4 added

by mucikiki, 7 years ago

Attachment: test2concat.mp4 added

comment:6 by mucikiki, 7 years ago

First concat:

/usr/local/bin/ffmpeg -f concat -i /var/www/html/1/download/test-1-2.txt -c copy /var/www/html/1/download/test1concat.mp4
ffmpeg version N-85647-g59d219b Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10)
  configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
  libavutil      55. 61.100 / 55. 61.100
  libavcodec     57. 93.100 / 57. 93.100
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 87.100 /  6. 87.100
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x3912a20] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from '/var/www/html/1/download/test-1-2.txt':
  Duration: N/A, start: 0.000000, bitrate: 30 kb/s
    Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 30 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc
    Metadata:
      creation_time   : 2017-09-30T12:49:58.000000Z
      handler_name    : Alias Data Handler
      encoder         : AVC Coding
Output #0, mp4, to '/var/www/html/1/download/test1concat.mp4':
  Metadata:
    encoder         : Lavf57.72.101
    Stream #0:0(eng): Video: h264 (Main) ([33][0][0][0] / 0x0021), yuv420p(tv, bt709), 1280x720, q=2-31, 30 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc
    Metadata:
      creation_time   : 2017-09-30T12:49:58.000000Z
      handler_name    : Alias Data Handler
      encoder         : AVC Coding
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x39182c0] Auto-inserting h264_mp4toannexb bitstream filter
frame=  374 fps=0.0 q=-1.0 Lsize=      51kB time=00:00:12.41 bitrate=  33.7kbits/s speed=1.17e+03x    
video:47kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 8.141509%

Second concat:

root@Debian2:/home/t2s4e0p# /usr/local/bin/ffmpeg -f concat -i /var/www/html/1/download/test-1-2b.txt -c copy /var/www/html/1/download/test2concat.mp4
ffmpeg version N-85647-g59d219b Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10)
  configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
  libavutil      55. 61.100 / 55. 61.100
  libavcodec     57. 93.100 / 57. 93.100
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 87.100 /  6. 87.100
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2dcea20] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from '/var/www/html/1/download/test-1-2b.txt':
  Duration: N/A, start: 0.000000, bitrate: 14 kb/s
    Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1280x720, 14 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc
    Metadata:
      handler_name    : VideoHandler
Output #0, mp4, to '/var/www/html/1/download/test2concat.mp4':
  Metadata:
    encoder         : Lavf57.72.101
    Stream #0:0(eng): Video: h264 (Main) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=2-31, 14 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2df9180] Auto-inserting h264_mp4toannexb bitstream filter
frame=  374 fps=0.0 q=-1.0 Lsize=      27kB time=00:00:12.37 bitrate=  17.7kbits/s speed=1.57e+03x    
video:21kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 24.252748%

I noticed a great difference in muxing overhead, maybe it can be useful.

comment:7 by mucikiki, 7 years ago

Last thing, I also test the newer version of ffmpeg, with same results:

ffmpeg version N-87628-g148c8e8 Copyright (c) 2000-2017 the FFmpeg developers

built with gcc 6.3.0 (Debian 6.3.0-18) 20170516
configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
libavutil 55. 77.101 / 55. 77.101
libavcodec 57.106.104 / 57.106.104
libavformat 57. 82.102 / 57. 82.102
libavdevice 57. 9.101 / 57. 9.101
libavfilter 6.106.100 / 6.106.100
libswscale 4. 7.103 / 4. 7.103
libswresample 2. 8.100 / 2. 8.100
libpostproc 54. 6.100 / 54. 6.100

comment:8 by mucikiki, 7 years ago

I finally found a workaround, if I use concat protocol everything is ok, the error is only related to concat demuxer.

comment:9 by Sasi Inguva, 7 years ago

The file produced by ffmpeg after encoding "test2.mp4" has different duration values in "mvhd", "elst" vs in "mdhd" .

This is because ffmpeg is using less-granular "movie timescale" (1000) than the one used for the stream (30000) . The mdhd duration of 187187 is actually correct because there are only 187 samples of 1001 duratino each in stts. However when the ffmpeg MOV muxer adds the mvhd, edts boxes it rounds the duration to 6240, because of the timescale 1000.

mvhd:
Timescale 1000
Duration 6240

mdhd:
Timescale 30000
Duration 187187
=> 187187/ 30000 ~ 6.239567s

elst:
EntryCount 1
Entry[0].SegmentDuration 6240
Entry[0].MediaTime 2002

When we have -advanced_editlist 1 (which is the default), the edit list parsing code will correctly set the stream duration based on edit list segment duration to 6.24s.

When we have -advanced_editlist 0, then the duration is obtained from mdhd or summing up stts durations, so we have a duration of 6.239567

What we can do here is to change ffmpeg MOV muxer to use a more granular timescale.


Note: See TracTickets for help on using tickets.