Where in the mp4 file structure is the duration of it?
-
Search for duration in the document. It's either a 8-byte (version 1) or 4-byte length in the header. – Michael Todd Jun 16 '10 at 21:13
-
@MichaelTodd I did it, and there's a lot of durations in the file. I'll read the whole document to understand better, if no one knows the answer. – The Student Jun 17 '10 at 13:02
-
Related (this one for Java): http://stackoverflow.com/questions/3015393/how-to-handle-mp4-files-with-java – The Student Jun 17 '10 at 15:34
-
2Maybe you could look at VLC/mplayer/insert-open-source-mp4-player-name source and see how that figures out the duration? – terminus Jun 24 '10 at 11:12
-
@lepple mp4 and avi are the same structure? – The Student Jun 24 '10 at 12:59
-
Possibly nowhere. I don't know mp4, but with mpeg 1 and 2 (which I don't really know either) I thought you had to count the GOPs (groups of pictures) and the frames within each GOP and then calculate based on the frame rate. I could easily be wrong, though. Even the number of GOPs isn't stored, I think - you just have to scan and see (imagine a tape-based camcorder to understand why). I could very easily be wrong, though. – Jun 26 '10 at 09:51
9 Answers
This may not be the answer to your problem but it was to mine: http://mediainfo.sourceforge.net/
(It has a library and it's open source so you can just check for the part(s) you need)

- 37,236
- 20
- 111
- 154
-
I'm no more looking for this, but this is probably the best answer here. – The Student Sep 05 '10 at 14:51
-
http://www.onlinemp4parser.com/ gives more details on atoms than mediainfo – Alam Mar 16 '18 at 14:44
See https://github.com/sannies/mp4parser project. It is a Java library that shows the structure of mp4 files.

- 7,382
- 3
- 42
- 50

- 5,205
- 4
- 51
- 64
For the Red5 MP4 reader I used the "mvhd" atom, since it contains both time scale and duration fields. Getting the duration from the atom will be different based on the version being used, below you can see an example:
public long create_full_atom(MP4DataStream bitstream) throws IOException {
long value = bitstream.readBytes(4);
version = (int)value >> 24;
flags = (int)value & 0xffffff;
readed += 4;
return readed;
}
public long create_movie_header_atom(MP4DataStream bitstream) throws IOException {
create_full_atom(bitstream);
if (version == 1) {
creationTime = createDate(bitstream.readBytes(8));
modificationTime = createDate(bitstream.readBytes(8));
timeScale = (int)bitstream.readBytes(4);
duration = bitstream.readBytes(8);
readed += 28;
} else {
creationTime = createDate(bitstream.readBytes(4));
modificationTime = createDate(bitstream.readBytes(4));
timeScale = (int)bitstream.readBytes(4);
duration = bitstream.readBytes(4);
readed += 16;
}
int qt_preferredRate = (int)bitstream.readBytes(4);
int qt_preferredVolume = (int)bitstream.readBytes(2);
bitstream.skipBytes(10);
long qt_matrixA = bitstream.readBytes(4);
long qt_matrixB = bitstream.readBytes(4);
long qt_matrixU = bitstream.readBytes(4);
long qt_matrixC = bitstream.readBytes(4);
long qt_matrixD = bitstream.readBytes(4);
long qt_matrixV = bitstream.readBytes(4);
long qt_matrixX = bitstream.readBytes(4);
long qt_matrixY = bitstream.readBytes(4);
long qt_matrixW = bitstream.readBytes(4);
long qt_previewTime = bitstream.readBytes(4);
long qt_previewDuration = bitstream.readBytes(4);
long qt_posterTime = bitstream.readBytes(4);
long qt_selectionTime = bitstream.readBytes(4);
long qt_selectionDuration = bitstream.readBytes(4);
long qt_currentTime = bitstream.readBytes(4);
long nextTrackID = bitstream.readBytes(4);
readed += 80;
return readed;
}
On a side note I used the values to calculate play time and fps like so:
The videoSampleCount variable comes from the "stsz" atom.
double fps = (videoSampleCount * timeScale) / (double) duration;
double videoTime = ((double) duration / (double) timeScale);

- 9,715
- 11
- 67
- 131
As far as i know - "mp4" container is derived from the QuickTime atom structure. You can read the description of QuickTime File Format.
Parsing quicktime atoms is not a big deal (look at atomicParsley project). I'm not sure for MP4, but as for MOV-files - there's a "duration" field in "mvhd" (movie header) atom and also in "tkhd" (track header) atom. This duration is usually a number of frames multiplied by the "time scale" attribute. Time scale can be found in the same atoms.

- 91
- 3
- 10
MP4 is a "container" format, which basically means it can contain a number of different audio or video streams. And each stream could have it's own duration value...
To dig out what you need, you're going to want some more reference files. I might suggest looking here and here... but you'll probably have to go searching beyond that for the different types of A/V streams you want to support.

- 27,179
- 15
- 70
- 84
Basically MP4 structure is a tree. Macro areas are:
- ftyp - file type
- moov - contains meta data (song title, autors, url, and other infos)
- free - empty area to separate header and data
- mdat - contains the audio frames
You can try this freeware MP4 Analyzer tool
http://www.thinmultimedia.co.kr/products/MP4Reader_download.html
Duration of the movie is in the movie header mvhd. The duration in seconds is derived from two fields in mvhd.
- 4 byte time scale
- 4 byte duration
These are lines 380 and 382 in spec posted by @Tom Brito.
So given timescale 'ts' and duration 'dur'
Duration in seconds = dur / ts

- 5,178
- 2
- 15
- 13

- 1,626
- 1
- 17
- 26
Using MP4Parser http://code.google.com/p/mp4parser/ as previous poster mentioned - they even have a sample that provides duration:

- 2,043
- 19
- 21
Media Box Viewer can be used. It is MP4 and Quicktime parser. When you open a Quicktime file, you can see the atom structure. Look for the video description atom. One of its properties is the duration. Media Box Viewer can be downloaded from www.jdxsoftware.org.

- 6,728
- 6
- 33
- 68

- 31
- 1