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