I need to determine the duration of the video without relying on ffmpeg/ffprobe. To accomplish this, I created a program that searches for the mvhd box, reads its timescale and duration, and calculates the duration in seconds by dividing the duration by the timescale.
To validate my approach, I compared the calculated duration with the result obtained from ffprobe. However, there was a slight discrepancy. For instance, the calculated duration was 26.031 seconds (26031/1000), whereas ffprobe reported it as 25.98 seconds.
I'm curious about the reason behind this inconsistency. Is it possible that the video metadata stored in the mvhd box differs from the actual length of the video?
Each track in a MP4 or MOV may have an edit list. This edit list contains a presentation program for that track. For example, it can say to start playing 30 seconds of a track's media from timestamp 4 seconds at double speed, but holding or dwelling on the first frame for 5 seconds.
e.g.
entry duration time rate
0000 5000 -1 1.000000
0001 30000 16000 2.000000
(time is denoted in terms of trak timescale, duration in movie timescale)
In this case, the track presentation duration is 5 + 30/2 = 20 seconds.
So, the effective duration of a track is the presentation duration after the edit list adjustments have been made. The crude duration of the raw media in the track does not matter. The MP4 duration as then computed by ffmpeg/ffprobe is the longest effective track duration.
Now most MP4s either don't have any tracks with an edit list or a trivial one which simply plays the whole track as stored i.e. start at time 0, play full duration at rate 1. The stored track duration is supposed to incorporate the edit list but the authoring app may not have done so. So the mvhd duration is not definitive.
If you need to manually parse mp4s, you have to search for trak -> edts -> elst
box.