taglib-wasm 0.3.13 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -35
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +117 -134
- package/dist/index.js.map +1 -0
- package/dist/src/constants.js +211 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/errors.js +180 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/file-utils.d.ts +2 -2
- package/dist/src/file-utils.d.ts.map +1 -1
- package/dist/src/file-utils.js +394 -0
- package/dist/src/file-utils.js.map +1 -0
- package/dist/src/mod.d.ts +4 -4
- package/dist/src/mod.d.ts.map +1 -1
- package/dist/src/mod.js +7 -0
- package/dist/src/mod.js.map +1 -0
- package/dist/src/simple.d.ts +4 -4
- package/dist/src/simple.d.ts.map +1 -1
- package/dist/src/{simple.ts → simple.js} +193 -311
- package/dist/src/simple.js.map +1 -0
- package/dist/src/taglib.d.ts +3 -3
- package/dist/src/taglib.d.ts.map +1 -1
- package/dist/src/taglib.js +516 -0
- package/dist/src/taglib.js.map +1 -0
- package/dist/src/types.d.ts +2 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +239 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/file.js +65 -0
- package/dist/src/utils/file.js.map +1 -0
- package/dist/src/utils/write.js +49 -0
- package/dist/src/utils/write.js.map +1 -0
- package/dist/src/wasm-workers.d.ts +1 -1
- package/dist/src/wasm-workers.d.ts.map +1 -1
- package/dist/src/wasm-workers.js +148 -0
- package/dist/src/wasm-workers.js.map +1 -0
- package/dist/src/wasm.js +6 -0
- package/dist/src/wasm.js.map +1 -0
- package/dist/src/web-utils.d.ts +2 -2
- package/dist/src/web-utils.d.ts.map +1 -1
- package/{src/web-utils.ts → dist/src/web-utils.js} +102 -184
- package/dist/src/web-utils.js.map +1 -0
- package/dist/src/workers.d.ts +2 -2
- package/dist/src/workers.d.ts.map +1 -1
- package/dist/src/workers.js +389 -0
- package/dist/src/workers.js.map +1 -0
- package/dist/taglib-wrapper.js +8 -2528
- package/package.json +7 -10
- package/dist/index.ts +0 -221
- package/dist/src/constants.ts +0 -227
- package/dist/src/errors.ts +0 -254
- package/dist/src/file-utils.ts +0 -483
- package/dist/src/file.js +0 -52
- package/dist/src/global.d.ts +0 -12
- package/dist/src/mod.ts +0 -19
- package/dist/src/taglib.ts +0 -961
- package/dist/src/types.ts +0 -538
- package/dist/src/utils/file.ts +0 -86
- package/dist/src/utils/write.ts +0 -66
- package/dist/src/wasm-workers.ts +0 -176
- package/dist/src/wasm.ts +0 -133
- package/dist/src/web-utils.ts +0 -347
- package/dist/src/workers.ts +0 -461
- package/dist/src/write.js +0 -33
- package/index.ts +0 -221
- package/lib/taglib/COPYING.LGPL +0 -502
- package/lib/taglib/COPYING.MPL +0 -470
- package/lib/taglib/README.md +0 -24
- package/src/constants.ts +0 -227
- package/src/errors.ts +0 -254
- package/src/file-utils.ts +0 -483
- package/src/global.d.ts +0 -12
- package/src/mod.ts +0 -19
- package/src/simple.ts +0 -667
- package/src/taglib.ts +0 -961
- package/src/types.ts +0 -538
- package/src/utils/file.ts +0 -86
- package/src/utils/write.ts +0 -66
- package/src/wasm-workers.ts +0 -176
- package/src/wasm.ts +0 -133
- package/src/workers.ts +0 -461
package/src/simple.ts
DELETED
|
@@ -1,667 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Simplified API for taglib-wasm matching go-taglib's interface
|
|
3
|
-
*
|
|
4
|
-
* This module provides a dead-simple API for reading and writing audio metadata,
|
|
5
|
-
* inspired by go-taglib's excellent developer experience.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* import { readTags, applyTags, updateTags, readProperties } from "taglib-wasm/simple";
|
|
10
|
-
*
|
|
11
|
-
* // Read tags
|
|
12
|
-
* const tags = await readTags("song.mp3");
|
|
13
|
-
* console.log(tags.album);
|
|
14
|
-
*
|
|
15
|
-
* // Apply tags (returns modified buffer)
|
|
16
|
-
* const modifiedBuffer = await applyTags("song.mp3", {
|
|
17
|
-
* album: "New Album",
|
|
18
|
-
* artist: "New Artist"
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* // Update tags on disk
|
|
22
|
-
* await updateTags("song.mp3", {
|
|
23
|
-
* album: "New Album",
|
|
24
|
-
* artist: "New Artist"
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* // Read audio properties
|
|
28
|
-
* const props = await readProperties("song.mp3");
|
|
29
|
-
* console.log(`Duration: ${props.length}s, Bitrate: ${props.bitrate}kbps`);
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import { TagLib } from "./taglib.ts";
|
|
34
|
-
import type { AudioProperties, Picture, Tag } from "./types.ts";
|
|
35
|
-
import { PictureType } from "./types.ts";
|
|
36
|
-
import {
|
|
37
|
-
FileOperationError,
|
|
38
|
-
InvalidFormatError,
|
|
39
|
-
MetadataError,
|
|
40
|
-
} from "./errors.ts";
|
|
41
|
-
import { readFileData } from "./utils/file.ts";
|
|
42
|
-
import { writeFileData } from "./utils/write.ts";
|
|
43
|
-
|
|
44
|
-
// Cached TagLib instance for auto-initialization
|
|
45
|
-
let cachedTagLib: TagLib | null = null;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get or create a TagLib instance with auto-initialization.
|
|
49
|
-
* Uses a cached instance for performance.
|
|
50
|
-
*
|
|
51
|
-
* @internal
|
|
52
|
-
* @returns Promise resolving to TagLib instance
|
|
53
|
-
*/
|
|
54
|
-
async function getTagLib(): Promise<TagLib> {
|
|
55
|
-
if (!cachedTagLib) {
|
|
56
|
-
// Use the NPM version for compatibility
|
|
57
|
-
const { TagLib } = await import("./taglib.ts");
|
|
58
|
-
cachedTagLib = await TagLib.initialize();
|
|
59
|
-
}
|
|
60
|
-
return cachedTagLib as TagLib;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Read metadata tags from an audio file
|
|
65
|
-
*
|
|
66
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
67
|
-
* @returns Object containing all metadata tags
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```typescript
|
|
71
|
-
* const tags = await readTags("song.mp3");
|
|
72
|
-
* console.log(tags.title, tags.artist, tags.album);
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
export async function readTags(
|
|
76
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
77
|
-
): Promise<Tag> {
|
|
78
|
-
const taglib = await getTagLib();
|
|
79
|
-
const audioFile = await taglib.open(file);
|
|
80
|
-
try {
|
|
81
|
-
if (!audioFile.isValid()) {
|
|
82
|
-
throw new InvalidFormatError(
|
|
83
|
-
"File may be corrupted or in an unsupported format",
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return audioFile.tag();
|
|
88
|
-
} finally {
|
|
89
|
-
audioFile.dispose();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Apply metadata tags to an audio file and return the modified buffer
|
|
95
|
-
*
|
|
96
|
-
* This function loads the file, applies the tag changes, and returns
|
|
97
|
-
* the modified file as a buffer. The original file is not modified.
|
|
98
|
-
*
|
|
99
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
100
|
-
* @param tags - Object containing tags to apply (undefined values are ignored)
|
|
101
|
-
* @param options - Write options (currently unused, for go-taglib compatibility)
|
|
102
|
-
* @returns Modified file buffer with new tags applied
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* const modifiedBuffer = await applyTags("song.mp3", {
|
|
107
|
-
* title: "New Title",
|
|
108
|
-
* artist: "New Artist",
|
|
109
|
-
* album: "New Album",
|
|
110
|
-
* year: 2025
|
|
111
|
-
* });
|
|
112
|
-
* // Save modifiedBuffer to file or use as needed
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export async function applyTags(
|
|
116
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
117
|
-
tags: Partial<Tag>,
|
|
118
|
-
options?: number,
|
|
119
|
-
): Promise<Uint8Array> {
|
|
120
|
-
const taglib = await getTagLib();
|
|
121
|
-
const audioFile = await taglib.open(file);
|
|
122
|
-
try {
|
|
123
|
-
if (!audioFile.isValid()) {
|
|
124
|
-
throw new InvalidFormatError(
|
|
125
|
-
"File may be corrupted or in an unsupported format",
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Get the tag object and write each tag if defined
|
|
130
|
-
const tag = audioFile.tag();
|
|
131
|
-
if (tags.title !== undefined) tag.setTitle(tags.title);
|
|
132
|
-
if (tags.artist !== undefined) tag.setArtist(tags.artist);
|
|
133
|
-
if (tags.album !== undefined) tag.setAlbum(tags.album);
|
|
134
|
-
if (tags.comment !== undefined) tag.setComment(tags.comment);
|
|
135
|
-
if (tags.genre !== undefined) tag.setGenre(tags.genre);
|
|
136
|
-
if (tags.year !== undefined) tag.setYear(tags.year);
|
|
137
|
-
if (tags.track !== undefined) tag.setTrack(tags.track);
|
|
138
|
-
|
|
139
|
-
// Save changes to in-memory buffer
|
|
140
|
-
if (!audioFile.save()) {
|
|
141
|
-
throw new FileOperationError(
|
|
142
|
-
"save",
|
|
143
|
-
"Failed to save metadata changes. The file may be read-only or corrupted.",
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Get the modified buffer after saving
|
|
148
|
-
return audioFile.getFileBuffer();
|
|
149
|
-
} finally {
|
|
150
|
-
audioFile.dispose();
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Update metadata tags in an audio file and save to disk
|
|
157
|
-
*
|
|
158
|
-
* This function modifies the file on disk by applying the specified tags
|
|
159
|
-
* and writing the changes back to the original file path.
|
|
160
|
-
*
|
|
161
|
-
* @param file - File path as a string (required for disk operations)
|
|
162
|
-
* @param tags - Object containing tags to write (undefined values are ignored)
|
|
163
|
-
* @param options - Write options (currently unused, for go-taglib compatibility)
|
|
164
|
-
* @throws {InvalidInputError} If file is not a string
|
|
165
|
-
* @throws {FileOperationError} If file write fails
|
|
166
|
-
* @returns Promise that resolves when the file has been updated on disk
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```typescript
|
|
170
|
-
* // Update tags and save to disk
|
|
171
|
-
* await updateTags("song.mp3", {
|
|
172
|
-
* title: "New Title",
|
|
173
|
-
* artist: "New Artist"
|
|
174
|
-
* });
|
|
175
|
-
* // File on disk now has updated tags
|
|
176
|
-
* ```
|
|
177
|
-
*
|
|
178
|
-
* @see applyTags - For getting a modified buffer without writing to disk
|
|
179
|
-
*/
|
|
180
|
-
export async function updateTags(
|
|
181
|
-
file: string,
|
|
182
|
-
tags: Partial<Tag>,
|
|
183
|
-
options?: number,
|
|
184
|
-
): Promise<void> {
|
|
185
|
-
if (typeof file !== "string") {
|
|
186
|
-
throw new Error("updateTags requires a file path string to save changes");
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Get the modified buffer
|
|
190
|
-
const modifiedBuffer = await applyTags(file, tags, options);
|
|
191
|
-
|
|
192
|
-
// Write the buffer back to the file
|
|
193
|
-
await writeFileData(file, modifiedBuffer);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Read audio properties from a file
|
|
198
|
-
*
|
|
199
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
200
|
-
* @returns Audio properties including duration, bitrate, sample rate, etc.
|
|
201
|
-
*
|
|
202
|
-
* @example
|
|
203
|
-
* ```typescript
|
|
204
|
-
* const props = await readProperties("song.mp3");
|
|
205
|
-
* console.log(`Duration: ${props.length} seconds`);
|
|
206
|
-
* console.log(`Bitrate: ${props.bitrate} kbps`);
|
|
207
|
-
* console.log(`Sample rate: ${props.sampleRate} Hz`);
|
|
208
|
-
* ```
|
|
209
|
-
*/
|
|
210
|
-
export async function readProperties(
|
|
211
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
212
|
-
): Promise<AudioProperties> {
|
|
213
|
-
const taglib = await getTagLib();
|
|
214
|
-
const audioFile = await taglib.open(file);
|
|
215
|
-
try {
|
|
216
|
-
if (!audioFile.isValid()) {
|
|
217
|
-
throw new InvalidFormatError(
|
|
218
|
-
"File may be corrupted or in an unsupported format",
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const props = audioFile.audioProperties();
|
|
223
|
-
if (!props) {
|
|
224
|
-
throw new MetadataError(
|
|
225
|
-
"read",
|
|
226
|
-
"File may not contain valid audio data",
|
|
227
|
-
"audioProperties",
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
return props;
|
|
231
|
-
} finally {
|
|
232
|
-
audioFile.dispose();
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Tag field constants for go-taglib compatibility.
|
|
238
|
-
* These match the constants used in go-taglib for consistent API.
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```typescript
|
|
242
|
-
* import { Title, Artist, Album } from "taglib-wasm/simple";
|
|
243
|
-
*
|
|
244
|
-
* const tags = await readTags("song.mp3");
|
|
245
|
-
* console.log(tags[Title]); // Same as tags.title
|
|
246
|
-
* console.log(tags[Artist]); // Same as tags.artist
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
export const Title = "title";
|
|
250
|
-
export const Artist = "artist";
|
|
251
|
-
export const Album = "album";
|
|
252
|
-
export const Comment = "comment";
|
|
253
|
-
export const Genre = "genre";
|
|
254
|
-
export const Year = "year";
|
|
255
|
-
export const Track = "track";
|
|
256
|
-
export const AlbumArtist = "albumartist";
|
|
257
|
-
export const Composer = "composer";
|
|
258
|
-
export const DiscNumber = "discnumber";
|
|
259
|
-
|
|
260
|
-
// Additional convenience functions
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Check if a file is a valid audio file
|
|
264
|
-
*
|
|
265
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
266
|
-
* @returns true if the file is a valid audio file
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```typescript
|
|
270
|
-
* if (await isValidAudioFile("maybe-audio.bin")) {
|
|
271
|
-
* const tags = await readTags("maybe-audio.bin");
|
|
272
|
-
* }
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
export async function isValidAudioFile(
|
|
276
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
277
|
-
): Promise<boolean> {
|
|
278
|
-
try {
|
|
279
|
-
const taglib = await getTagLib();
|
|
280
|
-
const audioFile = await taglib.open(file);
|
|
281
|
-
const valid = audioFile.isValid();
|
|
282
|
-
audioFile.dispose();
|
|
283
|
-
|
|
284
|
-
return valid;
|
|
285
|
-
} catch {
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Get the audio format of a file
|
|
292
|
-
*
|
|
293
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
294
|
-
* @returns Audio format string (e.g., "MP3", "FLAC", "OGG") or undefined
|
|
295
|
-
*
|
|
296
|
-
* @example
|
|
297
|
-
* ```typescript
|
|
298
|
-
* const format = await getFormat("song.mp3");
|
|
299
|
-
* console.log(`File format: ${format}`); // "MP3"
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
export async function getFormat(
|
|
303
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
304
|
-
): Promise<string | undefined> {
|
|
305
|
-
const taglib = await getTagLib();
|
|
306
|
-
const audioFile = await taglib.open(file);
|
|
307
|
-
try {
|
|
308
|
-
if (!audioFile.isValid()) {
|
|
309
|
-
return undefined;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return audioFile.getFormat();
|
|
313
|
-
} finally {
|
|
314
|
-
audioFile.dispose();
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Clear all tags from a file
|
|
320
|
-
*
|
|
321
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
322
|
-
* @returns Modified file buffer with tags removed
|
|
323
|
-
*
|
|
324
|
-
* @example
|
|
325
|
-
* ```typescript
|
|
326
|
-
* const cleanBuffer = await clearTags("song.mp3");
|
|
327
|
-
* // Save cleanBuffer to remove all metadata
|
|
328
|
-
* ```
|
|
329
|
-
*/
|
|
330
|
-
export async function clearTags(
|
|
331
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
332
|
-
): Promise<Uint8Array> {
|
|
333
|
-
return applyTags(file, {
|
|
334
|
-
title: "",
|
|
335
|
-
artist: "",
|
|
336
|
-
album: "",
|
|
337
|
-
comment: "",
|
|
338
|
-
genre: "",
|
|
339
|
-
year: 0,
|
|
340
|
-
track: 0,
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Read cover art/pictures from an audio file
|
|
346
|
-
*
|
|
347
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
348
|
-
* @returns Array of Picture objects containing cover art
|
|
349
|
-
*
|
|
350
|
-
* @example
|
|
351
|
-
* ```typescript
|
|
352
|
-
* const pictures = await readPictures("song.mp3");
|
|
353
|
-
* for (const pic of pictures) {
|
|
354
|
-
* console.log(`Type: ${pic.type}, MIME: ${pic.mimeType}, Size: ${pic.data.length}`);
|
|
355
|
-
* }
|
|
356
|
-
* ```
|
|
357
|
-
*/
|
|
358
|
-
export async function readPictures(
|
|
359
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
360
|
-
): Promise<Picture[]> {
|
|
361
|
-
const taglib = await getTagLib();
|
|
362
|
-
const audioFile = await taglib.open(file);
|
|
363
|
-
try {
|
|
364
|
-
if (!audioFile.isValid()) {
|
|
365
|
-
throw new InvalidFormatError(
|
|
366
|
-
"File may be corrupted or in an unsupported format",
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
return audioFile.getPictures();
|
|
371
|
-
} finally {
|
|
372
|
-
audioFile.dispose();
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Apply pictures/cover art to an audio file and return the modified buffer
|
|
378
|
-
*
|
|
379
|
-
* This function loads the file, replaces all existing pictures with the new ones,
|
|
380
|
-
* and returns the modified file as a buffer. The original file is not modified.
|
|
381
|
-
*
|
|
382
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
383
|
-
* @param pictures - Array of Picture objects to set (replaces all existing)
|
|
384
|
-
* @returns Modified file buffer with new pictures applied
|
|
385
|
-
*
|
|
386
|
-
* @example
|
|
387
|
-
* ```typescript
|
|
388
|
-
* const coverArt = {
|
|
389
|
-
* mimeType: "image/jpeg",
|
|
390
|
-
* data: jpegData, // Uint8Array
|
|
391
|
-
* type: PictureType.FrontCover,
|
|
392
|
-
* description: "Album cover"
|
|
393
|
-
* };
|
|
394
|
-
* const modifiedBuffer = await applyPictures("song.mp3", [coverArt]);
|
|
395
|
-
* ```
|
|
396
|
-
*/
|
|
397
|
-
export async function applyPictures(
|
|
398
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
399
|
-
pictures: Picture[],
|
|
400
|
-
): Promise<Uint8Array> {
|
|
401
|
-
const taglib = await getTagLib();
|
|
402
|
-
const audioFile = await taglib.open(file);
|
|
403
|
-
try {
|
|
404
|
-
if (!audioFile.isValid()) {
|
|
405
|
-
throw new InvalidFormatError(
|
|
406
|
-
"File may be corrupted or in an unsupported format",
|
|
407
|
-
);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// Set the pictures
|
|
411
|
-
audioFile.setPictures(pictures);
|
|
412
|
-
|
|
413
|
-
// Save changes to in-memory buffer
|
|
414
|
-
if (!audioFile.save()) {
|
|
415
|
-
throw new FileOperationError(
|
|
416
|
-
"save",
|
|
417
|
-
"Failed to save picture changes. The file may be read-only or corrupted.",
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// Get the modified buffer after saving
|
|
422
|
-
return audioFile.getFileBuffer();
|
|
423
|
-
} finally {
|
|
424
|
-
audioFile.dispose();
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Add a single picture to an audio file and return the modified buffer
|
|
430
|
-
*
|
|
431
|
-
* This function loads the file, adds the picture to existing ones,
|
|
432
|
-
* and returns the modified file as a buffer. The original file is not modified.
|
|
433
|
-
*
|
|
434
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
435
|
-
* @param picture - Picture object to add
|
|
436
|
-
* @returns Modified file buffer with picture added
|
|
437
|
-
*
|
|
438
|
-
* @example
|
|
439
|
-
* ```typescript
|
|
440
|
-
* const backCover = {
|
|
441
|
-
* mimeType: "image/png",
|
|
442
|
-
* data: pngData, // Uint8Array
|
|
443
|
-
* type: PictureType.BackCover,
|
|
444
|
-
* description: "Back cover"
|
|
445
|
-
* };
|
|
446
|
-
* const modifiedBuffer = await addPicture("song.mp3", backCover);
|
|
447
|
-
* ```
|
|
448
|
-
*/
|
|
449
|
-
export async function addPicture(
|
|
450
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
451
|
-
picture: Picture,
|
|
452
|
-
): Promise<Uint8Array> {
|
|
453
|
-
const taglib = await getTagLib();
|
|
454
|
-
const audioFile = await taglib.open(file);
|
|
455
|
-
try {
|
|
456
|
-
if (!audioFile.isValid()) {
|
|
457
|
-
throw new InvalidFormatError(
|
|
458
|
-
"File may be corrupted or in an unsupported format",
|
|
459
|
-
);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// Add the picture
|
|
463
|
-
audioFile.addPicture(picture);
|
|
464
|
-
|
|
465
|
-
// Save changes to in-memory buffer
|
|
466
|
-
if (!audioFile.save()) {
|
|
467
|
-
throw new FileOperationError(
|
|
468
|
-
"save",
|
|
469
|
-
"Failed to save picture changes. The file may be read-only or corrupted.",
|
|
470
|
-
);
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// Get the modified buffer after saving
|
|
474
|
-
return audioFile.getFileBuffer();
|
|
475
|
-
} finally {
|
|
476
|
-
audioFile.dispose();
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Clear all pictures from a file
|
|
482
|
-
*
|
|
483
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
484
|
-
* @returns Modified file buffer with pictures removed
|
|
485
|
-
*
|
|
486
|
-
* @example
|
|
487
|
-
* ```typescript
|
|
488
|
-
* const cleanBuffer = await clearPictures("song.mp3");
|
|
489
|
-
* // Save cleanBuffer to remove all cover art
|
|
490
|
-
* ```
|
|
491
|
-
*/
|
|
492
|
-
export async function clearPictures(
|
|
493
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
494
|
-
): Promise<Uint8Array> {
|
|
495
|
-
return applyPictures(file, []);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Get the primary cover art from an audio file
|
|
500
|
-
*
|
|
501
|
-
* Returns the front cover if available, otherwise the first picture found.
|
|
502
|
-
* Returns null if no pictures are present.
|
|
503
|
-
*
|
|
504
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
505
|
-
* @returns Primary cover art data or null
|
|
506
|
-
*
|
|
507
|
-
* @example
|
|
508
|
-
* ```typescript
|
|
509
|
-
* const coverArt = await getCoverArt("song.mp3");
|
|
510
|
-
* if (coverArt) {
|
|
511
|
-
* console.log(`Cover art size: ${coverArt.length} bytes`);
|
|
512
|
-
* }
|
|
513
|
-
* ```
|
|
514
|
-
*/
|
|
515
|
-
export async function getCoverArt(
|
|
516
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
517
|
-
): Promise<Uint8Array | null> {
|
|
518
|
-
const pictures = await readPictures(file);
|
|
519
|
-
if (pictures.length === 0) {
|
|
520
|
-
return null;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
// Try to find front cover first
|
|
524
|
-
const frontCover = pictures.find((pic) =>
|
|
525
|
-
pic.type === PictureType.FrontCover
|
|
526
|
-
);
|
|
527
|
-
if (frontCover) {
|
|
528
|
-
return frontCover.data;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
// Return first picture if no front cover
|
|
532
|
-
return pictures[0].data;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Set the primary cover art for an audio file
|
|
537
|
-
*
|
|
538
|
-
* Replaces all existing pictures with a single front cover image.
|
|
539
|
-
*
|
|
540
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
541
|
-
* @param imageData - Image data as Uint8Array
|
|
542
|
-
* @param mimeType - MIME type of the image (e.g., "image/jpeg", "image/png")
|
|
543
|
-
* @returns Modified file buffer with cover art set
|
|
544
|
-
*
|
|
545
|
-
* @example
|
|
546
|
-
* ```typescript
|
|
547
|
-
* const jpegData = await Deno.readFile("cover.jpg");
|
|
548
|
-
* const modifiedBuffer = await setCoverArt("song.mp3", jpegData, "image/jpeg");
|
|
549
|
-
* ```
|
|
550
|
-
*/
|
|
551
|
-
export async function setCoverArt(
|
|
552
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
553
|
-
imageData: Uint8Array,
|
|
554
|
-
mimeType: string,
|
|
555
|
-
): Promise<Uint8Array> {
|
|
556
|
-
const picture: Picture = {
|
|
557
|
-
mimeType,
|
|
558
|
-
data: imageData,
|
|
559
|
-
type: PictureType.FrontCover,
|
|
560
|
-
description: "Front Cover",
|
|
561
|
-
};
|
|
562
|
-
return applyPictures(file, [picture]);
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
/**
|
|
566
|
-
* Find a picture by its type
|
|
567
|
-
*
|
|
568
|
-
* @param pictures - Array of pictures to search
|
|
569
|
-
* @param type - Picture type to find
|
|
570
|
-
* @returns Picture matching the type or null
|
|
571
|
-
*
|
|
572
|
-
* @example
|
|
573
|
-
* ```typescript
|
|
574
|
-
* const pictures = await readPictures("song.mp3");
|
|
575
|
-
* const backCover = findPictureByType(pictures, PictureType.BackCover);
|
|
576
|
-
* if (backCover) {
|
|
577
|
-
* console.log("Found back cover art");
|
|
578
|
-
* }
|
|
579
|
-
* ```
|
|
580
|
-
*/
|
|
581
|
-
export function findPictureByType(
|
|
582
|
-
pictures: Picture[],
|
|
583
|
-
type: PictureType,
|
|
584
|
-
): Picture | null {
|
|
585
|
-
return pictures.find((pic) => pic.type === type) || null;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
/**
|
|
589
|
-
* Replace or add a picture of a specific type
|
|
590
|
-
*
|
|
591
|
-
* If a picture of the given type already exists, it will be replaced.
|
|
592
|
-
* Otherwise, the new picture will be added to the existing ones.
|
|
593
|
-
*
|
|
594
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
595
|
-
* @param newPicture - Picture to add or replace
|
|
596
|
-
* @returns Modified file buffer with picture updated
|
|
597
|
-
*
|
|
598
|
-
* @example
|
|
599
|
-
* ```typescript
|
|
600
|
-
* const backCover: Picture = {
|
|
601
|
-
* mimeType: "image/png",
|
|
602
|
-
* data: pngData,
|
|
603
|
-
* type: PictureType.BackCover,
|
|
604
|
-
* description: "Back cover"
|
|
605
|
-
* };
|
|
606
|
-
* const modifiedBuffer = await replacePictureByType("song.mp3", backCover);
|
|
607
|
-
* ```
|
|
608
|
-
*/
|
|
609
|
-
export async function replacePictureByType(
|
|
610
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
611
|
-
newPicture: Picture,
|
|
612
|
-
): Promise<Uint8Array> {
|
|
613
|
-
const pictures = await readPictures(file);
|
|
614
|
-
|
|
615
|
-
// Remove any existing picture of the same type
|
|
616
|
-
const filteredPictures = pictures.filter((pic) =>
|
|
617
|
-
pic.type !== newPicture.type
|
|
618
|
-
);
|
|
619
|
-
|
|
620
|
-
// Add the new picture
|
|
621
|
-
filteredPictures.push(newPicture);
|
|
622
|
-
|
|
623
|
-
return applyPictures(file, filteredPictures);
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/**
|
|
627
|
-
* Get picture metadata without the actual image data
|
|
628
|
-
*
|
|
629
|
-
* Useful for checking what pictures are present without loading
|
|
630
|
-
* potentially large image data into memory.
|
|
631
|
-
*
|
|
632
|
-
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
633
|
-
* @returns Array of picture metadata (type, mimeType, description, size)
|
|
634
|
-
*
|
|
635
|
-
* @example
|
|
636
|
-
* ```typescript
|
|
637
|
-
* const metadata = await getPictureMetadata("song.mp3");
|
|
638
|
-
* for (const info of metadata) {
|
|
639
|
-
* console.log(`${info.description}: ${info.mimeType}, ${info.size} bytes`);
|
|
640
|
-
* }
|
|
641
|
-
* ```
|
|
642
|
-
*/
|
|
643
|
-
export async function getPictureMetadata(
|
|
644
|
-
file: string | Uint8Array | ArrayBuffer | File,
|
|
645
|
-
): Promise<
|
|
646
|
-
Array<{
|
|
647
|
-
type: PictureType;
|
|
648
|
-
mimeType: string;
|
|
649
|
-
description?: string;
|
|
650
|
-
size: number;
|
|
651
|
-
}>
|
|
652
|
-
> {
|
|
653
|
-
const pictures = await readPictures(file);
|
|
654
|
-
return pictures.map((pic) => ({
|
|
655
|
-
type: pic.type,
|
|
656
|
-
mimeType: pic.mimeType,
|
|
657
|
-
description: pic.description,
|
|
658
|
-
size: pic.data.length,
|
|
659
|
-
}));
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
/**
|
|
663
|
-
* Re-export commonly used types for convenience.
|
|
664
|
-
* These types define the structure of metadata and audio properties.
|
|
665
|
-
*/
|
|
666
|
-
export type { AudioProperties, Picture, Tag } from "./types.ts";
|
|
667
|
-
export { PictureType } from "./types.ts";
|