Skip to content

fbbdev/mp4analyzer.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mp4analyzer.js

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/

Building

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

Usage

A trivial example

<!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>

Namespace

mp4analyzer.js exports a global object named MP4. This namespace contains all methods and properties defined by the library.

Checking for browser support

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.

Running the analyzer

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.

Reading results

{
  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

Browser support

Firefox >= 15, Chrome >= 9, Internet Explorer >= 10, Opera >= 12.1, Safari >= 6.0.2

Detail:

MP4/MOV file format documentation

  1. QuickTime File Format Specification: https://developer.apple.com/library/mac/documentation/QuickTime/qtff/QTFFPreface/qtffPreface.html

  2. MOV/ISOM demuxer code from FFMPEG:

  1. AtomicParsley documentation: http://atomicparsley.sourceforge.net/mpeg-4files.html

  2. MPEG4 Registration Authority: http://www.mp4ra.org

  3. Fourcc codes of video codecs: http://www.fourcc.org/codecs.php

License

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.