What is music-metadata?
The music-metadata npm package is a powerful tool for parsing and extracting metadata from audio files. It supports a wide range of audio formats and provides detailed information about the audio file, including tags, format, and technical properties.
What are music-metadata's main functionalities?
Extract Basic Metadata
This feature allows you to extract basic metadata from an audio file, such as title, artist, album, and genre. The code sample demonstrates how to use the `parseFile` method to read and log metadata from an MP3 file.
const mm = require('music-metadata');
const fs = require('fs');
async function getMetadata(filePath) {
try {
const metadata = await mm.parseFile(filePath);
console.log(metadata);
} catch (error) {
console.error(error.message);
}
}
getMetadata('path/to/audio/file.mp3');
Extract Format Information
This feature allows you to extract format information from an audio file, such as the container type, codec, sample rate, and bit rate. The code sample demonstrates how to use the `parseFile` method to read and log format information from an MP3 file.
const mm = require('music-metadata');
const fs = require('fs');
async function getFormatInfo(filePath) {
try {
const metadata = await mm.parseFile(filePath);
console.log(metadata.format);
} catch (error) {
console.error(error.message);
}
}
getFormatInfo('path/to/audio/file.mp3');
Extract Technical Properties
This feature allows you to extract technical properties from an audio file, such as duration, number of channels, and bit depth. The code sample demonstrates how to use the `parseFile` method to read and log technical properties from an MP3 file.
const mm = require('music-metadata');
const fs = require('fs');
async function getTechnicalProperties(filePath) {
try {
const metadata = await mm.parseFile(filePath);
console.log(metadata.common);
} catch (error) {
console.error(error.message);
}
}
getTechnicalProperties('path/to/audio/file.mp3');
Other packages similar to music-metadata
node-id3
The node-id3 package is focused on reading and writing ID3 tags in MP3 files. It provides a simpler interface for handling ID3 tags compared to music-metadata, but it is limited to MP3 files and does not support other audio formats.
musicmetadata
The musicmetadata package is another tool for extracting metadata from audio files. It supports a variety of audio formats and provides similar functionality to music-metadata. However, it is less actively maintained and may not support the latest audio formats and tags.
id3js
The id3js package is a JavaScript library for reading ID3 tags from MP3 files. It is designed to work in both Node.js and browser environments. While it offers similar functionality to music-metadata for MP3 files, it does not support other audio formats.
Streaming music metadata parser for node and the browser.
Installation
Install via npm:
npm install git+https://github.com/Borewit/music-metadata.git
You can also download a pre packaged browser release from dist/music-metadata.js
.
See example/index.html
for usage.
Supports
- mp3 (1.1, 2.2, 2.3, 2.4)
- m4a (mp4)
- vorbis (ogg, flac)
- asf (wma, wmv)
- MonkeyAudio, APEv2 (ape)
API
var fs = require('fs');
var mm = require('music-metadata');
var parser = mm(fs.createReadStream('sample.mp3'), {native=true, duration=true}function (err, metadata) {
if (err) throw err;
console.log(metadata);
});
This will output the standard music metadata:
{
format:
{
duration : 302.41
bitrate: 44100,
bitsPerSample: 16,
tagType, 'id3v2.3',
numberOfChannels: 2
}
common:
{
artist : ['Spor'],
album : 'Nightlife, Vol 5.',
albumartist : [ 'Andy C', 'Spor' ],
title : 'Stronger',
year : '2010',
track : { no : 1, of : 44 },
disk : { no : 1, of : 2 },
genre : ['Drum & Bass'],
label: 'RAM Records',
musicbrainz_albumid: 'a6da0420-f7ec-4b47-9e5a-9b0f33eeb8f2',
picture : [ { format : 'jpg', data : <Buffer> } ]
},
'id3v2.4':
{
TPE1 : ['Spor'],
TALB : 'Nightlife, Vol 5.',
TPE2 : [ 'Andy C', 'Spor' ],
TIT2 : 'Stronger',
TYER : '2010',
TRCK : '1/44',
TPOS : '1/2',
TCON : ['Drum & Bass'],
TPUB : 'RAM Records',
'TXXX:MusicBrainz Album Id': 'a6da0420-f7ec-4b47-9e5a-9b0f33eeb8f2'
...
}
}
Note, the stream is not closed by default. To prevent leaks, you must close it yourself:
var readableStream = fs.createReadStream('sample.mp3');
var parser = mm(readableStream, function (err, metadata) {
if (err) throw err;
readableStream.close();
});
music-metadata
also emits all metadata it discovers during parsing. For example if you wanted to read the TLEN
frame from an id3v2.x file you can do this:
parser.on('TLEN', function (result) {
console.log(result);
});
You can also read the duration; to calculate the duration music-metadata
may need to parse the entire file
so only enable this if you need the functionality.
mm(fs.createReadStream('sample.mp3'), { duration: true }, function (err, metadata) {
});
Note that in order to read the duration for streams that are not file streams, you must also pass the size of the file in bytes.
mm(fs.createReadStream('sample.mp3'), { duration: true, fileSize: 26838 }, function (err, metadata) {
});
Licence
(The MIT License)
Copyright (c) 2016 Borewit
Based on [musicmetadata] (https://github.com/leetreveil/musicmetadata/) written by Lee Treveil leetreveil@gmail.com and many others.
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.