music-metadata 11.8.1 → 11.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,874 +1,874 @@
1
- [![CI](https://github.com/Borewit/music-metadata/actions/workflows/ci.yml/badge.svg)](https://github.com/Borewit/music-metadata/actions/workflows/ci.yml)
2
- [![NPM version](https://img.shields.io/npm/v/music-metadata.svg)](https://npmjs.org/package/music-metadata)
3
- [![npm downloads](http://img.shields.io/npm/dm/music-metadata.svg)](https://npmcharts.com/compare/music-metadata?start=600&interval=30)
4
- [![Coverage Status](https://coveralls.io/repos/github/Borewit/music-metadata/badge.svg?branch=master)](https://coveralls.io/github/Borewit/music-metadata?branch=master)
5
- [![Codacy Badge](https://api.codacy.com/project/badge/Grade/57d731b05c9e41889a2a17cb4b0384d7)](https://app.codacy.com/app/Borewit/music-metadata?utm_source=github.com&utm_medium=referral&utm_content=Borewit/music-metadata&utm_campaign=Badge_Grade_Dashboard)
6
- [![CodeQL](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml)
7
- [![DeepScan grade](https://deepscan.io/api/teams/5165/projects/6938/branches/61821/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=5165&pid=6938&bid=61821)
8
- [![Known Vulnerabilities](https://snyk.io/test/github/Borewit/music-metadata/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/music-metadata?targetFile=package.json)
9
- [![Discord](https://img.shields.io/discord/460524735235883049.svg)](https://discord.gg/KyBr6sb)
10
-
11
- # music-metadata
12
-
13
- Key features:
14
- - **Comprehensive Format Support**: Supports popular audio formats like MP3, MP4, FLAC, Ogg, WAV, AIFF, and more.
15
- - **Extensive Metadata Extraction**: Extracts detailed metadata, including ID3v1, ID3v2, APE, Vorbis, and iTunes/MP4 tags.
16
- - **Streaming Support**: Efficiently handles large audio files by reading metadata from streams, making it suitable for server-side and browser-based applications.
17
- - **Promise-Based API**: Provides a modern, promise-based API for easy integration into asynchronous workflows.
18
- - **Cross-Platform**: Works in both [Node.js](https://nodejs.org/) and browser environments with the help of bundlers like [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/introduction/).
19
-
20
- The [`music-metadata`](https://github.com/Borewit/music-metadata) module is ideal for developers working on media applications, music players, or any project that requires access to detailed audio file metadata.
21
-
22
- ## Compatibility
23
-
24
- Module: version 8 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
25
- The distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.
26
-
27
- > [!NOTE]
28
- > See also [CommonJS backward Compatibility](#commonjs-backward-compatibility)
29
-
30
- This module requires a [Node.js ≥ 18](https://nodejs.org/en/about/previous-releases) engine.
31
- It can also be used in a browser environment when bundled with a module bundler.
32
-
33
- ## Support the Project
34
- If you find this project useful and would like to support its development, consider sponsoring or contributing:
35
-
36
- - [Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
37
-
38
- - Buy me a coffee:
39
-
40
- <a href="https://www.buymeacoffee.com/borewit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy me A coffee" height="41" width="174"></a>
41
-
42
- ## Features
43
-
44
- ### Support for audio file types
45
-
46
- | Audio format | Description | Logo |
47
- |---------------------------------------------------------------------------|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------|
48
- | [AIFF / AIFF-C](https://wikipedia.org/wiki/Audio_Interchange_File_Format) | Audio Interchange File Format | <img src="./image/logo-Apple_Computer_Logo_rainbow.svg" width="40" alt="Apple rainbow logo"> |
49
- | [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) | ADTS / Advanced Audio Coding | <img src="./image/logo-AAC_original.svg" width="40" alt="AAC logo"> |
50
- | [APE](https://wikipedia.org/wiki/Monkey's_Audio) | Monkey's Audio | <img src="./image/logo-APE-AI.svg" width="40" alt="Monkey's Audio logo"> |
51
- | [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format) | Advanced Systems Format | |
52
- | [BWF](https://en.wikipedia.org/wiki/Broadcast_Wave_Format) | Extended WAV format for broadcast and archiving | |
53
- | [DSDIFF](https://wikipedia.org/wiki/Direct_Stream_Digital) | Philips DSDIFF | <img src="./image/logo-DSD.svg" width="80" alt="DSD logo"> |
54
- | [DSF](https://wikipedia.org/wiki/Direct_Stream_Digital) | Sony's DSD Stream File | <img src="./image/logo-DSD.svg" width="80" alt="DSD logo"> |
55
- | [FLAC](https://wikipedia.org/wiki/FLAC) | Free Lossless Audio Codec | <img src="./image/logo-FLAC.svg" width="80" alt="FLAC logo"> |
56
- | [MP2](https://wikipedia.org/wiki/MPEG-1_Audio_Layer_II) | MPEG-1 Audio Layer II (predecessor to MP3) | |
57
- | [Matroska](https://wikipedia.org/wiki/Matroska) | Matroska (EBML), mka, mkv | <img src="./image/logo-Matroska_2010.svg" width="80" alt="Matroska logo"> |
58
- | [MP3](https://wikipedia.org/wiki/MP3) | MPEG-1 / MPEG-2 Audio Layer III | <img src="./image/logo-Mp3.svg" width="80" alt="MP3 logo"> |
59
- | [MPC](https://wikipedia.org/wiki/Musepack) | Musepack SV7 | <img src="./image/logo-Musepack.svg" width="80" alt="musepack logo"> |
60
- | [MPEG 4](https://wikipedia.org/wiki/MPEG-4) | mp4, m4a, m4v | <img src="./image/logo-MPEG4-350581.svg" width="80" alt="mpeg 4 logo"> |
61
- | [Ogg](https://en.wikipedia.org/wiki/Ogg) | Open container format | <img src="./image/logo-Ogg.svg" width="80" alt="Ogg logo"> |
62
- | [Opus](https://wikipedia.org/wiki/Opus_(audio_format)) | Low-latency, high-quality codec for speech and music | <img src="./image/logo-Opus.svg" width="80" alt="Opus logo"> |
63
- | [Speex](https://wikipedia.org/wiki/Speex) | Open-source speech codec optimized for VoIP | <img src="./image/logo-Speex_2006.svg" width="80" alt="Speex logo"> |
64
- | [Theora](https://en.wikipedia.org/wiki/Theora) | Open video compression format (typically paired with Ogg) | <img src="./image/logo-Theora_2007.svg" width="70" alt="Theora logo"> |
65
- | [Vorbis](https://wikipedia.org/wiki/Ogg_Vorbis) | Vorbis audio compression | <img src="./image/logo-Vorbis_many_fish_2005.svg" width="70" alt="Vorbis logo"> |
66
- | [WAV](https://wikipedia.org/wiki/WAV) | Uncompressed PCM audio in RIFF container | |
67
- | [WebM](https://wikipedia.org/wiki/WebM) | WebM | <img src="./image/logo-WebM.svg" width="80" alt="Matroska logo"> |
68
- | [WV](https://wikipedia.org/wiki/WavPack) | WavPack | <img src="./image/logo-wavpack.svg" width="60" alt="WavPack logo"> |
69
- | [WMA](https://wikipedia.org/wiki/Windows_Media_Audio) | Windows Media Audio | <img src="./image/logo-Windows_Media_Player_simplified.svg" width="40" alt="Windows Media logo"> |
70
-
71
- ### Supported tag headers
72
-
73
- Following tag header formats are supported:
74
- - [APE](https://wikipedia.org/wiki/APE_tag)
75
- - [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format)
76
- - EXIF 2.3
77
- - [ID3](https://wikipedia.org/wiki/ID3): ID3v1, ID3v1.1, ID3v2.2, [ID3v2.3](http://id3.org/id3v2.3.0) & [ID3v2.4](http://id3.org/id3v2.4.0-frames)
78
- - [iTunes](https://github.com/sergiomb2/libmp4v2/wiki/iTunesMetadata)
79
- - [RIFF](https://wikipedia.org/wiki/Resource_Interchange_File_Format)/INFO
80
- - [Vorbis comment](https://wikipedia.org/wiki/Vorbis_comment)
81
- - [AIFF](https://wikipedia.org/wiki/Audio_Interchange_File_Format)
82
-
83
- Following lyric formats are supported:
84
- - [LRC](https://en.wikipedia.org/wiki/LRC_(file_format))
85
- - Synchronized lyrics (SYLT)
86
- - Unsynchronized lyrics (USULT)
87
-
88
- Support for [MusicBrainz](https://musicbrainz.org/) tags as written by [Picard](https://picard.musicbrainz.org/).
89
- [ReplayGain](https://wiki.hydrogenaud.io/index.php?title=ReplayGain) tags are supported.
90
-
91
- ### Audio format & encoding details
92
-
93
- Support for encoding / format details:
94
- - [Bit rate](https://wikipedia.org/wiki/Bit_rate)
95
- - [Audio bit depth](https://wikipedia.org/wiki/Audio_bit_depth)
96
- - Duration
97
- - Encoding profile (e.g. [CBR](https://en.wikipedia.org/wiki/Constant_bitrate), V0, V2)
98
-
99
-
100
- ## Online demo's
101
- - [<img src="https://raw.githubusercontent.com/Borewit/audio-tag-analyzer/master/src/assets/icon/audio-tag-analyzer.svg" width="40">Audio Tag Analyzer](https://audio-tag-analyzer.netlify.app/)
102
- - [<img src="https://cdn.sanity.io/images/3do82whm/next/ba8c847f13a5fa39d88f8bc9b7846b7886531b18-2500x2500.svg" width="40"> Webamp](https://webamp.org/)
103
- - [ICY Radio Stream Player](https://icy-radio-stream-player.netlify.app/)
104
- - Expected to be released soon: [Overtone](https://overtone.pro/) by [Johannes Schickling](https://github.com/schickling)
105
-
106
- ## Usage
107
-
108
- ### Installation
109
- Install using [npm](http://npmjs.org/):
110
-
111
- ```bash
112
- npm install music-metadata
113
- ```
114
- or using [yarn](https://yarnpkg.com/):
115
- ```bash
116
- yarn add music-metadata
117
- ```
118
-
119
- ## API Documentation
120
-
121
- ### Overview
122
-
123
- **Node.js specific** functions to read an audio file or stream:
124
- 1. **File Parsing**: Parse audio files directly from the filesystem using the [parseFile function](#parsefile-function)
125
- 1. **Stream Parsing**: Parse audio metadata from a Node.js [Readable stream](https://nodejs.org/api/stream.html#class-streamreadable) using the [parseStream function](#parsewebstream-function).
126
-
127
- **Cross-platform** functions available to read an audio file or stream:
128
-
129
- There are multiple ways to parse (read) audio tracks:
130
- 1. **Web Stream Parsing**: Parse audio data from a web-compatible [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) using the [parseWebStream function](#parsewebstream-function).
131
- 1. **Blob Parsing**: Parse audio metadata from a (Web API) [Blob](https://developer.mozilla.org/docs/Web/API/Blob) or [File](https://developer.mozilla.org/docs/Web/API/File) using the [parseBlob function](#parseblob-function).
132
- 1. **Buffer Parsing**: Parse audio metadata from a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or [Buffer](https://nodejs.org/api/buffer.html) using the [parseBuffer function](#parsebuffer-function).
133
- 1. **Tokenizer Parsing:** Use a custom or third-party [strtok3](https://github.com/Borewit/strtok3) `ITokenizer` to parse using the [parseFromTokenizer function](#parsefromtokenizer-function).
134
-
135
- > [!NOTE]
136
- > Direct file access in Node.js is generally faster because it can 'jump' to various parts of the file without reading intermediate data.
137
-
138
- ### Node.js specific function
139
- These functions are tailored for Node.js environments and leverage Node.js-specific APIs,
140
- making them incompatible with browser-based JavaScript engines.
141
-
142
- #### `parseFile` function
143
-
144
- The `parseFile` function is intended for extracting metadata from audio files on the local filesystem in a Node.js environment.
145
- It reads the specified file, parses its audio metadata, and returns a promise that resolves with this information.
146
-
147
- ##### Syntax
148
- ```ts
149
- parseFile(filePath: string, options?: IOptions): Promise<IAudioMetadata>
150
- ```
151
-
152
- ##### Parameters
153
-
154
- - `filePath`: `string`
155
-
156
- The path to the media file from which metadata should be extracted.
157
- This should be a valid path to an audio file on the local filesystem.
158
-
159
- - `options`: [IOptions](#ioptions-interface) (optional)
160
-
161
- An optional configuration object that allows customization of the parsing process.
162
- These options can include whether to calculate the file's duration, skip embedded cover art,
163
- or other parsing behaviors.
164
-
165
- ##### Returns
166
-
167
- - `Promise<IAudioMetadata>`:
168
-
169
- A promise that resolves to an IAudioMetadata object containing metadata about the audio file.
170
- The metadata includes details such as the file format, codec, duration, bit rate, and any embedded tags like album, artist, or track information.
171
-
172
- ##### Usage Notes
173
-
174
- - This function is **Node.js-only** and relies on Node.js-specific APIs to access the filesystem.
175
-
176
- - For browser environments, consider using the [parseBlob](#parseblob-function) to parse [File object](https://developer.mozilla.org/en-US/docs/Web/API/File) objects.
177
-
178
- ##### Example:
179
-
180
- The following example demonstrates how to use the parseFile function to read metadata from an audio file:
181
- ```js
182
- import { parseFile } from 'music-metadata';
183
- import { inspect } from 'node:util';
184
-
185
- (async () => {
186
- try {
187
- const filePath = 'test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3';
188
- const metadata = await parseFile(filePath);
189
-
190
- // Output the parsed metadata to the console in a readable format
191
- console.log(inspect(metadata, { showHidden: false, depth: null }));
192
- } catch (error) {
193
- console.error('Error parsing metadata:', error.message);
194
- }
195
- })();
196
- ```
197
-
198
- #### `parseStream` function
199
-
200
- The parseStream function is used to parse metadata from an audio track provided as a Node.js [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable) stream.
201
- This is particularly useful for processing audio data that is being streamed or piped from another source, such as a web server or file system.
202
-
203
- ##### Syntax:
204
- ```ts
205
- parseStream(stream: Readable, fileInfo?: IFileInfo | string, options?: IOptions): Promise<IAudioMetadata>
206
- ```
207
-
208
- ##### Parameters:
209
-
210
- - `stream`: `Readable`:
211
-
212
- The Node.js [Readable](https://nodejs.org/api/stream.html#class-streamreadable) stream from which the audio data is read.
213
- This stream should provide the raw audio data to be analyzed.
214
-
215
- - `fileInfo`: `IFileInfo` (optional)
216
-
217
- An object containing file-related information or a string representing the MIME-type of the audio stream.
218
- The fileInfo parameter can help the parser to correctly identify the audio format and may include:
219
-
220
- - `mimeType`: A string representing the [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) (e.g., `audio/mpeg`).
221
-
222
- If provided, it is assumed the streamed file content is to be the MIME-type.
223
- If not provided, the parser will attempt to determine the format based on the content of the stream.
224
-
225
- - `size`: The total size of the audio stream in bytes (useful for streams with a known length).
226
-
227
- - `path`: A string representing the file path or filename, which can also assist in determining the format.
228
-
229
- - `options`: `IOptions` (optional)
230
-
231
- An optional object containing additional parsing options.
232
- These options allow you to customize the parsing process,
233
- such as whether to calculate the duration or skip cover art extraction.
234
-
235
- ##### Returns
236
-
237
- - `Promise<IAudioMetadata>`:
238
-
239
- A promise that resolves to an `IAudioMetadata` object containing detailed metadata about the audio stream.
240
- This metadata includes information about the format, codec, duration, bitrate, and any embedded tags such as artist, album, or track information.
241
-
242
- ##### Usage Notes
243
- - This function is only available in Node.js environments, as it relies on the [Node.js stream API](https://nodejs.org/api/stream.html).
244
-
245
- ##### Example:
246
-
247
- The following example demonstrates how to use the `parseStream` function to read metadata from an audio stream:
248
- ```js
249
- import { parseStream } from 'music-metadata';
250
- import { createReadStream } from 'fs';
251
-
252
- (async () => {
253
- try {
254
- // Create a readable stream from a file
255
- const audioStream = createReadStream('path/to/audio/file.mp3');
256
-
257
- // Parse the metadata from the stream
258
- const metadata = await parseStream(audioStream, { mimeType: 'audio/mpeg'});
259
-
260
- // Log the parsed metadata
261
- console.log(metadata);
262
- } catch (error) {
263
- console.error('Error parsing metadata:', error.message);
264
- }
265
- })();
266
-
267
- ```
268
-
269
- ### Cross-platform functions
270
- These functions are designed to be cross-platform,
271
- meaning it can be used in both Node.js and web browsers.
272
-
273
- #### `parseWebStream` function
274
-
275
- The parseWebStream function is used to extract metadata from an audio track provided as a web-compatible ReadableStream.
276
- This function is ideal for applications running in web environments, such as browsers,
277
- where audio data is streamed over the network or read from other web-based sources.
278
-
279
-
280
- ##### Syntax
281
- ```ts
282
- parseWebStream(webStream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo | string, options?: IOptions): Promise<IAudioMetadata>
283
- ```
284
-
285
- ##### Parameters
286
-
287
- - `webStream`: `ReadableStream<Uint8Array>`
288
-
289
- A [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) that provides the audio data to be parsed.
290
- This stream should emit Uint8Array chunks, representing the raw audio data.
291
-
292
- - `fileInfo`: `IFileInfo` (optional)
293
-
294
- An object containing file-related information or a string representing the MIME-type of the audio stream.
295
- The fileInfo parameter can help the parser to correctly identify the audio format and may include:
296
-
297
- - `mimeType`: A string representing the [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) (e.g., `audio/mpeg`).
298
-
299
- If provided, it is assumed the streamed file content is to be the MIME-type.
300
- If not provided, the parser will attempt to determine the format based on the content of the stream.
301
-
302
- - `size`: The total size of the audio stream in bytes (useful for streams with a known length).
303
-
304
- - `path`: A string representing the file path or filename, which can also assist in determining the format.
305
-
306
- - `options`: `IOptions` (optional)
307
-
308
- An optional object containing additional parsing options.
309
- These options allow you to customize the parsing process,
310
- such as whether to calculate the duration or skip cover art extraction.
311
-
312
- ##### Returns
313
-
314
- - `Promise<IAudioMetadata>`:
315
-
316
- A promise that resolves to an `IAudioMetadata` object containing detailed metadata about the audio stream.
317
- This metadata includes information about the format, codec, duration, bitrate, and any embedded tags such as artist, album, or track information.
318
-
319
- ##### Example
320
- Here’s an example of how to use the `parseWebStream` function to extract metadata from an audio stream in a web application:
321
-
322
- ```js
323
- import { parseWebStream } from 'music-metadata';
324
-
325
- (async () => {
326
- try {
327
- // Fetch the audio file
328
- const response = await fetch('https://github.com/Borewit/test-audio/raw/refs/heads/master/Various%20Artists%20-%202008%20-%20netBloc%20Vol%2013%20-%20Color%20in%20a%20World%20of%20Monochrome%20%5BAAC-40%5D/1.02.%20Solid%20Ground.m4a');
329
-
330
- // Extract the Content-Length header and convert it to a number
331
- const contentLength = response.headers.get('Content-Length');
332
- const size = contentLength ? parseInt(contentLength, 10) : undefined;
333
-
334
- // Parse the metadata from the web stream
335
- const metadata = await parseWebStream(response.body, {
336
- mimeType: response.headers.get('Content-Type'),
337
- size // Important to pass the content-length
338
- });
339
-
340
- console.log(metadata);
341
- } catch (error) {
342
- console.error('Error parsing metadata:', error.message);
343
- }
344
- })();
345
- ```
346
-
347
- The example uses the [`fetch` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to retrieve an audio file from a URL.
348
- The `response.body` provides a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) that is then passed to `parseWebStream`.
349
-
350
- #### `parseBlob` function
351
-
352
- Parses metadata from an audio file represented as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
353
- This function is suitable for use in environments that support the ReadableStreamBYOBReader, which is **available in Node.js 20** and above.
354
-
355
- ##### Syntax
356
- ```ts
357
- parseBlob(blob: Blob, options?: IOptions = {}): Promise<IAudioMetadata>
358
- ```
359
-
360
- ##### Parameters
361
-
362
- - `blob`: [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
363
-
364
- The Blob object containing the audio data to be parsed.
365
- This can be a file or any binary data. If the Blob is an instance of File, its name will be used as the file path in the metadata.
366
-
367
- - `options`: [IOptions](#ioptions-interface) (optional)
368
-
369
- An optional configuration object that specifies parsing options.
370
-
371
- ##### Returns
372
-
373
- - `Promise<IAudioMetadata>`:
374
-
375
- A promise that resolves to the metadata of the audio file.
376
-
377
- ##### Example
378
-
379
- ```js
380
- import { parseBlob } from 'music-metadata';
381
-
382
- (async () => {
383
- const fileInput = document.querySelector('input[type="file"]');
384
- const file = fileInput.files[0];
385
-
386
- try {
387
- const metadata = await parseBlob(file);
388
- console.log(metadata);
389
- } catch (error) {
390
- console.error('Error parsing metadata:', error.message);
391
- }
392
- })();
393
- ```
394
-
395
- #### `parseBuffer` function
396
-
397
- Parses metadata from an audio file where the audio data is held in a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or [Buffer](https://nodejs.org/api/buffer.html).
398
- This function is particularly useful when you already have audio data in memory.
399
-
400
- ##### Syntax
401
- ```ts
402
- parseBuffer(buffer: Uint8Array, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>
403
- ```
404
-
405
- ##### Parameters
406
- - `uint8Array`: [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
407
-
408
- A [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) containing the audio data to be parsed.
409
-
410
- - `fileInfo`: `IFileInfo` | `string` (optional)
411
-
412
- An object containing file information such as mimeType and size.
413
- Alternatively, you can pass a MIME-type string directly.
414
- This helps the parser understand the format of the audio data.
415
-
416
- - `options`: [IOptions](#ioptions-interface) (optional)
417
-
418
- An optional configuration object that specifies parsing options.
419
-
420
- ##### Returns
421
- - `Promise<IAudioMetadata>`:
422
-
423
- A promise that resolves to the metadata of the audio file.
424
-
425
-
426
- ##### Example
427
-
428
- ```js
429
- import { parseBuffer } from 'music-metadata';
430
- import fs from 'fs';
431
-
432
- (async () => {
433
- const buffer = fs.readFileSync('path/to/audio/file.mp3');
434
-
435
- try {
436
- const metadata = await parseBuffer(buffer, { mimeType: 'audio/mpeg' });
437
- console.log(metadata);
438
- } catch (error) {
439
- console.error('Error parsing metadata:', error.message);
440
- }
441
- })();
442
- ```
443
-
444
- #### `parseFromTokenizer` function
445
- Parses metadata from an audio source that implements the [strtok3](https://github.com/Borewit/strtok3) ITokenizer interface.
446
- This is a low-level function that provides flexibility for advanced use cases,
447
- such as parsing metadata from streaming audio or custom data sources.
448
-
449
- This also enables special read modules like:
450
- - [streaming-http-token-reader](https://github.com/Borewit/streaming-http-token-reader) for chunked HTTP(S) reading, using [HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests).
451
-
452
- ##### Syntax
453
- ```ts
454
- parseFromTokenizer(tokenizer: ITokenizer, options?: IOptions): Promise<IAudioMetadata>
455
- ```
456
-
457
- ##### Parameters
458
- - `tokenizer: ITokenizer`
459
-
460
- An instance of an ITokenizer that provides access to the audio data.
461
- The tokenizer abstracts the reading process, enabling support for various types of sources, including streams, buffers, or custom data readers.
462
-
463
- - `options`: [IOptions](#ioptions-interface) (optional)
464
-
465
- An optional configuration object that specifies parsing options.
466
-
467
- ##### Returns
468
- - `Promise<IAudioMetadata>`:
469
-
470
- A promise that resolves to the metadata of the audio source, including information like the title, artist, album, and more.
471
-
472
-
473
- ##### Example
474
- ````js
475
- import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
476
- import { S3Client } from '@aws-sdk/client-s3';
477
- import { makeTokenizer } from '@tokenizer/s3';
478
- import { parseFromTokenizer as mmParseFromTokenizer } from 'music-metadata';
479
-
480
- // Configure the S3 client
481
- const s3 = new S3Client({
482
- region: 'eu-west-2',
483
- credentials: fromNodeProviderChain(),
484
- });
485
-
486
- // Helper function to create a tokenizer for S3 objects
487
- async function makeS3TestDataTokenizer(key, options) {
488
- return await makeTokenizer(s3, {
489
- Bucket: 'music-metadata',
490
- Key: key,
491
- }, options);
492
- }
493
-
494
- // Function to read and log metadata from an S3 object
495
- async function readMetadata() {
496
- try {
497
- // Create a tokenizer for the specified S3 object
498
- const tokenizer = await makeS3TestDataTokenizer('path/to/audio/file.mp3', { disableChunked: false });
499
-
500
- // Parse the metadata from the tokenizer
501
- const metadata = await mmParseFromTokenizer(tokenizer);
502
-
503
- // Log the retrieved metadata
504
- console.log(metadata);
505
- } catch (error) {
506
- console.error('Error parsing metadata:', error.message);
507
- }
508
- }
509
-
510
- // Execute the metadata reading function
511
- readMetadata();
512
- ````
513
- ##### Additional Resources
514
- - [strtok3](https://github.com/Borewit/strtok3) - Learn more about the `ITokenizer` interface and how to implement it for various use cases.
515
- - [AWS SDK for JavaScript](https://aws.amazon.com/sdk-for-javascript/) - Documentation on using the AWS SDK to interact with S3 and other AWS services.
516
- - [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3) - Example of `ITokenizer` implementation.
517
-
518
- ### Handling Parse Errors
519
-
520
- `music-metadata` provides a robust and extensible error handling system with custom error classes that inherit from the standard JavaScript `Error`.
521
- All possible parsing errors are part of a union type `UnionOfParseErrors`, ensuring that every error scenario is accounted for in your code.
522
-
523
- #### Union of Parse Errors
524
-
525
- All parsing errors extend from the base class `ParseError` and are included in the `UnionOfParseErrors` type:
526
- ```ts
527
- export type UnionOfParseErrors =
528
- | CouldNotDetermineFileTypeError
529
- | UnsupportedFileTypeError
530
- | UnexpectedFileContentError
531
- | FieldDecodingError
532
- | InternalParserError;
533
- ```
534
-
535
- #### Error Types
536
-
537
- - `CouldNotDetermineFileTypeError`: Raised when the file type cannot be determined.
538
- - `UnsupportedFileTypeError`: Raised when an unsupported file type is encountered.
539
- - `UnexpectedFileContentError`: Raised when the file content does not match the expected format.
540
- - `FieldDecodingError`: Raised when a specific field in the file cannot be decoded.
541
- - `InternalParserError`: Raised for internal parser errors.
542
-
543
- ### Other functions
544
-
545
- #### `orderTags` function
546
-
547
- Utility to Converts the native tags to a dictionary index on the tag identifier
548
-
549
- ```ts
550
- orderTags(nativeTags: ITag[]): [tagId: string]: any[]
551
- ```
552
-
553
- ```js
554
- import { parseFile, orderTags } from 'music-metadata';
555
- import { inspect } from 'util';
556
-
557
- (async () => {
558
- try {
559
- const metadata = await parseFile('../test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
560
- const orderedTags = orderTags(metadata.native['ID3v2.3']);
561
- console.log(inspect(orderedTags, { showHidden: false, depth: null }));
562
- } catch (error) {
563
- console.error(error.message);
564
- }
565
- })();
566
- ```
567
-
568
- #### `ratingToStars` function
569
-
570
- 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.
571
-
572
- ```ts
573
- ratingToStars(rating: number): number
574
- ```
575
- #### `selectCover` function
576
-
577
- Select cover image based on image type field, otherwise the first picture in file.
578
-
579
- ```ts
580
- export function selectCover(pictures?: IPicture[]): IPicture | null
581
- ```
582
-
583
- ```js
584
- import { parseFile, selectCover } from 'music-metadata';
585
-
586
- (async () => {
587
- const {common} = await parseFile(filePath);
588
- const cover = selectCover(common.picture); // pick the cover image
589
- }
590
- )();
591
- ```
592
-
593
- #### `getSupportedMimeTypes` function
594
-
595
- Returns a list of supported MIME-types. This may include some MIME-types which are not formally recognized.
596
-
597
- ### `IOptions` Interface
598
- - `duration`: `boolean` (default: `false`)
599
-
600
- When `true`, the parser will read the entire media file _if necessary_ to determine the duration.
601
- This is only applicable in cases where duration cannot be reliably inferred without full file analysis.
602
- Note that enabling this option **does not guarantee** that duration will be available,
603
- only that the parser will attempt to calculate it when possible, even if it requires reading the full file.
604
-
605
- - `mkvUseIndex`: `boolean` (default: `false`)
606
-
607
- When `true`, the parser uses the SeekHead index in Matroska (MKV) files to skip segment and cluster elements.
608
- This experimental feature can improve performance, but:
609
- - Metadata not listed in the SeekHead may be skipped.
610
- - If the SeekHead is missing, this option has no effect.
611
-
612
- - `observer`: `(update: MetadataEvent) => void;`:
613
-
614
- Callback function triggered when common tags or format properties are updated during parsing.
615
- Allows real-time monitoring of metadata as it becomes available.
616
-
617
- - `skipCovers`: `boolean` (default: `false`)
618
-
619
- When `true`, embedded cover art (images) will not be extracted.
620
- Useful for reducing memory and processing when cover images are unnecessary.
621
-
622
- - `skipPostHeaders`: `boolean` (default: `false`)
623
- When `true`, tag headers located at the end of the file will not be read.
624
- This is particularly beneficial for streaming input, as it avoids the need to read the entire stream.
625
-
626
- > [!NOTE]
627
- > - The `duration` option is typically included in most cases, but setting it to true ensures that the entire file is parsed if necessary to get an accurate duration.
628
- > - Using `mkvUseIndex` can improve performance in Matroska files, but be aware of potential side effects, such as missing metadata due to skipped elements.
629
-
630
-
631
- ### `IAudioMetadata` interface
632
-
633
- If the returned promise resolves, the metadata (TypeScript `IAudioMetadata` interface) contains:
634
- - [`metadata.format`](#metadataformat) Audio format information
635
- - [`metadata.common`](#metadatacommon) Is a generic (abstract) way of reading metadata information.
636
- - [`metadata.trackInfo`](#metadatatrackInfo) Is a generic (abstract) way of reading metadata information.
637
- - `metadata.native` List of native (original) tags found in the parsed audio file.
638
-
639
- #### `metadata.format`
640
-
641
- The questionmark `?` indicates the property is optional.
642
-
643
- Audio format information. Defined in the TypeScript `IFormat` interface:
644
- - `format.container?: string` Audio encoding format. e.g.: 'flac'
645
- - `format.codec?` Name of the codec (algorithm used for the audio compression)
646
- - `format.codecProfile?: string` Codec profile / settings
647
- - `format.tagTypes?: TagType[]` List of tagging formats found in parsed audio file
648
- - `format.duration?: number` Duration in seconds
649
- - `format.bitrate?: number` Number bits per second of encoded audio file
650
- - `format.sampleRate?: number` Sampling rate in Samples per second (S/s)
651
- - `format.bitsPerSample?: number` Audio bit depth
652
- - `format.lossless?: boolean` True if lossless, false for lossy encoding
653
- - `format.numberOfChannels?: number` Number of audio channels
654
- - `format.creationTime?: Date` Track creation time
655
- - `format.modificationTime?: Date` Track modification / tag update time
656
- - `format.trackGain?: number` Track gain in dB
657
- - `format.albumGain?: number` Album gain in dB
658
-
659
- #### `metadata.trackInfo`
660
-
661
- To support advanced containers like [Matroska](https://wikipedia.org/wiki/Matroska) or [MPEG-4](https://en.wikipedia.org/wiki/MPEG-4), which may contain multiple audio and video tracks, the **experimental**- `metadata.trackInfo` has been added,
662
-
663
- `metadata.trackInfo` is either `undefined` or has an **array** of [trackInfo](#trackinfo)
664
-
665
- ##### trackInfo
666
-
667
- Audio format information. Defined in the TypeScript `IFormat` interface:
668
- - `trackInfo.type?: TrackType` Track type
669
- - `trackInfo.codecName?: string` Codec name
670
- - `trackInfo.codecSettings?: string` Codec settings
671
- - `trackInfo.flagEnabled?: boolean` Set if the track is usable, default: `true`
672
- - `trackInfo.flagDefault?: boolean` Set if that track (audio, video or subs) SHOULD be active if no language found matches the user preference.
673
- - `trackInfo.flagLacing?: boolean` Set if the track **may** contain blocks using lacing
674
- - `trackInfo.name?: string` A human-readable track name.
675
- - `trackInfo.language?: string` Specifies the language of the track
676
- - `trackInfo.audio?: IAudioTrack`, see [`trackInfo.audioTrack`](#trackinfoaudiotrack)
677
- - `trackInfo.video?: IVideoTrack`, see [`trackInfo.videoTrack`](#trackinfovideotrack)
678
-
679
- ##### `trackInfo.audioTrack`
680
-
681
- - `audioTrack.samplingFrequency?: number`
682
- - `audioTrack.outputSamplingFrequency?: number`
683
- - `audioTrack.channels?: number`
684
- - `audioTrack.channelPositions?: Buffer`
685
- - `audioTrack.bitDepth?: number`
686
-
687
- ##### `trackInfo.videoTrack`
688
-
689
- - `videoTrack.flagInterlaced?: boolean`
690
- - `videoTrack.stereoMode?: number`
691
- - `videoTrack.pixelWidth?: number`
692
- - `videoTrack.pixelHeight?: number`
693
- - `videoTrack.displayWidth?: number`
694
- - `videoTrack.displayHeight?: number`
695
- - `videoTrack.displayUnit?: number`
696
- - `videoTrack.aspectRatioType?: number`
697
- - `videoTrack.colourSpace?: Buffer`
698
- - `videoTrack.gammaValue?: number`
699
-
700
- #### `metadata.common`
701
-
702
- [Common tag documentation](doc/common_metadata.md) is automatically generated.
703
-
704
- ## Examples
705
-
706
- 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.
707
- ```js
708
- import { parseStream } from 'music-metadata';
709
- import { inspect } from 'util';
710
-
711
- (async () => {
712
- const metadata = await parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838}, {duration: true});
713
- console.log(inspect(metadata, {showHidden: false, depth: null}));
714
- someReadStream.close();
715
- }
716
- )();
717
- ```
718
-
719
- ### Access cover art
720
-
721
- Via `metadata.common.picture` you can access an array of cover art if present.
722
- Each picture has this interface:
723
-
724
- ```ts
725
- /**
726
- * Attached picture, typically used for cover art
727
- */
728
- export interface IPicture {
729
- /**
730
- * Image mime type
731
- */
732
- format: string;
733
- /**
734
- * Image data
735
- */
736
- data: Buffer;
737
- /**
738
- * Optional description
739
- */
740
- description?: string;
741
- /**
742
- * Picture type
743
- */
744
- type?: string;
745
- }
746
- ```
747
-
748
- To assign `img` HTML-object you can do something like:
749
- ```js
750
- import {uint8ArrayToBase64} from 'uint8array-extras';
751
-
752
- img.src = `data:${picture.format};base64,${uint8ArrayToBase64(picture.data)}`;
753
- ```
754
-
755
- ## Dependencies
756
-
757
- Dependency diagram:
758
- ```mermaid
759
- graph TD;
760
- MMN("music-metadata (Node.js entry point)")-->MMP
761
- MMN-->FTN
762
- MMP("music-metadata (primary entry point)")-->S(strtok3)
763
- MMP-->TY(token-types)
764
- MMP-->FTP
765
- MMP-->UAE
766
- FTN("file-type (Node.js entry point)")-->FTP
767
- FTP("file-type (primary entry point)")-->S
768
- S(strtok3)-->TO("@tokenizer/token")
769
- TY(token-types)-->TO
770
- TY-->IE("ieee754")
771
- FTP-->TY
772
- NS("node:stream")
773
- FTN-->NS
774
- FTP-->UAE(uint8array-extras)
775
- style NS fill:#F88,stroke:#A44
776
- style IE fill:#CCC,stroke:#888
777
- style FTN fill:#FAA,stroke:#A44
778
- style MMN fill:#FAA,stroke:#A44
779
- ```
780
-
781
- Dependency list:
782
- - [tokenizer-token](https://github.com/Borewit/tokenizer-token)
783
- - [strtok3](https://github.com/Borewit/strtok3)
784
- - [token-types](https://github.com/Borewit/token-types)
785
- - [file-type](https://github.com/sindresorhus/file-type)
786
- - [@tokenizer-token](https://github.com/Borewit/tokenizer-token)
787
-
788
- ## CommonJS backward compatibility
789
-
790
- Using Node.js ≥ 22, which is support loading ESM module via require
791
- ```js
792
- const mm = require('music-metadata');
793
- ```
794
-
795
- For older Node.js version < 22, you need to dynamically import **music-metadata**:
796
- ```js
797
- (async () => {
798
- // Dynamically loads the ESM module in a CommonJS project
799
- const mm = await import('music-metadata');
800
- })();
801
- ```
802
-
803
- For CommonJS TypeScript projects, I recommend to avoid using `commonjs` for the TypeScript compiler `module` option,
804
- and either use `node16` or `nodenext`, which enable utilizing [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import).
805
-
806
- If you do want to use the classic `commonjs` option, this is how you can get the _dynamic import_ to work.
807
-
808
- ```js
809
- import {loadEsm} from 'load-esm';
810
-
811
- (async () => {
812
- // Dynamically loads the ESM module in a CommonJS project
813
- const mm = await loadEsm<typeof import('music-metadata')>('music-metadata');
814
- })();
815
- ```
816
-
817
- When you use Node.js version ≥ 22, which supports loading ESM modules via require, this compensates for that issue.
818
-
819
- ## Frequently Asked Questions
820
-
821
- ### How can I traverse (a long) list of files?
822
-
823
- What is important that file parsing should be done in a sequential manner.
824
- In a plain loop, due to the asynchronous character (like most JavaScript functions), it would cause all the files to run in parallel which is will cause your application to hang in no time.
825
- There are multiple ways of achieving this:
826
-
827
- 1. Using recursion
828
-
829
- ```js
830
- import { parseFile } from 'music-metadata';
831
-
832
- function parseFiles(audioFiles) {
833
-
834
- const audioFile = audioFiles.shift();
835
-
836
- if (audioFile) {
837
- return parseFile(audioFile).then(metadata => {
838
- // Do great things with the metadata
839
- return parseFiles(audioFiles); // process rest of the files AFTER we are finished
840
- })
841
- }
842
- }
843
-
844
- ```
845
-
846
- 1. Use async/await
847
-
848
- Use [async/await](https://javascript.info/async-await)
849
-
850
- ```js
851
- import { parseFile } from 'music-metadata';
852
-
853
- // it is required to declare the function 'async' to allow the use of await
854
- async function parseFiles(audioFiles) {
855
-
856
- for (const audioFile of audioFiles) {
857
-
858
- // await will ensure the metadata parsing is completed before we move on to the next file
859
- const metadata = await parseFile(audioFile);
860
- // Do great things with the metadata
861
- }
862
- }
863
- ```
864
-
865
- ### Using music-metadata with TypeScript and module-resolution set to bundler.
866
-
867
- If the TypeScript compiler option [`moduleResolution`](https://www.typescriptlang.org/tsconfig/#moduleResolution)
868
- is set to `"bundler"`, it does not set the ECMAScript `"node"` condition, causing the Node specific function fail to import.
869
-
870
- This is the case using Next.js. See [issue #2370](https://github.com/Borewit/music-metadata/issues/2370) how to resolve that.
871
-
872
- ## Licence
873
-
874
- This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.
1
+ [![CI](https://github.com/Borewit/music-metadata/actions/workflows/ci.yml/badge.svg)](https://github.com/Borewit/music-metadata/actions/workflows/ci.yml)
2
+ [![NPM version](https://img.shields.io/npm/v/music-metadata.svg)](https://npmjs.org/package/music-metadata)
3
+ [![npm downloads](http://img.shields.io/npm/dm/music-metadata.svg)](https://npmcharts.com/compare/music-metadata?start=600&interval=30)
4
+ [![Coverage Status](https://coveralls.io/repos/github/Borewit/music-metadata/badge.svg?branch=master)](https://coveralls.io/github/Borewit/music-metadata?branch=master)
5
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/57d731b05c9e41889a2a17cb4b0384d7)](https://app.codacy.com/app/Borewit/music-metadata?utm_source=github.com&utm_medium=referral&utm_content=Borewit/music-metadata&utm_campaign=Badge_Grade_Dashboard)
6
+ [![CodeQL](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml)
7
+ [![DeepScan grade](https://deepscan.io/api/teams/5165/projects/6938/branches/61821/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=5165&pid=6938&bid=61821)
8
+ [![Known Vulnerabilities](https://snyk.io/test/github/Borewit/music-metadata/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/music-metadata?targetFile=package.json)
9
+ [![Discord](https://img.shields.io/discord/460524735235883049.svg)](https://discord.gg/KyBr6sb)
10
+
11
+ # music-metadata
12
+
13
+ Key features:
14
+ - **Comprehensive Format Support**: Supports popular audio formats like MP3, MP4, FLAC, Ogg, WAV, AIFF, and more.
15
+ - **Extensive Metadata Extraction**: Extracts detailed metadata, including ID3v1, ID3v2, APE, Vorbis, and iTunes/MP4 tags.
16
+ - **Streaming Support**: Efficiently handles large audio files by reading metadata from streams, making it suitable for server-side and browser-based applications.
17
+ - **Promise-Based API**: Provides a modern, promise-based API for easy integration into asynchronous workflows.
18
+ - **Cross-Platform**: Works in both [Node.js](https://nodejs.org/) and browser environments with the help of bundlers like [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/introduction/).
19
+
20
+ The [`music-metadata`](https://github.com/Borewit/music-metadata) module is ideal for developers working on media applications, music players, or any project that requires access to detailed audio file metadata.
21
+
22
+ ## Compatibility
23
+
24
+ Module: version 8 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
25
+ The distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.
26
+
27
+ > [!NOTE]
28
+ > See also [CommonJS backward Compatibility](#commonjs-backward-compatibility)
29
+
30
+ This module requires a [Node.js ≥ 18](https://nodejs.org/en/about/previous-releases) engine.
31
+ It can also be used in a browser environment when bundled with a module bundler.
32
+
33
+ ## Support the Project
34
+ If you find this project useful and would like to support its development, consider sponsoring or contributing:
35
+
36
+ - [Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
37
+
38
+ - Buy me a coffee:
39
+
40
+ <a href="https://www.buymeacoffee.com/borewit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy me A coffee" height="41" width="174"></a>
41
+
42
+ ## Features
43
+
44
+ ### Support for audio file types
45
+
46
+ | Audio format | Description | Logo |
47
+ |---------------------------------------------------------------------------|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------|
48
+ | [AIFF / AIFF-C](https://wikipedia.org/wiki/Audio_Interchange_File_Format) | Audio Interchange File Format | <img src="./image/logo-Apple_Computer_Logo_rainbow.svg" width="40" alt="Apple rainbow logo"> |
49
+ | [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) | ADTS / Advanced Audio Coding | <img src="./image/logo-AAC_original.svg" width="40" alt="AAC logo"> |
50
+ | [APE](https://wikipedia.org/wiki/Monkey's_Audio) | Monkey's Audio | <img src="./image/logo-APE-AI.svg" width="40" alt="Monkey's Audio logo"> |
51
+ | [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format) | Advanced Systems Format | |
52
+ | [BWF](https://en.wikipedia.org/wiki/Broadcast_Wave_Format) | Extended WAV format for broadcast and archiving | |
53
+ | [DSDIFF](https://wikipedia.org/wiki/Direct_Stream_Digital) | Philips DSDIFF | <img src="./image/logo-DSD.svg" width="80" alt="DSD logo"> |
54
+ | [DSF](https://wikipedia.org/wiki/Direct_Stream_Digital) | Sony's DSD Stream File | <img src="./image/logo-DSD.svg" width="80" alt="DSD logo"> |
55
+ | [FLAC](https://wikipedia.org/wiki/FLAC) | Free Lossless Audio Codec | <img src="./image/logo-FLAC.svg" width="80" alt="FLAC logo"> |
56
+ | [MP2](https://wikipedia.org/wiki/MPEG-1_Audio_Layer_II) | MPEG-1 Audio Layer II (predecessor to MP3) | |
57
+ | [Matroska](https://wikipedia.org/wiki/Matroska) | Matroska (EBML), mka, mkv | <img src="./image/logo-Matroska_2010.svg" width="80" alt="Matroska logo"> |
58
+ | [MP3](https://wikipedia.org/wiki/MP3) | MPEG-1 / MPEG-2 Audio Layer III | <img src="./image/logo-Mp3.svg" width="80" alt="MP3 logo"> |
59
+ | [MPC](https://wikipedia.org/wiki/Musepack) | Musepack SV7 | <img src="./image/logo-Musepack.svg" width="80" alt="musepack logo"> |
60
+ | [MPEG 4](https://wikipedia.org/wiki/MPEG-4) | mp4, m4a, m4v | <img src="./image/logo-MPEG4-350581.svg" width="80" alt="mpeg 4 logo"> |
61
+ | [Ogg](https://en.wikipedia.org/wiki/Ogg) | Open container format | <img src="./image/logo-Ogg.svg" width="80" alt="Ogg logo"> |
62
+ | [Opus](https://wikipedia.org/wiki/Opus_(audio_format)) | Low-latency, high-quality codec for speech and music | <img src="./image/logo-Opus.svg" width="80" alt="Opus logo"> |
63
+ | [Speex](https://wikipedia.org/wiki/Speex) | Open-source speech codec optimized for VoIP | <img src="./image/logo-Speex_2006.svg" width="80" alt="Speex logo"> |
64
+ | [Theora](https://en.wikipedia.org/wiki/Theora) | Open video compression format (typically paired with Ogg) | <img src="./image/logo-Theora_2007.svg" width="70" alt="Theora logo"> |
65
+ | [Vorbis](https://wikipedia.org/wiki/Ogg_Vorbis) | Vorbis audio compression | <img src="./image/logo-Vorbis_many_fish_2005.svg" width="70" alt="Vorbis logo"> |
66
+ | [WAV](https://wikipedia.org/wiki/WAV) | Uncompressed PCM audio in RIFF container | |
67
+ | [WebM](https://wikipedia.org/wiki/WebM) | WebM | <img src="./image/logo-WebM.svg" width="80" alt="Matroska logo"> |
68
+ | [WV](https://wikipedia.org/wiki/WavPack) | WavPack | <img src="./image/logo-wavpack.svg" width="60" alt="WavPack logo"> |
69
+ | [WMA](https://wikipedia.org/wiki/Windows_Media_Audio) | Windows Media Audio | <img src="./image/logo-Windows_Media_Player_simplified.svg" width="40" alt="Windows Media logo"> |
70
+
71
+ ### Supported tag headers
72
+
73
+ Following tag header formats are supported:
74
+ - [APE](https://wikipedia.org/wiki/APE_tag)
75
+ - [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format)
76
+ - EXIF 2.3
77
+ - [ID3](https://wikipedia.org/wiki/ID3): ID3v1, ID3v1.1, ID3v2.2, [ID3v2.3](http://id3.org/id3v2.3.0) & [ID3v2.4](http://id3.org/id3v2.4.0-frames)
78
+ - [iTunes](https://github.com/sergiomb2/libmp4v2/wiki/iTunesMetadata)
79
+ - [RIFF](https://wikipedia.org/wiki/Resource_Interchange_File_Format)/INFO
80
+ - [Vorbis comment](https://wikipedia.org/wiki/Vorbis_comment)
81
+ - [AIFF](https://wikipedia.org/wiki/Audio_Interchange_File_Format)
82
+
83
+ Following lyric formats are supported:
84
+ - [LRC](https://en.wikipedia.org/wiki/LRC_(file_format))
85
+ - Synchronized lyrics (SYLT)
86
+ - Unsynchronized lyrics (USULT)
87
+
88
+ Support for [MusicBrainz](https://musicbrainz.org/) tags as written by [Picard](https://picard.musicbrainz.org/).
89
+ [ReplayGain](https://wiki.hydrogenaud.io/index.php?title=ReplayGain) tags are supported.
90
+
91
+ ### Audio format & encoding details
92
+
93
+ Support for encoding / format details:
94
+ - [Bit rate](https://wikipedia.org/wiki/Bit_rate)
95
+ - [Audio bit depth](https://wikipedia.org/wiki/Audio_bit_depth)
96
+ - Duration
97
+ - Encoding profile (e.g. [CBR](https://en.wikipedia.org/wiki/Constant_bitrate), V0, V2)
98
+
99
+
100
+ ## Online demo's
101
+ - [<img src="https://raw.githubusercontent.com/Borewit/audio-tag-analyzer/master/src/assets/icon/audio-tag-analyzer.svg" width="40">Audio Tag Analyzer](https://audio-tag-analyzer.netlify.app/)
102
+ - [<img src="https://cdn.sanity.io/images/3do82whm/next/ba8c847f13a5fa39d88f8bc9b7846b7886531b18-2500x2500.svg" width="40"> Webamp](https://webamp.org/)
103
+ - [ICY Radio Stream Player](https://icy-radio-stream-player.netlify.app/)
104
+ - Expected to be released soon: [Overtone](https://overtone.pro/) by [Johannes Schickling](https://github.com/schickling)
105
+
106
+ ## Usage
107
+
108
+ ### Installation
109
+ Install using [npm](http://npmjs.org/):
110
+
111
+ ```bash
112
+ npm install music-metadata
113
+ ```
114
+ or using [yarn](https://yarnpkg.com/):
115
+ ```bash
116
+ yarn add music-metadata
117
+ ```
118
+
119
+ ## API Documentation
120
+
121
+ ### Overview
122
+
123
+ **Node.js specific** functions to read an audio file or stream:
124
+ 1. **File Parsing**: Parse audio files directly from the filesystem using the [parseFile function](#parsefile-function)
125
+ 1. **Stream Parsing**: Parse audio metadata from a Node.js [Readable stream](https://nodejs.org/api/stream.html#class-streamreadable) using the [parseStream function](#parsewebstream-function).
126
+
127
+ **Cross-platform** functions available to read an audio file or stream:
128
+
129
+ There are multiple ways to parse (read) audio tracks:
130
+ 1. **Web Stream Parsing**: Parse audio data from a web-compatible [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) using the [parseWebStream function](#parsewebstream-function).
131
+ 1. **Blob Parsing**: Parse audio metadata from a (Web API) [Blob](https://developer.mozilla.org/docs/Web/API/Blob) or [File](https://developer.mozilla.org/docs/Web/API/File) using the [parseBlob function](#parseblob-function).
132
+ 1. **Buffer Parsing**: Parse audio metadata from a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or [Buffer](https://nodejs.org/api/buffer.html) using the [parseBuffer function](#parsebuffer-function).
133
+ 1. **Tokenizer Parsing:** Use a custom or third-party [strtok3](https://github.com/Borewit/strtok3) `ITokenizer` to parse using the [parseFromTokenizer function](#parsefromtokenizer-function).
134
+
135
+ > [!NOTE]
136
+ > Direct file access in Node.js is generally faster because it can 'jump' to various parts of the file without reading intermediate data.
137
+
138
+ ### Node.js specific function
139
+ These functions are tailored for Node.js environments and leverage Node.js-specific APIs,
140
+ making them incompatible with browser-based JavaScript engines.
141
+
142
+ #### `parseFile` function
143
+
144
+ The `parseFile` function is intended for extracting metadata from audio files on the local filesystem in a Node.js environment.
145
+ It reads the specified file, parses its audio metadata, and returns a promise that resolves with this information.
146
+
147
+ ##### Syntax
148
+ ```ts
149
+ parseFile(filePath: string, options?: IOptions): Promise<IAudioMetadata>
150
+ ```
151
+
152
+ ##### Parameters
153
+
154
+ - `filePath`: `string`
155
+
156
+ The path to the media file from which metadata should be extracted.
157
+ This should be a valid path to an audio file on the local filesystem.
158
+
159
+ - `options`: [IOptions](#ioptions-interface) (optional)
160
+
161
+ An optional configuration object that allows customization of the parsing process.
162
+ These options can include whether to calculate the file's duration, skip embedded cover art,
163
+ or other parsing behaviors.
164
+
165
+ ##### Returns
166
+
167
+ - `Promise<IAudioMetadata>`:
168
+
169
+ A promise that resolves to an IAudioMetadata object containing metadata about the audio file.
170
+ The metadata includes details such as the file format, codec, duration, bit rate, and any embedded tags like album, artist, or track information.
171
+
172
+ ##### Usage Notes
173
+
174
+ - This function is **Node.js-only** and relies on Node.js-specific APIs to access the filesystem.
175
+
176
+ - For browser environments, consider using the [parseBlob](#parseblob-function) to parse [File object](https://developer.mozilla.org/en-US/docs/Web/API/File) objects.
177
+
178
+ ##### Example:
179
+
180
+ The following example demonstrates how to use the parseFile function to read metadata from an audio file:
181
+ ```js
182
+ import { parseFile } from 'music-metadata';
183
+ import { inspect } from 'node:util';
184
+
185
+ (async () => {
186
+ try {
187
+ const filePath = 'test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3';
188
+ const metadata = await parseFile(filePath);
189
+
190
+ // Output the parsed metadata to the console in a readable format
191
+ console.log(inspect(metadata, { showHidden: false, depth: null }));
192
+ } catch (error) {
193
+ console.error('Error parsing metadata:', error.message);
194
+ }
195
+ })();
196
+ ```
197
+
198
+ #### `parseStream` function
199
+
200
+ The parseStream function is used to parse metadata from an audio track provided as a Node.js [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable) stream.
201
+ This is particularly useful for processing audio data that is being streamed or piped from another source, such as a web server or file system.
202
+
203
+ ##### Syntax:
204
+ ```ts
205
+ parseStream(stream: Readable, fileInfo?: IFileInfo | string, options?: IOptions): Promise<IAudioMetadata>
206
+ ```
207
+
208
+ ##### Parameters:
209
+
210
+ - `stream`: `Readable`:
211
+
212
+ The Node.js [Readable](https://nodejs.org/api/stream.html#class-streamreadable) stream from which the audio data is read.
213
+ This stream should provide the raw audio data to be analyzed.
214
+
215
+ - `fileInfo`: `IFileInfo` (optional)
216
+
217
+ An object containing file-related information or a string representing the MIME-type of the audio stream.
218
+ The fileInfo parameter can help the parser to correctly identify the audio format and may include:
219
+
220
+ - `mimeType`: A string representing the [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) (e.g., `audio/mpeg`).
221
+
222
+ If provided, it is assumed the streamed file content is to be the MIME-type.
223
+ If not provided, the parser will attempt to determine the format based on the content of the stream.
224
+
225
+ - `size`: The total size of the audio stream in bytes (useful for streams with a known length).
226
+
227
+ - `path`: A string representing the file path or filename, which can also assist in determining the format.
228
+
229
+ - `options`: `IOptions` (optional)
230
+
231
+ An optional object containing additional parsing options.
232
+ These options allow you to customize the parsing process,
233
+ such as whether to calculate the duration or skip cover art extraction.
234
+
235
+ ##### Returns
236
+
237
+ - `Promise<IAudioMetadata>`:
238
+
239
+ A promise that resolves to an `IAudioMetadata` object containing detailed metadata about the audio stream.
240
+ This metadata includes information about the format, codec, duration, bitrate, and any embedded tags such as artist, album, or track information.
241
+
242
+ ##### Usage Notes
243
+ - This function is only available in Node.js environments, as it relies on the [Node.js stream API](https://nodejs.org/api/stream.html).
244
+
245
+ ##### Example:
246
+
247
+ The following example demonstrates how to use the `parseStream` function to read metadata from an audio stream:
248
+ ```js
249
+ import { parseStream } from 'music-metadata';
250
+ import { createReadStream } from 'fs';
251
+
252
+ (async () => {
253
+ try {
254
+ // Create a readable stream from a file
255
+ const audioStream = createReadStream('path/to/audio/file.mp3');
256
+
257
+ // Parse the metadata from the stream
258
+ const metadata = await parseStream(audioStream, { mimeType: 'audio/mpeg'});
259
+
260
+ // Log the parsed metadata
261
+ console.log(metadata);
262
+ } catch (error) {
263
+ console.error('Error parsing metadata:', error.message);
264
+ }
265
+ })();
266
+
267
+ ```
268
+
269
+ ### Cross-platform functions
270
+ These functions are designed to be cross-platform,
271
+ meaning it can be used in both Node.js and web browsers.
272
+
273
+ #### `parseWebStream` function
274
+
275
+ The parseWebStream function is used to extract metadata from an audio track provided as a web-compatible ReadableStream.
276
+ This function is ideal for applications running in web environments, such as browsers,
277
+ where audio data is streamed over the network or read from other web-based sources.
278
+
279
+
280
+ ##### Syntax
281
+ ```ts
282
+ parseWebStream(webStream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo | string, options?: IOptions): Promise<IAudioMetadata>
283
+ ```
284
+
285
+ ##### Parameters
286
+
287
+ - `webStream`: `ReadableStream<Uint8Array>`
288
+
289
+ A [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) that provides the audio data to be parsed.
290
+ This stream should emit Uint8Array chunks, representing the raw audio data.
291
+
292
+ - `fileInfo`: `IFileInfo` (optional)
293
+
294
+ An object containing file-related information or a string representing the MIME-type of the audio stream.
295
+ The fileInfo parameter can help the parser to correctly identify the audio format and may include:
296
+
297
+ - `mimeType`: A string representing the [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) (e.g., `audio/mpeg`).
298
+
299
+ If provided, it is assumed the streamed file content is to be the MIME-type.
300
+ If not provided, the parser will attempt to determine the format based on the content of the stream.
301
+
302
+ - `size`: The total size of the audio stream in bytes (useful for streams with a known length).
303
+
304
+ - `path`: A string representing the file path or filename, which can also assist in determining the format.
305
+
306
+ - `options`: `IOptions` (optional)
307
+
308
+ An optional object containing additional parsing options.
309
+ These options allow you to customize the parsing process,
310
+ such as whether to calculate the duration or skip cover art extraction.
311
+
312
+ ##### Returns
313
+
314
+ - `Promise<IAudioMetadata>`:
315
+
316
+ A promise that resolves to an `IAudioMetadata` object containing detailed metadata about the audio stream.
317
+ This metadata includes information about the format, codec, duration, bitrate, and any embedded tags such as artist, album, or track information.
318
+
319
+ ##### Example
320
+ Here’s an example of how to use the `parseWebStream` function to extract metadata from an audio stream in a web application:
321
+
322
+ ```js
323
+ import { parseWebStream } from 'music-metadata';
324
+
325
+ (async () => {
326
+ try {
327
+ // Fetch the audio file
328
+ const response = await fetch('https://github.com/Borewit/test-audio/raw/refs/heads/master/Various%20Artists%20-%202008%20-%20netBloc%20Vol%2013%20-%20Color%20in%20a%20World%20of%20Monochrome%20%5BAAC-40%5D/1.02.%20Solid%20Ground.m4a');
329
+
330
+ // Extract the Content-Length header and convert it to a number
331
+ const contentLength = response.headers.get('Content-Length');
332
+ const size = contentLength ? parseInt(contentLength, 10) : undefined;
333
+
334
+ // Parse the metadata from the web stream
335
+ const metadata = await parseWebStream(response.body, {
336
+ mimeType: response.headers.get('Content-Type'),
337
+ size // Important to pass the content-length
338
+ });
339
+
340
+ console.log(metadata);
341
+ } catch (error) {
342
+ console.error('Error parsing metadata:', error.message);
343
+ }
344
+ })();
345
+ ```
346
+
347
+ The example uses the [`fetch` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to retrieve an audio file from a URL.
348
+ The `response.body` provides a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) that is then passed to `parseWebStream`.
349
+
350
+ #### `parseBlob` function
351
+
352
+ Parses metadata from an audio file represented as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
353
+ This function is suitable for use in environments that support the ReadableStreamBYOBReader, which is **available in Node.js 20** and above.
354
+
355
+ ##### Syntax
356
+ ```ts
357
+ parseBlob(blob: Blob, options?: IOptions = {}): Promise<IAudioMetadata>
358
+ ```
359
+
360
+ ##### Parameters
361
+
362
+ - `blob`: [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
363
+
364
+ The Blob object containing the audio data to be parsed.
365
+ This can be a file or any binary data. If the Blob is an instance of File, its name will be used as the file path in the metadata.
366
+
367
+ - `options`: [IOptions](#ioptions-interface) (optional)
368
+
369
+ An optional configuration object that specifies parsing options.
370
+
371
+ ##### Returns
372
+
373
+ - `Promise<IAudioMetadata>`:
374
+
375
+ A promise that resolves to the metadata of the audio file.
376
+
377
+ ##### Example
378
+
379
+ ```js
380
+ import { parseBlob } from 'music-metadata';
381
+
382
+ (async () => {
383
+ const fileInput = document.querySelector('input[type="file"]');
384
+ const file = fileInput.files[0];
385
+
386
+ try {
387
+ const metadata = await parseBlob(file);
388
+ console.log(metadata);
389
+ } catch (error) {
390
+ console.error('Error parsing metadata:', error.message);
391
+ }
392
+ })();
393
+ ```
394
+
395
+ #### `parseBuffer` function
396
+
397
+ Parses metadata from an audio file where the audio data is held in a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or [Buffer](https://nodejs.org/api/buffer.html).
398
+ This function is particularly useful when you already have audio data in memory.
399
+
400
+ ##### Syntax
401
+ ```ts
402
+ parseBuffer(buffer: Uint8Array, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>
403
+ ```
404
+
405
+ ##### Parameters
406
+ - `uint8Array`: [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
407
+
408
+ A [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) containing the audio data to be parsed.
409
+
410
+ - `fileInfo`: `IFileInfo` | `string` (optional)
411
+
412
+ An object containing file information such as mimeType and size.
413
+ Alternatively, you can pass a MIME-type string directly.
414
+ This helps the parser understand the format of the audio data.
415
+
416
+ - `options`: [IOptions](#ioptions-interface) (optional)
417
+
418
+ An optional configuration object that specifies parsing options.
419
+
420
+ ##### Returns
421
+ - `Promise<IAudioMetadata>`:
422
+
423
+ A promise that resolves to the metadata of the audio file.
424
+
425
+
426
+ ##### Example
427
+
428
+ ```js
429
+ import { parseBuffer } from 'music-metadata';
430
+ import fs from 'fs';
431
+
432
+ (async () => {
433
+ const buffer = fs.readFileSync('path/to/audio/file.mp3');
434
+
435
+ try {
436
+ const metadata = await parseBuffer(buffer, { mimeType: 'audio/mpeg' });
437
+ console.log(metadata);
438
+ } catch (error) {
439
+ console.error('Error parsing metadata:', error.message);
440
+ }
441
+ })();
442
+ ```
443
+
444
+ #### `parseFromTokenizer` function
445
+ Parses metadata from an audio source that implements the [strtok3](https://github.com/Borewit/strtok3) ITokenizer interface.
446
+ This is a low-level function that provides flexibility for advanced use cases,
447
+ such as parsing metadata from streaming audio or custom data sources.
448
+
449
+ This also enables special read modules like:
450
+ - [streaming-http-token-reader](https://github.com/Borewit/streaming-http-token-reader) for chunked HTTP(S) reading, using [HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests).
451
+
452
+ ##### Syntax
453
+ ```ts
454
+ parseFromTokenizer(tokenizer: ITokenizer, options?: IOptions): Promise<IAudioMetadata>
455
+ ```
456
+
457
+ ##### Parameters
458
+ - `tokenizer: ITokenizer`
459
+
460
+ An instance of an ITokenizer that provides access to the audio data.
461
+ The tokenizer abstracts the reading process, enabling support for various types of sources, including streams, buffers, or custom data readers.
462
+
463
+ - `options`: [IOptions](#ioptions-interface) (optional)
464
+
465
+ An optional configuration object that specifies parsing options.
466
+
467
+ ##### Returns
468
+ - `Promise<IAudioMetadata>`:
469
+
470
+ A promise that resolves to the metadata of the audio source, including information like the title, artist, album, and more.
471
+
472
+
473
+ ##### Example
474
+ ````js
475
+ import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
476
+ import { S3Client } from '@aws-sdk/client-s3';
477
+ import { makeTokenizer } from '@tokenizer/s3';
478
+ import { parseFromTokenizer as mmParseFromTokenizer } from 'music-metadata';
479
+
480
+ // Configure the S3 client
481
+ const s3 = new S3Client({
482
+ region: 'eu-west-2',
483
+ credentials: fromNodeProviderChain(),
484
+ });
485
+
486
+ // Helper function to create a tokenizer for S3 objects
487
+ async function makeS3TestDataTokenizer(key, options) {
488
+ return await makeTokenizer(s3, {
489
+ Bucket: 'music-metadata',
490
+ Key: key,
491
+ }, options);
492
+ }
493
+
494
+ // Function to read and log metadata from an S3 object
495
+ async function readMetadata() {
496
+ try {
497
+ // Create a tokenizer for the specified S3 object
498
+ const tokenizer = await makeS3TestDataTokenizer('path/to/audio/file.mp3', { disableChunked: false });
499
+
500
+ // Parse the metadata from the tokenizer
501
+ const metadata = await mmParseFromTokenizer(tokenizer);
502
+
503
+ // Log the retrieved metadata
504
+ console.log(metadata);
505
+ } catch (error) {
506
+ console.error('Error parsing metadata:', error.message);
507
+ }
508
+ }
509
+
510
+ // Execute the metadata reading function
511
+ readMetadata();
512
+ ````
513
+ ##### Additional Resources
514
+ - [strtok3](https://github.com/Borewit/strtok3) - Learn more about the `ITokenizer` interface and how to implement it for various use cases.
515
+ - [AWS SDK for JavaScript](https://aws.amazon.com/sdk-for-javascript/) - Documentation on using the AWS SDK to interact with S3 and other AWS services.
516
+ - [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3) - Example of `ITokenizer` implementation.
517
+
518
+ ### Handling Parse Errors
519
+
520
+ `music-metadata` provides a robust and extensible error handling system with custom error classes that inherit from the standard JavaScript `Error`.
521
+ All possible parsing errors are part of a union type `UnionOfParseErrors`, ensuring that every error scenario is accounted for in your code.
522
+
523
+ #### Union of Parse Errors
524
+
525
+ All parsing errors extend from the base class `ParseError` and are included in the `UnionOfParseErrors` type:
526
+ ```ts
527
+ export type UnionOfParseErrors =
528
+ | CouldNotDetermineFileTypeError
529
+ | UnsupportedFileTypeError
530
+ | UnexpectedFileContentError
531
+ | FieldDecodingError
532
+ | InternalParserError;
533
+ ```
534
+
535
+ #### Error Types
536
+
537
+ - `CouldNotDetermineFileTypeError`: Raised when the file type cannot be determined.
538
+ - `UnsupportedFileTypeError`: Raised when an unsupported file type is encountered.
539
+ - `UnexpectedFileContentError`: Raised when the file content does not match the expected format.
540
+ - `FieldDecodingError`: Raised when a specific field in the file cannot be decoded.
541
+ - `InternalParserError`: Raised for internal parser errors.
542
+
543
+ ### Other functions
544
+
545
+ #### `orderTags` function
546
+
547
+ Utility to Converts the native tags to a dictionary index on the tag identifier
548
+
549
+ ```ts
550
+ orderTags(nativeTags: ITag[]): [tagId: string]: any[]
551
+ ```
552
+
553
+ ```js
554
+ import { parseFile, orderTags } from 'music-metadata';
555
+ import { inspect } from 'util';
556
+
557
+ (async () => {
558
+ try {
559
+ const metadata = await parseFile('../test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
560
+ const orderedTags = orderTags(metadata.native['ID3v2.3']);
561
+ console.log(inspect(orderedTags, { showHidden: false, depth: null }));
562
+ } catch (error) {
563
+ console.error(error.message);
564
+ }
565
+ })();
566
+ ```
567
+
568
+ #### `ratingToStars` function
569
+
570
+ 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.
571
+
572
+ ```ts
573
+ ratingToStars(rating: number): number
574
+ ```
575
+ #### `selectCover` function
576
+
577
+ Select cover image based on image type field, otherwise the first picture in file.
578
+
579
+ ```ts
580
+ export function selectCover(pictures?: IPicture[]): IPicture | null
581
+ ```
582
+
583
+ ```js
584
+ import { parseFile, selectCover } from 'music-metadata';
585
+
586
+ (async () => {
587
+ const {common} = await parseFile(filePath);
588
+ const cover = selectCover(common.picture); // pick the cover image
589
+ }
590
+ )();
591
+ ```
592
+
593
+ #### `getSupportedMimeTypes` function
594
+
595
+ Returns a list of supported MIME-types. This may include some MIME-types which are not formally recognized.
596
+
597
+ ### `IOptions` Interface
598
+ - `duration`: `boolean` (default: `false`)
599
+
600
+ When `true`, the parser will read the entire media file _if necessary_ to determine the duration.
601
+ This is only applicable in cases where duration cannot be reliably inferred without full file analysis.
602
+ Note that enabling this option **does not guarantee** that duration will be available,
603
+ only that the parser will attempt to calculate it when possible, even if it requires reading the full file.
604
+
605
+ - `mkvUseIndex`: `boolean` (default: `false`)
606
+
607
+ When `true`, the parser uses the SeekHead index in Matroska (MKV) files to skip segment and cluster elements.
608
+ This experimental feature can improve performance, but:
609
+ - Metadata not listed in the SeekHead may be skipped.
610
+ - If the SeekHead is missing, this option has no effect.
611
+
612
+ - `observer`: `(update: MetadataEvent) => void;`:
613
+
614
+ Callback function triggered when common tags or format properties are updated during parsing.
615
+ Allows real-time monitoring of metadata as it becomes available.
616
+
617
+ - `skipCovers`: `boolean` (default: `false`)
618
+
619
+ When `true`, embedded cover art (images) will not be extracted.
620
+ Useful for reducing memory and processing when cover images are unnecessary.
621
+
622
+ - `skipPostHeaders`: `boolean` (default: `false`)
623
+ When `true`, tag headers located at the end of the file will not be read.
624
+ This is particularly beneficial for streaming input, as it avoids the need to read the entire stream.
625
+
626
+ > [!NOTE]
627
+ > - The `duration` option is typically included in most cases, but setting it to true ensures that the entire file is parsed if necessary to get an accurate duration.
628
+ > - Using `mkvUseIndex` can improve performance in Matroska files, but be aware of potential side effects, such as missing metadata due to skipped elements.
629
+
630
+
631
+ ### `IAudioMetadata` interface
632
+
633
+ If the returned promise resolves, the metadata (TypeScript `IAudioMetadata` interface) contains:
634
+ - [`metadata.format`](#metadataformat) Audio format information
635
+ - [`metadata.common`](#metadatacommon) Is a generic (abstract) way of reading metadata information.
636
+ - [`metadata.trackInfo`](#metadatatrackInfo) Is a generic (abstract) way of reading metadata information.
637
+ - `metadata.native` List of native (original) tags found in the parsed audio file.
638
+
639
+ #### `metadata.format`
640
+
641
+ The questionmark `?` indicates the property is optional.
642
+
643
+ Audio format information. Defined in the TypeScript `IFormat` interface:
644
+ - `format.container?: string` Audio encoding format. e.g.: 'flac'
645
+ - `format.codec?` Name of the codec (algorithm used for the audio compression)
646
+ - `format.codecProfile?: string` Codec profile / settings
647
+ - `format.tagTypes?: TagType[]` List of tagging formats found in parsed audio file
648
+ - `format.duration?: number` Duration in seconds
649
+ - `format.bitrate?: number` Number bits per second of encoded audio file
650
+ - `format.sampleRate?: number` Sampling rate in Samples per second (S/s)
651
+ - `format.bitsPerSample?: number` Audio bit depth
652
+ - `format.lossless?: boolean` True if lossless, false for lossy encoding
653
+ - `format.numberOfChannels?: number` Number of audio channels
654
+ - `format.creationTime?: Date` Track creation time
655
+ - `format.modificationTime?: Date` Track modification / tag update time
656
+ - `format.trackGain?: number` Track gain in dB
657
+ - `format.albumGain?: number` Album gain in dB
658
+
659
+ #### `metadata.trackInfo`
660
+
661
+ To support advanced containers like [Matroska](https://wikipedia.org/wiki/Matroska) or [MPEG-4](https://en.wikipedia.org/wiki/MPEG-4), which may contain multiple audio and video tracks, the **experimental**- `metadata.trackInfo` has been added,
662
+
663
+ `metadata.trackInfo` is either `undefined` or has an **array** of [trackInfo](#trackinfo)
664
+
665
+ ##### trackInfo
666
+
667
+ Audio format information. Defined in the TypeScript `IFormat` interface:
668
+ - `trackInfo.type?: TrackType` Track type
669
+ - `trackInfo.codecName?: string` Codec name
670
+ - `trackInfo.codecSettings?: string` Codec settings
671
+ - `trackInfo.flagEnabled?: boolean` Set if the track is usable, default: `true`
672
+ - `trackInfo.flagDefault?: boolean` Set if that track (audio, video or subs) SHOULD be active if no language found matches the user preference.
673
+ - `trackInfo.flagLacing?: boolean` Set if the track **may** contain blocks using lacing
674
+ - `trackInfo.name?: string` A human-readable track name.
675
+ - `trackInfo.language?: string` Specifies the language of the track
676
+ - `trackInfo.audio?: IAudioTrack`, see [`trackInfo.audioTrack`](#trackinfoaudiotrack)
677
+ - `trackInfo.video?: IVideoTrack`, see [`trackInfo.videoTrack`](#trackinfovideotrack)
678
+
679
+ ##### `trackInfo.audioTrack`
680
+
681
+ - `audioTrack.samplingFrequency?: number`
682
+ - `audioTrack.outputSamplingFrequency?: number`
683
+ - `audioTrack.channels?: number`
684
+ - `audioTrack.channelPositions?: Buffer`
685
+ - `audioTrack.bitDepth?: number`
686
+
687
+ ##### `trackInfo.videoTrack`
688
+
689
+ - `videoTrack.flagInterlaced?: boolean`
690
+ - `videoTrack.stereoMode?: number`
691
+ - `videoTrack.pixelWidth?: number`
692
+ - `videoTrack.pixelHeight?: number`
693
+ - `videoTrack.displayWidth?: number`
694
+ - `videoTrack.displayHeight?: number`
695
+ - `videoTrack.displayUnit?: number`
696
+ - `videoTrack.aspectRatioType?: number`
697
+ - `videoTrack.colourSpace?: Buffer`
698
+ - `videoTrack.gammaValue?: number`
699
+
700
+ #### `metadata.common`
701
+
702
+ [Common tag documentation](doc/common_metadata.md) is automatically generated.
703
+
704
+ ## Examples
705
+
706
+ 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.
707
+ ```js
708
+ import { parseStream } from 'music-metadata';
709
+ import { inspect } from 'util';
710
+
711
+ (async () => {
712
+ const metadata = await parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838}, {duration: true});
713
+ console.log(inspect(metadata, {showHidden: false, depth: null}));
714
+ someReadStream.close();
715
+ }
716
+ )();
717
+ ```
718
+
719
+ ### Access cover art
720
+
721
+ Via `metadata.common.picture` you can access an array of cover art if present.
722
+ Each picture has this interface:
723
+
724
+ ```ts
725
+ /**
726
+ * Attached picture, typically used for cover art
727
+ */
728
+ export interface IPicture {
729
+ /**
730
+ * Image mime type
731
+ */
732
+ format: string;
733
+ /**
734
+ * Image data
735
+ */
736
+ data: Buffer;
737
+ /**
738
+ * Optional description
739
+ */
740
+ description?: string;
741
+ /**
742
+ * Picture type
743
+ */
744
+ type?: string;
745
+ }
746
+ ```
747
+
748
+ To assign `img` HTML-object you can do something like:
749
+ ```js
750
+ import {uint8ArrayToBase64} from 'uint8array-extras';
751
+
752
+ img.src = `data:${picture.format};base64,${uint8ArrayToBase64(picture.data)}`;
753
+ ```
754
+
755
+ ## Dependencies
756
+
757
+ Dependency diagram:
758
+ ```mermaid
759
+ graph TD;
760
+ MMN("music-metadata (Node.js entry point)")-->MMP
761
+ MMN-->FTN
762
+ MMP("music-metadata (primary entry point)")-->S(strtok3)
763
+ MMP-->TY(token-types)
764
+ MMP-->FTP
765
+ MMP-->UAE
766
+ FTN("file-type (Node.js entry point)")-->FTP
767
+ FTP("file-type (primary entry point)")-->S
768
+ S(strtok3)-->TO("@tokenizer/token")
769
+ TY(token-types)-->TO
770
+ TY-->IE("ieee754")
771
+ FTP-->TY
772
+ NS("node:stream")
773
+ FTN-->NS
774
+ FTP-->UAE(uint8array-extras)
775
+ style NS fill:#F88,stroke:#A44
776
+ style IE fill:#CCC,stroke:#888
777
+ style FTN fill:#FAA,stroke:#A44
778
+ style MMN fill:#FAA,stroke:#A44
779
+ ```
780
+
781
+ Dependency list:
782
+ - [tokenizer-token](https://github.com/Borewit/tokenizer-token)
783
+ - [strtok3](https://github.com/Borewit/strtok3)
784
+ - [token-types](https://github.com/Borewit/token-types)
785
+ - [file-type](https://github.com/sindresorhus/file-type)
786
+ - [@tokenizer-token](https://github.com/Borewit/tokenizer-token)
787
+
788
+ ## CommonJS backward compatibility
789
+
790
+ Using Node.js ≥ 22, which is support loading ESM module via require
791
+ ```js
792
+ const mm = require('music-metadata');
793
+ ```
794
+
795
+ For older Node.js version < 22, you need to dynamically import **music-metadata**:
796
+ ```js
797
+ (async () => {
798
+ // Dynamically loads the ESM module in a CommonJS project
799
+ const mm = await import('music-metadata');
800
+ })();
801
+ ```
802
+
803
+ For CommonJS TypeScript projects, I recommend to avoid using `commonjs` for the TypeScript compiler `module` option,
804
+ and either use `node16` or `nodenext`, which enable utilizing [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import).
805
+
806
+ If you do want to use the classic `commonjs` option, this is how you can get the _dynamic import_ to work.
807
+
808
+ ```js
809
+ import {loadEsm} from 'load-esm';
810
+
811
+ (async () => {
812
+ // Dynamically loads the ESM module in a CommonJS project
813
+ const mm = await loadEsm<typeof import('music-metadata')>('music-metadata');
814
+ })();
815
+ ```
816
+
817
+ When you use Node.js version ≥ 22, which supports loading ESM modules via require, this compensates for that issue.
818
+
819
+ ## Frequently Asked Questions
820
+
821
+ ### How can I traverse (a long) list of files?
822
+
823
+ What is important that file parsing should be done in a sequential manner.
824
+ In a plain loop, due to the asynchronous character (like most JavaScript functions), it would cause all the files to run in parallel which is will cause your application to hang in no time.
825
+ There are multiple ways of achieving this:
826
+
827
+ 1. Using recursion
828
+
829
+ ```js
830
+ import { parseFile } from 'music-metadata';
831
+
832
+ function parseFiles(audioFiles) {
833
+
834
+ const audioFile = audioFiles.shift();
835
+
836
+ if (audioFile) {
837
+ return parseFile(audioFile).then(metadata => {
838
+ // Do great things with the metadata
839
+ return parseFiles(audioFiles); // process rest of the files AFTER we are finished
840
+ })
841
+ }
842
+ }
843
+
844
+ ```
845
+
846
+ 1. Use async/await
847
+
848
+ Use [async/await](https://javascript.info/async-await)
849
+
850
+ ```js
851
+ import { parseFile } from 'music-metadata';
852
+
853
+ // it is required to declare the function 'async' to allow the use of await
854
+ async function parseFiles(audioFiles) {
855
+
856
+ for (const audioFile of audioFiles) {
857
+
858
+ // await will ensure the metadata parsing is completed before we move on to the next file
859
+ const metadata = await parseFile(audioFile);
860
+ // Do great things with the metadata
861
+ }
862
+ }
863
+ ```
864
+
865
+ ### Using music-metadata with TypeScript and module-resolution set to bundler.
866
+
867
+ If the TypeScript compiler option [`moduleResolution`](https://www.typescriptlang.org/tsconfig/#moduleResolution)
868
+ is set to `"bundler"`, it does not set the ECMAScript `"node"` condition, causing the Node specific function fail to import.
869
+
870
+ This is the case using Next.js. See [issue #2370](https://github.com/Borewit/music-metadata/issues/2370) how to resolve that.
871
+
872
+ ## Licence
873
+
874
+ This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.