I came across the issue of getting some strange and incorrect metadata from some video files I was working with and I couldn't succeed on finding a pattern or any type of handling using code and tools like ffmpeg, mp4box, ffprobe, mediainfo, mplayer, to get the real duration of the video.
Identifying the real duration of the video was a requirement for a project I was working and the only way I found to get it always right was to reencode the video file using ffmpeg and forcing encoded files to ignore original file's metadata, like:
ffmpeg -i INPUT_FILENAME -acodec copy -vcodec copy -map_metadata -1 OUTPUT_FILENAME"
(This might run faster than you expect. I got surprised, for the type of content and on the environment I was using, average time was 2 seconds)
... and then get duration using a tool at your choice. I like mediainfo, btw:
- "mediainfo FILE --Inform="Video;%Duration%" gives you duration in miliseconds.