Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mp3-header

Package Overview
Dependencies
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mp3-header

Parsing of MP3 headers including Xing table.

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
39
increased by18.18%
Maintainers
6
Weekly downloads
 
Created
Source

Node.js MP3 Header Parsing

npm install mp3-header

Full working example (mp3)

const fs        = require("fs");
const Mp3Header = require("mp3-header").Mp3Header;

const HEADER_LENGTH = 4;

fs.open("filename.mp3", 'r', function(err, fd) {

    var buffer  = new Buffer(HEADER_LENGTH);

    // The offset in the mp3 file where there is a valid MP3 frame
    var offset  = 0;

    fs.read(fd, buffer, 0, HEADER_LENGTH, offset, function(err, bytesRead, buffer) {

        if (err) {
            return fs.close(fd);
        }

        var header = new Mp3Header(buffer);
        if (header.parsed && header.is_valid) {
            console.info("MP3 Sample Rate: ", header.mpeg_samplerate);
        }

        fs.close(fd);
    });
});

Full working example (XING)

const fs         = require("fs");
const XingHeader = require("mp3-header").XingHeader;

const MAX_FRAME_LENGTH = 2881; // Theoretical max mp3 frame length

fs.open( "filename.mp3", 'r', function(err, fd) {

    var buffer  = new Buffer(MAX_FRAME_LENGTH);

    // The offset in the mp3 file where there is a valid Xing header
    var offset  = 0;

    fs.read(fd, buffer, 0, MAX_FRAME_LENGTH, offset, function(err, bytesRead, buffer) {

        if (err) {
            return fs.close(fd);
        }

        var header = new XingHeader(buffer);
        if (header.parsed && header.is_valid) {
            console.info("Number of audio frames: ", header.xing_frames);
        }

        fs.close(fd);
    });
});

Classes

The package exports two classes for parsing MP3 file headers: Mp3Header and XingHeader.

Mp3Header is the one that takes care of parsing the MPEG header of an MP3 audio frame, and provides informations about the encoding.

XingHeader is an extension Mp3Header that also takes care of parsing the metadata related to the Xing table of the MP3 file.

Properties

Once you parse a header, these are the properties you can access from the package classes:

ParameterTypePresent inExample
parsedBooleanBothtrue
is_validBooleanBothtrue
mpeg_versionIntegerBoth2
mpeg_layerIntegerBoth3
mpeg_has_paddingBooleanBothtrue
mpeg_channelsIntegerBoth1 (mono) or 2 (stereo)
mpeg_bitrateIntegerBoth128000
mpeg_samplerateIntegerBoth44100
mpeg_num_samplesIntegerBoth1152
mpeg_frame_lengthIntegerBoth384
xing_offsetIntegerXingHeader21
xing_framesIntegerXingHeader223
xing_bytesIntegerXingHeader43008
xing_keywordStringXingHeaderXing (VBR) or Info (CBR)
xing_flagsIntegerXingHeader
xing_has_framesBooleanXingHeadertrue
xing_has_bytesBooleanXingHeadertrue
xing_frames_offsetIntegerXingHeader
xing_bytes_offsetIntegerXingHeader

Run test suite

grunt test

More info

  • https://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header
  • http://gabriel.mp3-tech.org/mp3infotag.html

License

MIT

Keywords

FAQs

Package last updated on 29 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc