music-metadata 9.0.3 → 10.0.1
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/LICENSE.txt +9 -9
- package/README.md +520 -519
- package/lib/apev2/APEv2Parser.js +8 -2
- package/lib/apev2/APEv2Token.js +1 -1
- package/lib/common/GenericTagMapper.d.ts +1 -1
- package/lib/common/MetadataCollector.js +8 -0
- package/lib/core.d.ts +1 -0
- package/lib/core.js +1 -0
- package/lib/id3v2/FrameParser.d.ts +5 -1
- package/lib/id3v2/FrameParser.js +46 -20
- package/lib/id3v2/ID3v22TagMapper.js +0 -6
- package/lib/id3v2/ID3v24TagMapper.js +0 -7
- package/lib/id3v2/ID3v2Parser.d.ts +1 -0
- package/lib/id3v2/ID3v2Parser.js +11 -16
- package/lib/id3v2/ID3v2Token.d.ts +39 -0
- package/lib/id3v2/ID3v2Token.js +47 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -0
- package/lib/type.d.ts +26 -3
- package/lib/type.js +1 -0
- package/package.json +150 -148
package/README.md
CHANGED
|
@@ -1,519 +1,520 @@
|
|
|
1
|
-
[](https://github.com/Borewit/music-metadata/actions?query=branch%3Amaster)
|
|
2
|
-
[](https://ci.appveyor.com/project/Borewit/music-metadata/branch/master)
|
|
3
|
-
[](https://npmjs.org/package/music-metadata)
|
|
4
|
-
[](https://npmcharts.com/compare/music-metadata,jsmediatags,musicmetadata,node-id3,mp3-parser,id3-parser,wav-file-info?start=600)
|
|
5
|
-
[](https://coveralls.io/github/Borewit/music-metadata?branch=master)
|
|
6
|
-
[](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)
|
|
7
|
-
[](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml)
|
|
8
|
-
[](https://deepscan.io/dashboard#view=project&tid=5165&pid=6938&bid=61821)
|
|
9
|
-
[](https://snyk.io/test/github/Borewit/music-metadata?targetFile=package.json)
|
|
10
|
-
[](https://discord.gg/KyBr6sb)
|
|
11
|
-
|
|
12
|
-
# music-metadata
|
|
13
|
-
|
|
14
|
-
Stream and file based music metadata parser for [node.js](https://nodejs.org/) and browser projects.
|
|
15
|
-
Supports any common audio and tagging format.
|
|
16
|
-
|
|
17
|
-
## Compatibility
|
|
18
|
-
|
|
19
|
-
Module: version 8 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
|
|
20
|
-
JavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).
|
|
21
|
-
Requires Node.js ≥ 16 engine.
|
|
22
|
-
Primarily designed for [Node.js](https://nodejs.org/), but has also been designed for browser compatibility.
|
|
23
|
-
|
|
24
|
-
## Features
|
|
25
|
-
|
|
26
|
-
### Support for audio file types
|
|
27
|
-
|
|
28
|
-
| Audio format | Description | Wiki | |
|
|
29
|
-
| ------------- |---------------------------------| -------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------:|
|
|
30
|
-
| AIFF / AIFF-C | Audio Interchange File Format | [:link:](https://wikipedia.org/wiki/Audio_Interchange_File_Format) | <img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Apple_Computer_Logo_rainbow.svg" width="40" alt="Apple rainbow logo"> |
|
|
31
|
-
| AAC | ADTS / Advanced Audio Coding | [:link:](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) | <img src="https://svgshare.com/i/UT8.svg" width="40" alt="AAC logo"> |
|
|
32
|
-
| APE | Monkey's Audio | [:link:](https://wikipedia.org/wiki/Monkey's_Audio) | <img src="https://foreverhits.files.wordpress.com/2015/05/ape_audio.jpg" width="40" alt="Monkey's Audio logo"> |
|
|
33
|
-
| ASF | Advanced Systems Format | [:link:](https://wikipedia.org/wiki/Advanced_Systems_Format) | |
|
|
34
|
-
| BWF | Broadcast Wave Format | [:link:](https://en.wikipedia.org/wiki/Broadcast_Wave_Format) | |
|
|
35
|
-
| DSDIFF | Philips DSDIFF | [:link:](https://wikipedia.org/wiki/Direct_Stream_Digital) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/DSDlogo.svg" width="80" alt="DSD logo"> |
|
|
36
|
-
| DSF | Sony's DSD Stream File | [:link:](https://wikipedia.org/wiki/Direct_Stream_Digital) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/DSDlogo.svg" width="80" alt="DSD logo"> |
|
|
37
|
-
| FLAC | Free Lossless Audio Codec | [:link:](https://wikipedia.org/wiki/FLAC) | <img src="https://upload.wikimedia.org/wikipedia/commons/a/a2/FLAC_logo_vector.svg" width="80" alt="FLAC logo"> |
|
|
38
|
-
| MP2 | MPEG-1 Audio Layer II | [:link:](https://wikipedia.org/wiki/MPEG-1_Audio_Layer_II) | |
|
|
39
|
-
| Matroska | Matroska (EBML), mka, mkv | [:link:](https://wikipedia.org/wiki/Matroska) | <img src="https://upload.wikimedia.org/wikipedia/commons/1/1a/Matroska_2010.svg" width="80" alt="Matroska logo"> |
|
|
40
|
-
| MP3 | MPEG-1 / MPEG-2 Audio Layer III | [:link:](https://wikipedia.org/wiki/MP3) | <img src="https://upload.wikimedia.org/wikipedia/commons/e/ea/Mp3.svg" width="80" alt="MP3 logo"> |
|
|
41
|
-
| MPC | Musepack SV7 | [:link:](https://wikipedia.org/wiki/Musepack) | <img src="https://www.musepack.net/pictures/musepack_logo.png" width="80" alt="musepack logo"> |
|
|
42
|
-
| MPEG 4 | mp4, m4a, m4v | [:link:](https://wikipedia.org/wiki/MPEG-4) | <img src="https://svgshare.com/i/UU3.svg" width="80" alt="mpeg 4 logo"> |
|
|
43
|
-
| Ogg | Open container format | [:link:](https://en.wikipedia.org/wiki/Ogg) | <img src="https://upload.wikimedia.org/wikipedia/commons/a/a1/Ogg_Logo.svg" width="80" alt="Ogg logo"> |
|
|
44
|
-
| Opus | | [:link:](https://wikipedia.org/wiki/Opus_(audio_format)) | <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Opus_logo2.svg" width="80" alt="Opus logo"> |
|
|
45
|
-
| Speex | | [:link:](https://wikipedia.org/wiki/Speex) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/b5/Speex_logo_2006.svg" width="80" alt="Speex logo"> |
|
|
46
|
-
| Theora | | [:link:](https://en.wikipedia.org/wiki/Theora) | <img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Theora_logo_2007.svg" width="70" alt="Theora logo"> |
|
|
47
|
-
| Vorbis | Vorbis audio compression | [:link:](https://wikipedia.org/wiki/Ogg_Vorbis) | <img src="https://upload.wikimedia.org/wikipedia/commons/8/8d/Xiph.Org_logo_square.svg" width="70" alt="Vorbis logo"> |
|
|
48
|
-
| WAV | RIFF WAVE | [:link:](https://wikipedia.org/wiki/WAV) | |
|
|
49
|
-
| WebM | webm | [:link:](https://wikipedia.org/wiki/WebM) | <img src="https://upload.wikimedia.org/wikipedia/commons/3/34/WebM_logo.svg" width="80" alt="Matroska logo"> |
|
|
50
|
-
| WV | WavPack | [:link:](https://wikipedia.org/wiki/WavPack) | <img src="http://www.wavpack.com/wavpacklogo.svg" width="60" alt="WavPack logo"> |
|
|
51
|
-
| WMA | Windows Media Audio | [:link:](https://wikipedia.org/wiki/Windows_Media_Audio) | <img src="https://upload.wikimedia.org/wikipedia/commons/7/76/Windows_Media_Player_simplified_logo.svg" width="40" alt="Windows Media logo"> |
|
|
52
|
-
|
|
53
|
-
### Supported tag headers
|
|
54
|
-
|
|
55
|
-
Following tag header formats are supported:
|
|
56
|
-
- [APE](https://wikipedia.org/wiki/APE_tag)
|
|
57
|
-
- [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format)
|
|
58
|
-
- EXIF 2.3
|
|
59
|
-
- [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)
|
|
60
|
-
- [iTunes](https://github.com/sergiomb2/libmp4v2/wiki/iTunesMetadata)
|
|
61
|
-
- [RIFF](https://wikipedia.org/wiki/Resource_Interchange_File_Format)/INFO
|
|
62
|
-
- [Vorbis comment](https://wikipedia.org/wiki/Vorbis_comment)
|
|
63
|
-
- [AIFF](https://wikipedia.org/wiki/Audio_Interchange_File_Format)
|
|
64
|
-
|
|
65
|
-
It allows many tags to be accessed in audio format, and tag format independent way.
|
|
66
|
-
|
|
67
|
-
Support for [MusicBrainz](https://musicbrainz.org/) tags as written by [Picard](https://picard.musicbrainz.org/).
|
|
68
|
-
[ReplayGain](https://wiki.hydrogenaud.io/index.php?title=ReplayGain) tags are supported.
|
|
69
|
-
|
|
70
|
-
### Audio format & encoding details
|
|
71
|
-
|
|
72
|
-
Support for encoding / format details:
|
|
73
|
-
- [Bit rate](https://wikipedia.org/wiki/Bit_rate)
|
|
74
|
-
- [Audio bit depth](https://wikipedia.org/wiki/Audio_bit_depth)
|
|
75
|
-
- Duration
|
|
76
|
-
- Encoding profile (e.g. [CBR](https://en.wikipedia.org/wiki/Constant_bitrate), V0, V2)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
## Online demo's
|
|
80
|
-
- [<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/)
|
|
81
|
-
- [<img src="https://cdn.sanity.io/images/3do82whm/next/ba8c847f13a5fa39d88f8bc9b7846b7886531b18-2500x2500.svg" width="40"> Webamp](https://webamp.org/)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
### Sponsor
|
|
85
|
-
[Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
|
|
86
|
-
|
|
87
|
-
<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>
|
|
88
|
-
|
|
89
|
-
## Dependencies
|
|
90
|
-
|
|
91
|
-
Dependency diagram:
|
|
92
|
-
```mermaid
|
|
93
|
-
graph TD;
|
|
94
|
-
MMN("music-metadata (Node.js entry point)")-->MMP
|
|
95
|
-
MMN-->FTN
|
|
96
|
-
MMP("music-metadata (primary entry point)")-->S(strtok3)
|
|
97
|
-
MMP-->TY(token-types)
|
|
98
|
-
MMP-->FTP
|
|
99
|
-
MMP-->UAE
|
|
100
|
-
FTN("file-type (Node.js entry point)")-->FTP
|
|
101
|
-
FTP("file-type (primary entry point)")-->S
|
|
102
|
-
S(strtok3)-->P(peek-readable)
|
|
103
|
-
S(strtok3)-->TO("@tokenizer/token")
|
|
104
|
-
TY(token-types)-->TO
|
|
105
|
-
TY-->IE("ieee754")
|
|
106
|
-
FTP-->TY
|
|
107
|
-
NS("node:stream")
|
|
108
|
-
FTN-->NS
|
|
109
|
-
FTP-->UAE(uint8array-extras)
|
|
110
|
-
style NS fill:#F88,stroke:#A44
|
|
111
|
-
style IE fill:#CCC,stroke:#888
|
|
112
|
-
style FTN fill:#FAA,stroke:#A44
|
|
113
|
-
style MMN fill:#FAA,stroke:#A44
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
Dependency list:
|
|
117
|
-
- [tokenizer-token](https://github.com/Borewit/tokenizer-token)
|
|
118
|
-
- [strtok3](https://github.com/Borewit/strtok3)
|
|
119
|
-
- [token-types](https://github.com/Borewit/token-types)
|
|
120
|
-
- [file-type](https://github.com/sindresorhus/file-type)
|
|
121
|
-
- [@tokenizer-token](https://github.com/Borewit/tokenizer-token)
|
|
122
|
-
- [peek-readable](https://github.com/Borewit/peek-readable)
|
|
123
|
-
- [readable-web-to-node-stream](https://github.com/Borewit/readable-web-to-node-stream)
|
|
124
|
-
|
|
125
|
-
## Usage
|
|
126
|
-
|
|
127
|
-
### Installation
|
|
128
|
-
Install using [npm](http://npmjs.org/):
|
|
129
|
-
|
|
130
|
-
```bash
|
|
131
|
-
npm install music-metadata
|
|
132
|
-
```
|
|
133
|
-
or using [yarn](https://yarnpkg.com/):
|
|
134
|
-
```bash
|
|
135
|
-
yarn add music-metadata
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Import music-metadata
|
|
139
|
-
|
|
140
|
-
Import music-metadata:
|
|
141
|
-
```JavaScript
|
|
142
|
-
import { parseFile } from 'music-metadata';
|
|
143
|
-
```
|
|
144
|
-
Import the methods you need, like `parseFile` in this example.
|
|
145
|
-
|
|
146
|
-
### Module Functions
|
|
147
|
-
|
|
148
|
-
There are multiple ways to parse (read) audio tracks:
|
|
149
|
-
1. In Node.js, audio (music) files can be parsed using direct file access using the [parseFile function](#parsefile-function)
|
|
150
|
-
1. By parsing from a Web stream using the [parseStream function](#parsewebstream-function).
|
|
151
|
-
1. By parsing a (Web API) [Blob](https://developer.mozilla.org/docs/Web/API/Blob) or a [File](https://developer.mozilla.org/docs/Web/API/File) using the [parseBlob function](#parseblob-function).
|
|
152
|
-
1. From a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) using the [parseBuffer function](#parsebuffer-function).
|
|
153
|
-
1. Via your own, or a third-party [strtok3](https://github.com/Borewit/strtok3) ITokenizer using the [parseFromTokenizer function](#parsefromtokenizer-function).
|
|
154
|
-
|
|
155
|
-
Direct file access tends to be a little faster, because it can 'jump' to various parts in the file without being obliged to read intermediate data.
|
|
156
|
-
|
|
157
|
-
#### parseFile function
|
|
158
|
-
|
|
159
|
-
This method can only be used if the JavaScript engine is Node.js.
|
|
160
|
-
To read from a [File](https://developer.mozilla.org/docs/Web/API/File),
|
|
161
|
-
please see [fileTypeFromBlob(blob)](#parseblob-function).
|
|
162
|
-
|
|
163
|
-
Parses the specified file (`filePath`) and returns a promise with the metadata result (`IAudioMetadata`).
|
|
164
|
-
|
|
165
|
-
```ts
|
|
166
|
-
parseFile(filePath: string, opts: IOptions = {}): Promise<IAudioMetadata>`
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Example:
|
|
170
|
-
```js
|
|
171
|
-
import { parseFile } from 'music-metadata';
|
|
172
|
-
import { inspect } from 'util';
|
|
173
|
-
|
|
174
|
-
(async () => {
|
|
175
|
-
try {
|
|
176
|
-
const metadata = await parseFile('../music-metadata/test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
|
|
177
|
-
console.log(inspect(metadata, { showHidden: false, depth: null }));
|
|
178
|
-
} catch (error) {
|
|
179
|
-
console.error(error.message);
|
|
180
|
-
}
|
|
181
|
-
})();
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
#### parseStream function
|
|
185
|
-
|
|
186
|
-
_Only available using a Node.js JavaScript engines._
|
|
187
|
-
|
|
188
|
-
Parses the provided audio stream for metadata.
|
|
189
|
-
The stream should be of type [Node.js Readable](https://nodejs.org/api/stream.html#class-streamreadable).
|
|
190
|
-
It is recommended to provide the corresponding [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types).
|
|
191
|
-
An extension (e.g.: `.mp3`), filename or path will also work.
|
|
192
|
-
If the MIME-type or filename (via `fileInfo.path`) is not provided, or not understood, music-metadata will try to derive the type from the content.
|
|
193
|
-
|
|
194
|
-
```ts
|
|
195
|
-
parseStream(stream: Stream.Readable, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>`
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
Example:
|
|
199
|
-
```js
|
|
200
|
-
import { parseStream } from 'music-metadata';
|
|
201
|
-
|
|
202
|
-
(async () => {
|
|
203
|
-
try {
|
|
204
|
-
const metadata = await parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838});
|
|
205
|
-
console.log(metadata);
|
|
206
|
-
} catch (error) {
|
|
207
|
-
console.error(error.message);
|
|
208
|
-
}
|
|
209
|
-
})();
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
#### parseWebStream function
|
|
213
|
-
|
|
214
|
-
Parses the provided audio web stream for metadata.
|
|
215
|
-
The stream should be of type [ReadableStream<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream).
|
|
216
|
-
It is recommended to provide the corresponding [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types).
|
|
217
|
-
An extension (e.g.: `.mp3`), filename or path will also work.
|
|
218
|
-
If the MIME-type or filename (via `fileInfo.path`) is not provided, or not understood, music-metadata will try to derive the type from the content.
|
|
219
|
-
|
|
220
|
-
```ts
|
|
221
|
-
parseWebStream(stream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>`
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
#### parseBlob function
|
|
225
|
-
|
|
226
|
-
_This method can only be used if with a Node.js JavaScript engine._
|
|
227
|
-
|
|
228
|
-
Parse an audio file from a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or [File](https://developer.mozilla.org/en-US/docs/Web/API/File).
|
|
229
|
-
|
|
230
|
-
```js
|
|
231
|
-
const musicMetadata = require('music-metadata');
|
|
232
|
-
|
|
233
|
-
let blob;
|
|
234
|
-
|
|
235
|
-
musicMetadata.parseBlob(blob).then(metadata => {
|
|
236
|
-
// metadata has all the metadata found in the blob or file
|
|
237
|
-
});
|
|
238
|
-
```
|
|
239
|
-
Or with async/await if you prefer:
|
|
240
|
-
```js
|
|
241
|
-
(async () => {
|
|
242
|
-
let blob; // File or Blob
|
|
243
|
-
|
|
244
|
-
const metadata = await musicMetadata.parseBlob(blob);
|
|
245
|
-
// metadata has all the metadata found in the blob or file
|
|
246
|
-
});
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
#### parseBuffer function
|
|
250
|
-
|
|
251
|
-
Parse metadata from an audio file, where the audio file is held in a [Buffer](https://nodejs.org/api/buffer.html).
|
|
252
|
-
|
|
253
|
-
```ts
|
|
254
|
-
parseBuffer(buffer:
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
Example:
|
|
258
|
-
```js
|
|
259
|
-
import { parseBuffer } from 'music-metadata';
|
|
260
|
-
|
|
261
|
-
(async () => {
|
|
262
|
-
try {
|
|
263
|
-
const metadata = await parseBuffer(someBuffer, 'audio/mpeg');
|
|
264
|
-
console.log(metadata);
|
|
265
|
-
} catch (error) {
|
|
266
|
-
console.error(error.message);
|
|
267
|
-
}
|
|
268
|
-
})();
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
#### parseFromTokenizer function
|
|
272
|
-
This is a low level function, reading from a [strtok3](https://github.com/Borewit/strtok3) ITokenizer interface.
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
import {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
- `
|
|
328
|
-
- `
|
|
329
|
-
- `
|
|
330
|
-
- `
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
- [`metadata.
|
|
340
|
-
- [`metadata.
|
|
341
|
-
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
- `format.
|
|
350
|
-
- `format.
|
|
351
|
-
- `format.
|
|
352
|
-
- `format.
|
|
353
|
-
- `format.
|
|
354
|
-
- `format.
|
|
355
|
-
- `format.
|
|
356
|
-
- `format.
|
|
357
|
-
- `format.
|
|
358
|
-
- `format.
|
|
359
|
-
- `format.
|
|
360
|
-
- `format.
|
|
361
|
-
- `format.
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
- `trackInfo.
|
|
374
|
-
- `trackInfo.
|
|
375
|
-
- `trackInfo.
|
|
376
|
-
- `trackInfo.
|
|
377
|
-
- `trackInfo.
|
|
378
|
-
- `trackInfo.
|
|
379
|
-
- `trackInfo.
|
|
380
|
-
- `trackInfo.
|
|
381
|
-
- `trackInfo.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
- `audioTrack.
|
|
387
|
-
- `audioTrack.
|
|
388
|
-
- `audioTrack.
|
|
389
|
-
- `audioTrack.
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
- `videoTrack.
|
|
395
|
-
- `videoTrack.
|
|
396
|
-
- `videoTrack.
|
|
397
|
-
- `videoTrack.
|
|
398
|
-
- `videoTrack.
|
|
399
|
-
- `videoTrack.
|
|
400
|
-
- `videoTrack.
|
|
401
|
-
- `videoTrack.
|
|
402
|
-
- `videoTrack.
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
import {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
1
|
+
[](https://github.com/Borewit/music-metadata/actions?query=branch%3Amaster)
|
|
2
|
+
[](https://ci.appveyor.com/project/Borewit/music-metadata/branch/master)
|
|
3
|
+
[](https://npmjs.org/package/music-metadata)
|
|
4
|
+
[](https://npmcharts.com/compare/music-metadata,jsmediatags,musicmetadata,node-id3,mp3-parser,id3-parser,wav-file-info?start=600)
|
|
5
|
+
[](https://coveralls.io/github/Borewit/music-metadata?branch=master)
|
|
6
|
+
[](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)
|
|
7
|
+
[](https://github.com/Borewit/music-metadata/actions/workflows/codeql-analysis.yml)
|
|
8
|
+
[](https://deepscan.io/dashboard#view=project&tid=5165&pid=6938&bid=61821)
|
|
9
|
+
[](https://snyk.io/test/github/Borewit/music-metadata?targetFile=package.json)
|
|
10
|
+
[](https://discord.gg/KyBr6sb)
|
|
11
|
+
|
|
12
|
+
# music-metadata
|
|
13
|
+
|
|
14
|
+
Stream and file based music metadata parser for [node.js](https://nodejs.org/) and browser projects.
|
|
15
|
+
Supports any common audio and tagging format.
|
|
16
|
+
|
|
17
|
+
## Compatibility
|
|
18
|
+
|
|
19
|
+
Module: version 8 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
|
|
20
|
+
JavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).
|
|
21
|
+
Requires Node.js ≥ 16 engine.
|
|
22
|
+
Primarily designed for [Node.js](https://nodejs.org/), but has also been designed for browser compatibility.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
### Support for audio file types
|
|
27
|
+
|
|
28
|
+
| Audio format | Description | Wiki | |
|
|
29
|
+
| ------------- |---------------------------------| -------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------:|
|
|
30
|
+
| AIFF / AIFF-C | Audio Interchange File Format | [:link:](https://wikipedia.org/wiki/Audio_Interchange_File_Format) | <img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Apple_Computer_Logo_rainbow.svg" width="40" alt="Apple rainbow logo"> |
|
|
31
|
+
| AAC | ADTS / Advanced Audio Coding | [:link:](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) | <img src="https://svgshare.com/i/UT8.svg" width="40" alt="AAC logo"> |
|
|
32
|
+
| APE | Monkey's Audio | [:link:](https://wikipedia.org/wiki/Monkey's_Audio) | <img src="https://foreverhits.files.wordpress.com/2015/05/ape_audio.jpg" width="40" alt="Monkey's Audio logo"> |
|
|
33
|
+
| ASF | Advanced Systems Format | [:link:](https://wikipedia.org/wiki/Advanced_Systems_Format) | |
|
|
34
|
+
| BWF | Broadcast Wave Format | [:link:](https://en.wikipedia.org/wiki/Broadcast_Wave_Format) | |
|
|
35
|
+
| DSDIFF | Philips DSDIFF | [:link:](https://wikipedia.org/wiki/Direct_Stream_Digital) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/DSDlogo.svg" width="80" alt="DSD logo"> |
|
|
36
|
+
| DSF | Sony's DSD Stream File | [:link:](https://wikipedia.org/wiki/Direct_Stream_Digital) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/DSDlogo.svg" width="80" alt="DSD logo"> |
|
|
37
|
+
| FLAC | Free Lossless Audio Codec | [:link:](https://wikipedia.org/wiki/FLAC) | <img src="https://upload.wikimedia.org/wikipedia/commons/a/a2/FLAC_logo_vector.svg" width="80" alt="FLAC logo"> |
|
|
38
|
+
| MP2 | MPEG-1 Audio Layer II | [:link:](https://wikipedia.org/wiki/MPEG-1_Audio_Layer_II) | |
|
|
39
|
+
| Matroska | Matroska (EBML), mka, mkv | [:link:](https://wikipedia.org/wiki/Matroska) | <img src="https://upload.wikimedia.org/wikipedia/commons/1/1a/Matroska_2010.svg" width="80" alt="Matroska logo"> |
|
|
40
|
+
| MP3 | MPEG-1 / MPEG-2 Audio Layer III | [:link:](https://wikipedia.org/wiki/MP3) | <img src="https://upload.wikimedia.org/wikipedia/commons/e/ea/Mp3.svg" width="80" alt="MP3 logo"> |
|
|
41
|
+
| MPC | Musepack SV7 | [:link:](https://wikipedia.org/wiki/Musepack) | <img src="https://www.musepack.net/pictures/musepack_logo.png" width="80" alt="musepack logo"> |
|
|
42
|
+
| MPEG 4 | mp4, m4a, m4v | [:link:](https://wikipedia.org/wiki/MPEG-4) | <img src="https://svgshare.com/i/UU3.svg" width="80" alt="mpeg 4 logo"> |
|
|
43
|
+
| Ogg | Open container format | [:link:](https://en.wikipedia.org/wiki/Ogg) | <img src="https://upload.wikimedia.org/wikipedia/commons/a/a1/Ogg_Logo.svg" width="80" alt="Ogg logo"> |
|
|
44
|
+
| Opus | | [:link:](https://wikipedia.org/wiki/Opus_(audio_format)) | <img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Opus_logo2.svg" width="80" alt="Opus logo"> |
|
|
45
|
+
| Speex | | [:link:](https://wikipedia.org/wiki/Speex) | <img src="https://upload.wikimedia.org/wikipedia/commons/b/b5/Speex_logo_2006.svg" width="80" alt="Speex logo"> |
|
|
46
|
+
| Theora | | [:link:](https://en.wikipedia.org/wiki/Theora) | <img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Theora_logo_2007.svg" width="70" alt="Theora logo"> |
|
|
47
|
+
| Vorbis | Vorbis audio compression | [:link:](https://wikipedia.org/wiki/Ogg_Vorbis) | <img src="https://upload.wikimedia.org/wikipedia/commons/8/8d/Xiph.Org_logo_square.svg" width="70" alt="Vorbis logo"> |
|
|
48
|
+
| WAV | RIFF WAVE | [:link:](https://wikipedia.org/wiki/WAV) | |
|
|
49
|
+
| WebM | webm | [:link:](https://wikipedia.org/wiki/WebM) | <img src="https://upload.wikimedia.org/wikipedia/commons/3/34/WebM_logo.svg" width="80" alt="Matroska logo"> |
|
|
50
|
+
| WV | WavPack | [:link:](https://wikipedia.org/wiki/WavPack) | <img src="http://www.wavpack.com/wavpacklogo.svg" width="60" alt="WavPack logo"> |
|
|
51
|
+
| WMA | Windows Media Audio | [:link:](https://wikipedia.org/wiki/Windows_Media_Audio) | <img src="https://upload.wikimedia.org/wikipedia/commons/7/76/Windows_Media_Player_simplified_logo.svg" width="40" alt="Windows Media logo"> |
|
|
52
|
+
|
|
53
|
+
### Supported tag headers
|
|
54
|
+
|
|
55
|
+
Following tag header formats are supported:
|
|
56
|
+
- [APE](https://wikipedia.org/wiki/APE_tag)
|
|
57
|
+
- [ASF](https://wikipedia.org/wiki/Advanced_Systems_Format)
|
|
58
|
+
- EXIF 2.3
|
|
59
|
+
- [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)
|
|
60
|
+
- [iTunes](https://github.com/sergiomb2/libmp4v2/wiki/iTunesMetadata)
|
|
61
|
+
- [RIFF](https://wikipedia.org/wiki/Resource_Interchange_File_Format)/INFO
|
|
62
|
+
- [Vorbis comment](https://wikipedia.org/wiki/Vorbis_comment)
|
|
63
|
+
- [AIFF](https://wikipedia.org/wiki/Audio_Interchange_File_Format)
|
|
64
|
+
|
|
65
|
+
It allows many tags to be accessed in audio format, and tag format independent way.
|
|
66
|
+
|
|
67
|
+
Support for [MusicBrainz](https://musicbrainz.org/) tags as written by [Picard](https://picard.musicbrainz.org/).
|
|
68
|
+
[ReplayGain](https://wiki.hydrogenaud.io/index.php?title=ReplayGain) tags are supported.
|
|
69
|
+
|
|
70
|
+
### Audio format & encoding details
|
|
71
|
+
|
|
72
|
+
Support for encoding / format details:
|
|
73
|
+
- [Bit rate](https://wikipedia.org/wiki/Bit_rate)
|
|
74
|
+
- [Audio bit depth](https://wikipedia.org/wiki/Audio_bit_depth)
|
|
75
|
+
- Duration
|
|
76
|
+
- Encoding profile (e.g. [CBR](https://en.wikipedia.org/wiki/Constant_bitrate), V0, V2)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
## Online demo's
|
|
80
|
+
- [<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/)
|
|
81
|
+
- [<img src="https://cdn.sanity.io/images/3do82whm/next/ba8c847f13a5fa39d88f8bc9b7846b7886531b18-2500x2500.svg" width="40"> Webamp](https://webamp.org/)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Sponsor
|
|
85
|
+
[Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
|
|
86
|
+
|
|
87
|
+
<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>
|
|
88
|
+
|
|
89
|
+
## Dependencies
|
|
90
|
+
|
|
91
|
+
Dependency diagram:
|
|
92
|
+
```mermaid
|
|
93
|
+
graph TD;
|
|
94
|
+
MMN("music-metadata (Node.js entry point)")-->MMP
|
|
95
|
+
MMN-->FTN
|
|
96
|
+
MMP("music-metadata (primary entry point)")-->S(strtok3)
|
|
97
|
+
MMP-->TY(token-types)
|
|
98
|
+
MMP-->FTP
|
|
99
|
+
MMP-->UAE
|
|
100
|
+
FTN("file-type (Node.js entry point)")-->FTP
|
|
101
|
+
FTP("file-type (primary entry point)")-->S
|
|
102
|
+
S(strtok3)-->P(peek-readable)
|
|
103
|
+
S(strtok3)-->TO("@tokenizer/token")
|
|
104
|
+
TY(token-types)-->TO
|
|
105
|
+
TY-->IE("ieee754")
|
|
106
|
+
FTP-->TY
|
|
107
|
+
NS("node:stream")
|
|
108
|
+
FTN-->NS
|
|
109
|
+
FTP-->UAE(uint8array-extras)
|
|
110
|
+
style NS fill:#F88,stroke:#A44
|
|
111
|
+
style IE fill:#CCC,stroke:#888
|
|
112
|
+
style FTN fill:#FAA,stroke:#A44
|
|
113
|
+
style MMN fill:#FAA,stroke:#A44
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Dependency list:
|
|
117
|
+
- [tokenizer-token](https://github.com/Borewit/tokenizer-token)
|
|
118
|
+
- [strtok3](https://github.com/Borewit/strtok3)
|
|
119
|
+
- [token-types](https://github.com/Borewit/token-types)
|
|
120
|
+
- [file-type](https://github.com/sindresorhus/file-type)
|
|
121
|
+
- [@tokenizer-token](https://github.com/Borewit/tokenizer-token)
|
|
122
|
+
- [peek-readable](https://github.com/Borewit/peek-readable)
|
|
123
|
+
- [readable-web-to-node-stream](https://github.com/Borewit/readable-web-to-node-stream)
|
|
124
|
+
|
|
125
|
+
## Usage
|
|
126
|
+
|
|
127
|
+
### Installation
|
|
128
|
+
Install using [npm](http://npmjs.org/):
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
npm install music-metadata
|
|
132
|
+
```
|
|
133
|
+
or using [yarn](https://yarnpkg.com/):
|
|
134
|
+
```bash
|
|
135
|
+
yarn add music-metadata
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Import music-metadata
|
|
139
|
+
|
|
140
|
+
Import music-metadata:
|
|
141
|
+
```JavaScript
|
|
142
|
+
import { parseFile } from 'music-metadata';
|
|
143
|
+
```
|
|
144
|
+
Import the methods you need, like `parseFile` in this example.
|
|
145
|
+
|
|
146
|
+
### Module Functions
|
|
147
|
+
|
|
148
|
+
There are multiple ways to parse (read) audio tracks:
|
|
149
|
+
1. In Node.js, audio (music) files can be parsed using direct file access using the [parseFile function](#parsefile-function)
|
|
150
|
+
1. By parsing from a Web stream using the [parseStream function](#parsewebstream-function).
|
|
151
|
+
1. By parsing a (Web API) [Blob](https://developer.mozilla.org/docs/Web/API/Blob) or a [File](https://developer.mozilla.org/docs/Web/API/File) using the [parseBlob function](#parseblob-function).
|
|
152
|
+
1. From a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) using the [parseBuffer function](#parsebuffer-function).
|
|
153
|
+
1. Via your own, or a third-party [strtok3](https://github.com/Borewit/strtok3) ITokenizer using the [parseFromTokenizer function](#parsefromtokenizer-function).
|
|
154
|
+
|
|
155
|
+
Direct file access tends to be a little faster, because it can 'jump' to various parts in the file without being obliged to read intermediate data.
|
|
156
|
+
|
|
157
|
+
#### parseFile function
|
|
158
|
+
|
|
159
|
+
This method can only be used if the JavaScript engine is Node.js.
|
|
160
|
+
To read from a [File](https://developer.mozilla.org/docs/Web/API/File),
|
|
161
|
+
please see [fileTypeFromBlob(blob)](#parseblob-function).
|
|
162
|
+
|
|
163
|
+
Parses the specified file (`filePath`) and returns a promise with the metadata result (`IAudioMetadata`).
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
parseFile(filePath: string, opts: IOptions = {}): Promise<IAudioMetadata>`
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Example:
|
|
170
|
+
```js
|
|
171
|
+
import { parseFile } from 'music-metadata';
|
|
172
|
+
import { inspect } from 'util';
|
|
173
|
+
|
|
174
|
+
(async () => {
|
|
175
|
+
try {
|
|
176
|
+
const metadata = await parseFile('../music-metadata/test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
|
|
177
|
+
console.log(inspect(metadata, { showHidden: false, depth: null }));
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error(error.message);
|
|
180
|
+
}
|
|
181
|
+
})();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### parseStream function
|
|
185
|
+
|
|
186
|
+
_Only available using a Node.js JavaScript engines._
|
|
187
|
+
|
|
188
|
+
Parses the provided audio stream for metadata.
|
|
189
|
+
The stream should be of type [Node.js Readable](https://nodejs.org/api/stream.html#class-streamreadable).
|
|
190
|
+
It is recommended to provide the corresponding [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types).
|
|
191
|
+
An extension (e.g.: `.mp3`), filename or path will also work.
|
|
192
|
+
If the MIME-type or filename (via `fileInfo.path`) is not provided, or not understood, music-metadata will try to derive the type from the content.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
parseStream(stream: Stream.Readable, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>`
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Example:
|
|
199
|
+
```js
|
|
200
|
+
import { parseStream } from 'music-metadata';
|
|
201
|
+
|
|
202
|
+
(async () => {
|
|
203
|
+
try {
|
|
204
|
+
const metadata = await parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838});
|
|
205
|
+
console.log(metadata);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
console.error(error.message);
|
|
208
|
+
}
|
|
209
|
+
})();
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### parseWebStream function
|
|
213
|
+
|
|
214
|
+
Parses the provided audio web stream for metadata.
|
|
215
|
+
The stream should be of type [ReadableStream<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream).
|
|
216
|
+
It is recommended to provide the corresponding [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types).
|
|
217
|
+
An extension (e.g.: `.mp3`), filename or path will also work.
|
|
218
|
+
If the MIME-type or filename (via `fileInfo.path`) is not provided, or not understood, music-metadata will try to derive the type from the content.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
parseWebStream(stream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>`
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### parseBlob function
|
|
225
|
+
|
|
226
|
+
_This method can only be used if with a Node.js JavaScript engine._
|
|
227
|
+
|
|
228
|
+
Parse an audio file from a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or [File](https://developer.mozilla.org/en-US/docs/Web/API/File).
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
const musicMetadata = require('music-metadata');
|
|
232
|
+
|
|
233
|
+
let blob;
|
|
234
|
+
|
|
235
|
+
musicMetadata.parseBlob(blob).then(metadata => {
|
|
236
|
+
// metadata has all the metadata found in the blob or file
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
Or with async/await if you prefer:
|
|
240
|
+
```js
|
|
241
|
+
(async () => {
|
|
242
|
+
let blob; // File or Blob
|
|
243
|
+
|
|
244
|
+
const metadata = await musicMetadata.parseBlob(blob);
|
|
245
|
+
// metadata has all the metadata found in the blob or file
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### parseBuffer function
|
|
250
|
+
|
|
251
|
+
Parse metadata from an audio file, where the audio file is held in a [Buffer](https://nodejs.org/api/buffer.html).
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
parseBuffer(buffer: Uint8Array, fileInfo?: IFileInfo | string, opts?: IOptions = {}): Promise<IAudioMetadata>
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Example:
|
|
258
|
+
```js
|
|
259
|
+
import { parseBuffer } from 'music-metadata';
|
|
260
|
+
|
|
261
|
+
(async () => {
|
|
262
|
+
try {
|
|
263
|
+
const metadata = await parseBuffer(someBuffer, 'audio/mpeg');
|
|
264
|
+
console.log(metadata);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.error(error.message);
|
|
267
|
+
}
|
|
268
|
+
})();
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
#### parseFromTokenizer function
|
|
272
|
+
This is a low level function, reading from a [strtok3](https://github.com/Borewit/strtok3) ITokenizer interface.
|
|
273
|
+
|
|
274
|
+
This also enables special read modules like:
|
|
275
|
+
- [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).
|
|
276
|
+
|
|
277
|
+
#### orderTags function
|
|
278
|
+
|
|
279
|
+
Utility to Converts the native tags to a dictionary index on the tag identifier
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
orderTags(nativeTags: ITag[]): [tagId: string]: any[]
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
```js
|
|
286
|
+
import { parseFile, orderTags } from 'music-metadata';
|
|
287
|
+
import { inspect } from 'util';
|
|
288
|
+
|
|
289
|
+
(async () => {
|
|
290
|
+
try {
|
|
291
|
+
const metadata = await parseFile('../test/samples/MusicBrainz - Beth Hart - Sinner\'s Prayer [id3v2.3].V2.mp3');
|
|
292
|
+
const orderedTags = orderTags(metadata.native['ID3v2.3']);
|
|
293
|
+
console.log(inspect(orderedTags, { showHidden: false, depth: null }));
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error(error.message);
|
|
296
|
+
}
|
|
297
|
+
})();
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### ratingToStars function
|
|
301
|
+
|
|
302
|
+
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.
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
ratingToStars(rating: number): number
|
|
306
|
+
```
|
|
307
|
+
#### selectCover function
|
|
308
|
+
|
|
309
|
+
Select cover image based on image type field, otherwise the first picture in file.
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
export function selectCover(pictures?: IPicture[]): IPicture | null
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
import { parseFile, selectCover } from 'music-metadata';
|
|
317
|
+
|
|
318
|
+
(async () => {
|
|
319
|
+
const {common} = await parseFile(filePath);
|
|
320
|
+
const cover = selectCover(common.picture); // pick the cover image
|
|
321
|
+
}
|
|
322
|
+
)();
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Options
|
|
326
|
+
- `duration`: default: `false`, if set to `true`, it will parse the whole media file if required to determine the duration.
|
|
327
|
+
- `observer: (update: MetadataEvent) => void;`: Will be called after each change to `common` (generic) tag, or `format` properties.
|
|
328
|
+
- `skipCovers`: default: `false`, if set to `true`, it will not return embedded cover-art (images).
|
|
329
|
+
- `skipPostHeaders? boolean` default: `false`, if set to `true`, it will not search all the entire track for additional headers. Only recommenced to use in combination with streams.
|
|
330
|
+
- `includeChapters` default: `false`, if set to `true`, it will parse chapters (currently only MP4 files). _experimental functionality_
|
|
331
|
+
|
|
332
|
+
Although in most cases duration is included, in some cases it requires `music-metadata` parsing the entire file.
|
|
333
|
+
To enforce parsing the entire file if needed you should set `duration` to `true`.
|
|
334
|
+
|
|
335
|
+
### Metadata result
|
|
336
|
+
|
|
337
|
+
If the returned promise resolves, the metadata (TypeScript `IAudioMetadata` interface) contains:
|
|
338
|
+
- [`metadata.format`](#metadataformat) Audio format information
|
|
339
|
+
- [`metadata.common`](#metadatacommon) Is a generic (abstract) way of reading metadata information.
|
|
340
|
+
- [`metadata.trackInfo`](#metadatatrackInfo) Is a generic (abstract) way of reading metadata information.
|
|
341
|
+
- `metadata.native` List of native (original) tags found in the parsed audio file.
|
|
342
|
+
|
|
343
|
+
#### `metadata.format`
|
|
344
|
+
|
|
345
|
+
The questionmark `?` indicates the property is optional.
|
|
346
|
+
|
|
347
|
+
Audio format information. Defined in the TypeScript `IFormat` interface:
|
|
348
|
+
- `format.container?: string` Audio encoding format. e.g.: 'flac'
|
|
349
|
+
- `format.codec?` Name of the codec (algorithm used for the audio compression)
|
|
350
|
+
- `format.codecProfile?: string` Codec profile / settings
|
|
351
|
+
- `format.tagTypes?: TagType[]` List of tagging formats found in parsed audio file
|
|
352
|
+
- `format.duration?: number` Duration in seconds
|
|
353
|
+
- `format.bitrate?: number` Number bits per second of encoded audio file
|
|
354
|
+
- `format.sampleRate?: number` Sampling rate in Samples per second (S/s)
|
|
355
|
+
- `format.bitsPerSample?: number` Audio bit depth
|
|
356
|
+
- `format.lossless?: boolean` True if lossless, false for lossy encoding
|
|
357
|
+
- `format.numberOfChannels?: number` Number of audio channels
|
|
358
|
+
- `format.creationTime?: Date` Track creation time
|
|
359
|
+
- `format.modificationTime?: Date` Track modification / tag update time
|
|
360
|
+
- `format.trackGain?: number` Track gain in dB
|
|
361
|
+
- `format.albumGain?: number` Album gain in dB
|
|
362
|
+
|
|
363
|
+
#### `metadata.trackInfo`
|
|
364
|
+
|
|
365
|
+
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,
|
|
366
|
+
|
|
367
|
+
`metadata.trackInfo` is either `undefined` or has an **array** of [trackInfo](#trackinfo)
|
|
368
|
+
|
|
369
|
+
##### trackInfo
|
|
370
|
+
|
|
371
|
+
Audio format information. Defined in the TypeScript `IFormat` interface:
|
|
372
|
+
- `trackInfo.type?: TrackType` Track type
|
|
373
|
+
- `trackInfo.codecName?: string` Codec name
|
|
374
|
+
- `trackInfo.codecSettings?: string` Codec settings
|
|
375
|
+
- `trackInfo.flagEnabled?: boolean` Set if the track is usable, default: `true`
|
|
376
|
+
- `trackInfo.flagDefault?: boolean` Set if that track (audio, video or subs) SHOULD be active if no language found matches the user preference.
|
|
377
|
+
- `trackInfo.flagLacing?: boolean` Set if the track **may** contain blocks using lacing
|
|
378
|
+
- `trackInfo.name?: string` A human-readable track name.
|
|
379
|
+
- `trackInfo.language?: string` Specifies the language of the track
|
|
380
|
+
- `trackInfo.audio?: IAudioTrack`, see [`trackInfo.audioTrack`](#trackinfoaudiotrack)
|
|
381
|
+
- `trackInfo.video?: IVideoTrack`, see [`trackInfo.videoTrack`](#trackinfovideotrack)
|
|
382
|
+
|
|
383
|
+
##### `trackInfo.audioTrack`
|
|
384
|
+
|
|
385
|
+
- `audioTrack.samplingFrequency?: number`
|
|
386
|
+
- `audioTrack.outputSamplingFrequency?: number`
|
|
387
|
+
- `audioTrack.channels?: number`
|
|
388
|
+
- `audioTrack.channelPositions?: Buffer`
|
|
389
|
+
- `audioTrack.bitDepth?: number`
|
|
390
|
+
|
|
391
|
+
##### `trackInfo.videoTrack`
|
|
392
|
+
|
|
393
|
+
- `videoTrack.flagInterlaced?: boolean`
|
|
394
|
+
- `videoTrack.stereoMode?: number`
|
|
395
|
+
- `videoTrack.pixelWidth?: number`
|
|
396
|
+
- `videoTrack.pixelHeight?: number`
|
|
397
|
+
- `videoTrack.displayWidth?: number`
|
|
398
|
+
- `videoTrack.displayHeight?: number`
|
|
399
|
+
- `videoTrack.displayUnit?: number`
|
|
400
|
+
- `videoTrack.aspectRatioType?: number`
|
|
401
|
+
- `videoTrack.colourSpace?: Buffer`
|
|
402
|
+
- `videoTrack.gammaValue?: number`
|
|
403
|
+
|
|
404
|
+
#### `metadata.common`
|
|
405
|
+
|
|
406
|
+
[Common tag documentation](doc/common_metadata.md) is automatically generated.
|
|
407
|
+
|
|
408
|
+
## Examples
|
|
409
|
+
|
|
410
|
+
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.
|
|
411
|
+
```js
|
|
412
|
+
import { parseStream } from 'music-metadata';
|
|
413
|
+
import { inspect } from 'util';
|
|
414
|
+
|
|
415
|
+
(async () => {
|
|
416
|
+
const metadata = await parseStream(someReadStream, {mimeType: 'audio/mpeg', size: 26838}, {duration: true});
|
|
417
|
+
console.log(inspect(metadata, {showHidden: false, depth: null}));
|
|
418
|
+
someReadStream.close();
|
|
419
|
+
}
|
|
420
|
+
)();
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### Access cover art
|
|
424
|
+
|
|
425
|
+
Via `metadata.common.picture` you can access an array of cover art if present.
|
|
426
|
+
Each picture has this interface:
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
/**
|
|
430
|
+
* Attached picture, typically used for cover art
|
|
431
|
+
*/
|
|
432
|
+
export interface IPicture {
|
|
433
|
+
/**
|
|
434
|
+
* Image mime type
|
|
435
|
+
*/
|
|
436
|
+
format: string;
|
|
437
|
+
/**
|
|
438
|
+
* Image data
|
|
439
|
+
*/
|
|
440
|
+
data: Buffer;
|
|
441
|
+
/**
|
|
442
|
+
* Optional description
|
|
443
|
+
*/
|
|
444
|
+
description?: string;
|
|
445
|
+
/**
|
|
446
|
+
* Picture type
|
|
447
|
+
*/
|
|
448
|
+
type?: string;
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
To assign `img` HTML-object you can do something like:
|
|
453
|
+
```js
|
|
454
|
+
import {uint8ArrayToBase64} from 'uint8array-extras';
|
|
455
|
+
|
|
456
|
+
img.src = `data:${picture.format};base64,${uint8ArrayToBase64(picture.data)}`;
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
## Frequently Asked Questions
|
|
460
|
+
|
|
461
|
+
1. How can I traverse (a long) list of files?
|
|
462
|
+
|
|
463
|
+
What is important that file parsing should be done in a sequential manner.
|
|
464
|
+
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.
|
|
465
|
+
There are multiple ways of achieving this:
|
|
466
|
+
|
|
467
|
+
1. Using recursion
|
|
468
|
+
|
|
469
|
+
```js
|
|
470
|
+
import { parseFile } from 'music-metadata';
|
|
471
|
+
|
|
472
|
+
function parseFiles(audioFiles) {
|
|
473
|
+
|
|
474
|
+
const audioFile = audioFiles.shift();
|
|
475
|
+
|
|
476
|
+
if (audioFile) {
|
|
477
|
+
return parseFile(audioFile).then(metadata => {
|
|
478
|
+
// Do great things with the metadata
|
|
479
|
+
return parseFiles(audioFiles); // process rest of the files AFTER we are finished
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
2. Use async/await
|
|
487
|
+
|
|
488
|
+
Use [async/await](https://javascript.info/async-await)
|
|
489
|
+
|
|
490
|
+
```js
|
|
491
|
+
import { parseFile } from 'music-metadata';
|
|
492
|
+
|
|
493
|
+
// it is required to declare the function 'async' to allow the use of await
|
|
494
|
+
async function parseFiles(audioFiles) {
|
|
495
|
+
|
|
496
|
+
for (const audioFile of audioFiles) {
|
|
497
|
+
|
|
498
|
+
// await will ensure the metadata parsing is completed before we move on to the next file
|
|
499
|
+
const metadata = await parseFile(audioFile);
|
|
500
|
+
// Do great things with the metadata
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
3. Use a specialized module to traverse files
|
|
506
|
+
|
|
507
|
+
There are specialized modules to traversing (walking) files and directory,
|
|
508
|
+
like [walk](https://www.npmjs.com/package/walk).
|
|
509
|
+
|
|
510
|
+
## Licence
|
|
511
|
+
|
|
512
|
+
The MIT License (MIT)
|
|
513
|
+
|
|
514
|
+
Copyright © 2022 Borewit
|
|
515
|
+
|
|
516
|
+
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:
|
|
517
|
+
|
|
518
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
519
|
+
|
|
520
|
+
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.
|