taglib-wasm 0.3.3 → 0.3.9

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