Nascent Plasticine Materialist
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

music-metadata

3.1.0 • Public • Published

Build Status Build status NPM version npm downloads Dependencies Coverage Status Known Vulnerabilities Package Quality

music-metadata

Stream and file based music metadata parser for node.

Features

  • Supports metadata of the following audio and tag types:

Support for audio file types:

Audio format Description Wiki
AIFF / AIFF-C Audio Interchange File Format 🔗 Apple rainbow logo
APE Monkey's Audio 🔗
ASF Advanced Systems Format 🔗
FLAC Free Lossless Audio Codec 🔗 FLAC logo
MP2 MPEG-1 Audio Layer II 🔗
MP3 MPEG-1 / MEG-2 Audio Layer III 🔗 MP3 logo
M4A MPEG 4 Audio 🔗
Ogg / Opus 🔗 Opus logo
Ogg / Speex 🔗 Speex logo
Ogg / Vorbis 🔗 Vorbis logo
WAV 🔗 WAV logo
WavPack 🔗 WavPack logo
WMA Windows Media Audio 🔗 Windows Media logo

Support for tags:

Support for MusicBrainz tags as written by Picard.

Audio format & encoding details:

Online demo's

Compatibility

The JavaScript in runtime is compliant with ECMAScript 2015 (ES6).

Browser Support

Although music-metadata is designed to run in Node.js, it can also be used to run in the browser:

To avoid Node fs to be include, a sub-module inclusion has been made available:

import * as mm from 'music-metadata/lib/core';
function music-metadata music-metadata/lib/core
parseBuffer
parseStream *
parseFromTokenizer
parseFile

Donation

Not required, but would be extremely motivating. PayPal.me

Usage

Installation

Install via npm:

npm install music-metadata

or yarn

yarn add music-metadata

Import music-metadata:

This is how you can import music-metadata in JavaScript, in you code:

var mm = require('music-metadata');

This is how it's done in TypeScript:

import * as mm from 'music-metadata';

Module Functions:

There are two ways to parse (read) audio tracks:

  1. Audio (music) files can be parsed using direct file access using the parseFile function
  2. Using Node.js streams using the parseStream function.

Direct file access tends to be a little faster, because it can 'jump' to various parts in the file without being obliged to read intermediate date.

parseFile function

Parses the specified file (filePath) and returns a promise with the metadata result (IAudioMetadata).

parseFile(filePath: string, opts: IOptions = {}): Promise<IAudioMetadata>`

Javascript example:

const mm = require('music-metadata');
const util = require('util')
 
mm.parseFile('../test/samples/MusicBrainz-multiartist [id3v2.4].V2.mp3', {native: true})
  .then( metadata => {
    console.log(util.inspect(metadata, { showHidden: false, depth: null }));
  })
  .catch( err => {
    console.error(err.message);
  });

Typescript example:

import * as mm from 'music-metadata';
import * as util from 'util';
 
mm.parseFile('../test/samples/MusicBrainz-multiartist [id3v2.4].V2.mp3')
  .then( metadata => {
    console.log(util.inspect(metadata, {showHidden: false, depth: null}));
  })
  .catch((err) => {
    console.error(err.message);
  });

parseStream function

Parses the provided audio stream for metadata. It is recommended to provide the corresponding MIME-type. An extension (e.g.: .mp3), filename or path will also work. If the MIME-type or filename is not provided, or not understood, music-metadata will try to derive the type from the content.

parseStream(stream: Stream.Readable, mimeType?: string, opts?: IOptions = {}): Promise<IAudioMetadata>`

Example:

mm.parseStream(someReadStream, 'audio/mpeg', { fileSize: 26838 })
  .then( metadata => {
     console.log(util.inspect(metadata, { showHidden: false, depth: null }));
     someReadStream.close();
   });

parseBuffer function

Parses content of the provided buffer for metadata.

parseBuffer(buffer: Buffer, mimeType?: string, opts?: IOptions = {}): Promise<IAudioMetadata>

Example:

mm.parseBuffer(someBuffer, 'audio/mpeg', { fileSize: 26838 })
  .then( metadata => {
    console.log(util.inspect(metadata, { showHidden: false, depth: null }));
   });

orderTags function

Utility to Converts the native tags to a dictionary index on the tag identifier

orderTags(nativeTags: ITag[]): [tagId: string]: any[]

ratingToStars function

Can be used to convert the normalized rating value to the 0..5 stars, where 0 an undefined rating, 1 the star the lowest rating and 5 the highest rating.

ratingToStars(rating: number): number

Options:

  • duration: default: false, if set to true, it will parse the whole media file if required to determine the duration.
  • fileSize: only provide this in combination with parseStream function.
  • loadParser: (moduleName: string) => Promise<ITokenParser>;: default: lazy load using require, allows custom async lazy loading of parser modules. The resolved ITokenParser will not be cached.
  • native: default: false, if set to true, it will return native tags in addition to the common tags.
  • observer: (update: MetadataEvent) => void;: Will be called after each change to common (generic) tag, or format properties.
  • skipCovers: default: false, if set to true, it will not return embedded cover-art (images).
  • skipPostHeaders? boolean default: false, if set to true, it will not search all the entire track for additional headers. Only recommenced to use in combination with streams.

Although in most cases duration is included, in some cases it requires music-metadata parsing the entire file. To enforce parsing the entire file if needed you should set duration to true.

Metadata result:

If the returned promise resolves, the metadata (TypeScript IAudioMetadata interface) contains:

  • format: IFormat Audio format information
  • native: INativeTags List of native (original) tags found in the parsed audio file. If the native option is set to false, this property is not defined.
  • common: ICommonTagsResult Is a generic (abstract) way of reading metadata information.

Format

Audio format information. Defined in the TypeScript IFormat interface:

  • dataformat?: string Audio encoding format. e.g.: 'flac'
  • tagTypes?: TagType[] List of tagging formats found in parsed audio file
  • duration?: number Duration in seconds
  • bitrate?: number Number bits per second of encoded audio file
  • sampleRate?: number Sampling rate in Samples per second (S/s)
  • bitsPerSample?: number Audio bit depth
  • encoder? Encoder name
  • codecProfile?: string Codec profile
  • lossless?: boolean True if lossless, false for lossy encoding
  • numberOfChannels?: number Number of audio channels
  • numberOfSamples?: number Number of samples frames, one sample contains all channels. The duration is: numberOfSamples / sampleRate

Common

Common tag documentation is automatically generated.

Examples

In order to read the duration of a stream (with the exception of file streams), in some cases you should pass the size of the file in bytes.

mm.parseStream(someReadStream, 'audio/mpeg', { duration: true, fileSize: 26838 })
  .then( function (metadata) {
     console.log(util.inspect(metadata, { showHidden: false, depth: null }));
     someReadStream.close();
   });

Licence

(The MIT License)

Copyright (c) 2017 Borewit

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.

install

npm i music-metadata

Downloadsweekly downloads

1,270

version

3.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability