[FFmpeg-devel] [PATCH] mov: Properly abide by the track's media duration

Derek Buitenhuis derek.buitenhuis at gmail.com
Tue Apr 17 22:59:48 EEST 2018

The track's media duration from the mdhd atom takes precedence
over both the stts and elst atom for calculating and setting
the track's total duraion.

Technically, we shouldn't be using the stts atom at all for
calculating stream durations.

This fixes incorrect stream and final packet durations on files
with edit lists that are longer than the media duration.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis at gmail.com>
Personally I'd have removed the incorrect setting of the stream
duration in the stts reading code...
 libavformat/mov.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 1340bf3..8a8cd6d 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2931,7 +2931,7 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     st->nb_frames= total_sample_count;
     if (duration)
-        st->duration= duration;
+        st->duration= FFMIN(st->duration, duration);
     sc->track_end = duration;
     return 0;
@@ -3670,8 +3670,8 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
-    // Update av stream length
-    st->duration = edit_list_dts_entry_end - start_dts;
+    // Update av stream length, if it ends up shorter than the track's media duration
+    st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
     msc->start_pad = st->skip_samples;
     // Free the old index and the old CTTS structures

More information about the ffmpeg-devel mailing list