mp4analyzer.js is a tool for parsing mp4/mov files and extracting information. It uses HTML5 FileReader and DataView APIs to read local files. Currently it only returns the codec of the first video and audio streams, but it can be extended to extract anything contained in mp4 atoms.
Demo: http://fbbdev.github.io/mp4analyzer.js/
mp4analyzer.js has been designed to be minified and optimized with Google Closure Compiler. The included makefile helps in the building process. Four targets are available:
- wrap: wrap the library in a single uncompressed file (output: build/mp4analyzer.js)
- minify: minify the library with Closure compiler (output: build/mp4analyzer.min.js)
- optimize: minify the library with Closure Compiler's advanced mode (output: build/mp4analyzer.opt.js)
- all (default): build plain, minified and optimized versions of the library
To use Closure Compiler you must tell make where the Closure Compiler's jar file is located:
make CLOSURE_COMPILER=/path/to/compiler.jar
Make will invoke java -jar /path/to/compiler.jar
. You can also
override the full command:
make CLOSURE_COMMAND=your_compiler_cmd
<!DOCTYPE html>
<html>
<head>
<script src="mp4analyzer.js"></script>
<script>
if (!MP4.supported) {
// mp4analyzer is not supported by this browser
// (FileReader or DataView API not supported)
}
function handleFile(files) {
if (files[0])
MP4.analyze(files[0], function(result) {
// do something
});
}
</script>
</head>
<body>
<input type="file" onchange="handleFile(this.files)">
</body>
</html>
mp4analyzer.js exports a global object named MP4
. This namespace
contains all methods and properties defined by the library.
MP4.supported = true/false;
A boolean property named supported
is defined in the MP4
namespace.
It is set to false if the requested APIs are not supported.
MP4.analyze = function( // return type: boolean
blob, // type: Blob
callback // type: function(result)
);
To analyze a file call MP4.analyze
. The first argument
must be a HTML5 File or
Blob object.
A TypeError
exception is thrown when blob
does not inherit the Blob
object.
The second argument is a completion callback. The analysis process is
asynchronous to avoid blocking the browser while waiting for disk I/O.
The argument passed to the callback is the result object.
MP4.analyze
will return false
and do nothing when
MP4.supported
is false
; it will return true
otherwise.
{
error: Error('error message'), // null if no error occurred
video: { // null if no video stream found
codec: 'codec name'
},
audio: { // null if no audio stream found
codec: 'codec name'
}
};
This is the structure of the result object passed to the callback.
As stated above, the analyzer currently extracts only the codec name
for the first video and audio streams. If no video or audio stream is found,
the concerning field in the result object is set to null
.
If an error occured, the error
property contains an
Error
object.
result.video.codec
contains a FOURCC code. You can find a FOURCC list
here.
result.audio.codec
contains one of these strings:
- '.mp3': MPEG-1 Layer 3 (MP3)
- 'aac': MPEG-4 Advanced Audio Coding (AAC)
- 'ac-3': Digital Audio Compression Standard (AC-3, Enhanced AC-3)
- 'vorbis': OGG Vorbis
- 'dvca': DV Audio
- Infrequent values
Firefox >= 15, Chrome >= 9, Internet Explorer >= 10, Opera >= 12.1, Safari >= 6.0.2
Detail:
- FileReader: http://caniuse.com/filereader
- DataView: http://caniuse.com/typedarrays
-
QuickTime File Format Specification: https://developer.apple.com/library/mac/documentation/QuickTime/qtff/QTFFPreface/qtffPreface.html
-
MOV/ISOM demuxer code from FFMPEG:
- http://git.videolan.org/?p=ffmpeg.git;a=blob;f=libavformat/mov.c
- http://git.videolan.org/?p=ffmpeg.git;a=blob;f=libavformat/isom.h
- http://git.videolan.org/?p=ffmpeg.git;a=blob;f=libavformat/isom.c
-
AtomicParsley documentation: http://atomicparsley.sourceforge.net/mpeg-4files.html
-
MPEG4 Registration Authority: http://www.mp4ra.org
-
Fourcc codes of video codecs: http://www.fourcc.org/codecs.php
The MIT License (MIT)
Copyright (c) 2015 Fabio Massaioli
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.