UNPKG

taglib-wasm

Version:

TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps

41 lines (40 loc) 1.47 kB
import { MetadataError } from "../errors/classes.js"; import { readCoverArt, readPictures } from "../simple/index.js"; import { writeFileData } from "../utils/write.js"; import { generatePictureFilename } from "./mime-detection.js"; import { joinPath as join } from "../utils/path.js"; async function exportCoverArt(audioPath, imagePath) { const coverData = await readCoverArt(audioPath); if (!coverData) { throw new MetadataError("read", `No cover art found. Path: ${audioPath}`); } await writeFileData(imagePath, coverData); } async function exportPictureByType(audioPath, imagePath, type) { const pictures = await readPictures(audioPath); const picture = pictures.find((pic) => pic.type === type); if (!picture) { throw new MetadataError( "read", `No picture of type ${type} found. Path: ${audioPath}` ); } await writeFileData(imagePath, picture.data); } async function exportAllPictures(audioPath, outputDir, options = {}) { const pictures = await readPictures(audioPath); const exportedPaths = []; for (let i = 0; i < pictures.length; i++) { const picture = pictures[i]; const filename = options.nameFormat ? options.nameFormat(picture, i) : generatePictureFilename(picture, i); const fullPath = join(outputDir, filename); await writeFileData(fullPath, picture.data); exportedPaths.push(fullPath); } return exportedPaths; } export { exportAllPictures, exportCoverArt, exportPictureByType };