music-metadata-browser is a metadata parser, supporting virtual any audio format and tag header around.
music-metadata-browser is a node module, designed for modern browser applications. Because modules don't run directly in the browser you need a module bundler, like Webpack or Parcel. The module bundler turns the modules (dependencies), together with you application code, into a static asset (typically a minified JavaScript file). The output of module bundler is compatible and optimized for execution by a web browser.
Integration with React or Angular should not be a problem:
If you prefer to parse files or streams server (node.js) side, you should use music-metadata instead.
Audio format | Description | Wiki | |
---|---|---|---|
AIFF / AIFF-C | Audio Interchange File Format | đź”— | |
AAC | ADTS / Advanced Audio Coding | đź”— | |
APE | Monkey's Audio | đź”— | |
ASF | Advanced Systems Format | đź”— | |
DSDIFF | Philips DSDIFF | đź”— | |
DSF | Sony's DSD Stream File | đź”— | |
FLAC | Free Lossless Audio Codec | đź”— | |
MP2 | MPEG-1 Audio Layer II | đź”— | |
Matroska | Matroska (EBML), mka, mkv | đź”— | |
MP3 | MPEG-1 / MPEG-2 Audio Layer III | đź”— | |
MPC | Musepack SV7 | đź”— | |
MPEG 4 | mp4, m4a, m4v | đź”— | |
Ogg | Open container format | đź”— | |
Opus | đź”— | ||
Speex | đź”— | ||
Theora | đź”— | ||
Vorbis | Vorbis audio compression | đź”— | |
WAV | RIFF WAVE | đź”— | |
WebM | webm | đź”— | |
WV | WavPack | đź”— | |
WMA | Windows Media Audio | đź”— |
Support for MusicBrainz tags as written by Picard.
Support for encoding / format details:
- Bit rate
- Audio bit depth
- Duration
- encoding profile (e.g. CBR, V0, V2)
Install via npm:
npm install music-metadata-browser
or using yarn:
yarn add music-metadata-browser
Import music-metadata in JavaScript
const musicMetadata = require('music-metadata-browser');
This is how it's done in TypeScript
import * as musicMetadata from 'music-metadata-browser';
There are currently three ways to parse (read) audio tracks:
- parsing a Web API blob or file with the parseBlob function.
- Using ReadableStream using the parseReadableStream function.
- Using Node.js streams using the parseNodeStream function.
- Provide a URL to fetch the audio track from.
Parse an audio file from a Blob or File.
const musicMetadata = require('music-metadata-browser');
let blob;
musicMetadata.parseBlob(blob).then(metadata => {
// metadata has all the metadata found in the blob or file
});
Or with async/await if you prefer:
(async () => {
let blob;
const metadata = await musicMetadata.parseBlob(blob);
// metadata has all the metadata found in the blob or file
});
import * as mm from 'music-metadata-browser';
mm.parseReadableStream(readableStream)
.then(metadata => {
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
readableStream.close();
});
Parse from a Web API ReadableStream.
If available, pass the mime-type and file-size. Without the mime-type, the content will be audio type will be automatically detected.
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.
import * as mm from 'music-metadata-browser';
const readableStream = result.node;
mm.parseReadableStream(readableStream, {size: 21032, mimeType: 'audio/mpeg'})
.then( metadata => {
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
someReadStream.cancel();
});
import * as mm from 'music-metadata-browser';
mm.parseNodeStream(readableStream)
.then( metadata => {
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
readableStream.destroy();
});
The readable stream is derived from Node's readable stream.
If available, pass the mime-type and file-size. Without the mime-type, the content will be audio type will be automatically detected.
import * as mm from 'music-metadata-browser';
(async () => {
const metadata = await mm.parseNodeStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838 });
console.log(util.inspect(metadata, { showHidden: false, depth: null }));
someReadStream.close();
});
If you wish to stream your audio track over HTTP you need can use fetchFromUrl
which is using the Fetch API to retrieve the audio track:
import * as mm from 'music-metadata-browser';
(async () => {
const metadata = await mm.fetchFromUrl(audioTrackUrl, options);
});
Utility to Converts the native tags to a dictionary index on the tag identifier
orderTags(nativeTags: ITag[]): [tagId: string]: any[]
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)
rating
is a number between 0.0 and 1.0
Returns the number of stars: 0, 1, 2, 3, 4 or 5.
Select cover image based on image type field, otherwise the first picture in file.
export function selectCover(pictures?: IPicture[]): IPicture | null
import * as mm from 'music-metadata';
(async () => {
const {common} = await mm.parseFile(filePath);
const cover = mm.selectCover(common.picture); // pick the cover image
}
)();
duration
: default:false
, if set totrue
, it will parse the whole media file if required to determine the duration.fileSize
: only provide this in combination withparseStream
function.observer: (update: MetadataEvent) => void;
: Will be called after each change tocommon
(generic) tag, orformat
properties.skipCovers
: default:false
, if set totrue
, it will not return embedded cover-art (images).skipPostHeaders? boolean
default:false
, if set totrue
, it will not search all the entire track for additional headers. Only recommenced to use in combination with streams.includeChapters
default:false
, if set totrue
, it will parse chapters (currently only MP4 files). experimental functionality
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
.
If the returned promise resolves, the metadata (TypeScript IAudioMetadata
interface) contains:
format: IFormat
Audio format informationnative: INativeTags
List of native (original) tags found in the parsed audio file.common: ICommonTagsResult
Is a generic (abstract) way of reading metadata information.
Audio format information. Defined in the TypeScript IFormat
interface:
container?: string
Audio encoding format. e.g.: 'flac'codec?
Name of the codec (algorithm used for the audio compression)codecProfile?: string
Codec profile / settingstagTypes?: TagType[]
List of tagging formats found in parsed audio fileduration?: number
Duration in secondsbitrate?: number
Number bits per second of encoded audio filesampleRate?: number
Sampling rate in Samples per second (S/s)bitsPerSample?: number
Audio bit depthlossless?: boolean
True if lossless, false for lossy encodingnumberOfChannels?: number
Number of audio channelsnumberOfSamples?: number
Number of samples frames, one sample contains all channels. The duration is: numberOfSamples / sampleRate
Common tag documentation is automatically generated.
Automated unit tests are planned to be tested with different browsers. This service has been kindly made available by browserstack.com.
Unfortunately these tests are frequently failing due to technical difficulties.
(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.