README.md

mp3 Parser

Build Status NPM version Bower version

Locate and read mp3 sections: Individual mp3 frames as well as ID3v2 and Xing/Lame tags. For each of these sections present within a given mp3 file, mp3 Parser will provide data indicating their presence, their boundaries within the file, as well as any available informative data. In the current implementation, the latter is strictly true only for frames as thorough parsing and data-extraction from ID3v2 and Xing/Lame tags is work in progress. The primary use case (and raison d'etre) for this initial revision is performing precise cuts at frame / tag boundaries.

Set up

To use mp3 Parser

  • install with bower, bower install mp3-parser or
  • install with npm, npm install mp3-parser or
  • just include the relevant files (see below), in case you're targeting a browser and don't want to go over npm or bower.

mp3 Parser may be used as a CommonJS module on Node or in a browser, either as an AMD module or through plain <script> tags. It's automatically exported in the appropriate format depending on the current environment:

  • When working with CommonJS (e.g. Node), assuming mp3 Parser is npm installed:

      var mp3Parser = require("mp3-parser");
      var mp3Tags = mp3Parser.readTags(someMp3DataView);
    
  • When working with an AMD loader (e.g. requireJS):

      // Your module
      define(["path/to/mp3-parser/main"], function (mp3Parser) {
          var mp3Tags = mp3Parser.readTags(someMp3DataView);
      });
    

    In the example above, it is assumed that mp3-parser source files live in path/to/mp3-parser (relative to the root path used for module lookups). When using requireJS you may prefer to treat mp3-parser as a package:

      // Configure requireJS for the mp3-parser package ..
      require.config({
          packages: [{
              name: "mp3-parser",
              location: "path/to/mp3-parser"
          }]
      })
    
      // .. and refer to mp3-parser module by its given package name
      define(["mp3-parser"], function (mp3Parser) {
          var mp3Tags = mp3Parser.readTags(someMp3DataView);
      });
    
  • Setting up mp3 Parser in projects targetting browsers, without an AMD module loader, is unfortunately quite verbose as a number of files need to be included (in specific order). This should be mitigated in the future, but for now you would need:

      ...
      <script type="text/javascript" src="path/to/mp3-parser/lib/lib.js"></script>
      <script type="text/javascript" src="path/to/mp3-parser/lib/xing.js"></script>
      <script type="text/javascript" src="path/to/mp3-parser/lib/id3v2.js"></script>
      <script type="text/javascript" src="path/to/mp3-parser/main.js"></script>
      ...
    

    which would export the mp3Parser global:

      var mp3Tags = mp3Parser.readTags(someMp3DataView);
    

Usage

The parser exposes a collection of read____ methods, each dedicated to reading a specific section of the mp3 file. The current implementation includes readFrameHeader, readFrame, readLastFrame, readId3v2Tag, readXingTag and readTags. Each of these accepts a DataView-wrapped ArrayBuffer containing the mp3 data, and optionally an offset into the buffer.

In all cases, a 'description' will be returned - a hash containing key-value pairs relevant to the specific mp3 section being read. For example the hash returned by readFrameHeader will include an mpegAudioVersion key of value "MPEG Version 1 (ISO/IEC 11172-3)" and a layerDescription key of value "Layer III". A description will always have a _section hash with type, byteLength and offset keys:

  • type: "frame", "frameHeader", "Xing" or "ID3v2"
  • byteLenfth: Size of the section in bytes
  • offset: Buffer offset at which this section resides

In further detail:

readFrameHeader(buffer, [offset])

Read and return description of header of frame located at offset of DataView buffer. Returns null in the event that no frame header is found at offset.

readFrame(buffer, [offset[, requireNextFrame]])

Read and return description of frame located at offset of DataView buffer. Includes the frame header description (see readFrameHeader) plus some basic information about the frame - notably the frame's length in bytes. If requireNextFrame is set, the presence of a next valid frame will be required for this frame to be regarded as valid. Returns null in the event that no frame is found at offset.

readLastFrame(buffer, [offset[, requireNextFrame]])

Locate and return description of the very last valid frame in given DataView buffer. The search is carried out in reverse, from given offset (or the very last octet if offset is ommitted) to the first octet in the buffer. If requireNextFrame is set, the presence of a next valid frame will be required for any found frame to be regarded as valid (causing the method to essentially return the next-to-last frame on success). Returns null in the event that no frame is found at offset.

readId3v2Tag(buffer[, offset])

Read and return description of ID3v2 Tag located at offset of DataView buffer. (This will include any and all currently supported ID3v2 frames located within the tag). Returns null in the event that no tag is found at offset.

readXingTag(buffer[, offset])

Read and return description of Xing / Lame Tag located at offset of DataView buffer. Returns null in the event that no frame is found at offset.

readTags(buffer[, offset])

Read and return descriptions of all tags found up to (and additionally including) the very first frame. Returns an array of descriptions which may include that of a located ID3V2 tag, of a located Xing / Lame tag and of a located first frame.

I suggest that you also

License

Licensed and freely distributed under the MIT License (LICENSE.txt).

Copyright (c) 2013-2014 Alex Lambiris