Where in the mp4 file structure is the duration of it?
相关问题
- Countdown to the end of the HTML5 video
- C++ a class with an array of structs, without know
- Python function to read video and convert to frame
- Character.getNumericvalue in char Frequency table
- Quickest method for matching nested XML data again
相关文章
- How to create a MediaClip from RenderTargetBitmap
- Safari blocks play() on video despite being called
- Is there an existing solution for these particular
- Why MPMoviePlayerController fullscreen button icon
- Systematically applying a function to all fields o
- Why can we add null elements to a java LinkedList?
- Skip UIImagePickerController Preview View?
- Is Heap considered an Abstract Data Type?
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.
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.
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_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.
Duration of the movie is in the movie header mvhd. The duration in seconds is derived from two fields in mvhd.
These are lines 380 and 382 in spec posted by @Tom Brito.
So given timescale 'ts' and duration 'dur'
Duration in seconds = dur / ts
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.
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)