music-metadata 7.11.7 → 7.12.0

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