eidoriantan / mp3tag.js

MP3 tagging library written in pure JavaScript for Node.js and browsers
https://mp3tag.js.org
MIT License
83 stars 9 forks source link

Cover for song #256

Closed ivioninjs closed 3 years ago

ivioninjs commented 3 years ago

Hello! thanks for the work you've done! It's awesome tool!

But, I didn`t understand how add image (png | jpg ) to song. Could you write simple example please?

Thanks advanced!

eidoriantan commented 3 years ago

Hi! You can add the cover image to songs by modifying the APIC tag.

mp3tag.tags.v2.APIC = [{
  format: 'image/jpeg', // or image/png
  type: 3,
  description: '',
  data: bytes
}]

You can see the full list of types here. For the bytes' value, you have to read the image's buffer first then convert it to Uint8Array:

const bytes = new Uint8Array(buffer)
ivioninjs commented 3 years ago

Thanks a lot!

peshoFT commented 3 years ago

"mp3tag.tags.save is not a function" I directly copied the example that you presented

eidoriantan commented 3 years ago

"mp3tag.tags.save is not a function" I directly copied the example that you presented

Hey, you should use mp3tag.save() instead of mp3tag.tags.save()

peshoFT commented 3 years ago

"mp3tag.tags.save is not a function" I directly copied the example that you presented

Hey, you should use mp3tag.save() instead of mp3tag.tags.save()

Certainly going to try as soon as I get home, thank you in advance

peshoFT commented 3 years ago

Alright the .save works now Next question: how do I apply the tags to a given file?

eidoriantan commented 3 years ago

Alright the .save works now Next question: how do I apply the tags to a given file?

Saving it modifies the buffer at mp3tag.buffer so you can use this for download.

If you are using browser, you can download the file by:

const fileBlob = new Blob([mp3tag.buffer], { type: 'audio/mpeg' })
const a = document.createElement('a')
document.body.appendChild(a)
const url = window.URL.createObjectURL(fileBlob)
a.href = url
a.download = 'filename.mp3'
a.click()

And if you're using Node, you can use it to save the file by:

const fs = require('fs')
fs.writeFileSync('filename.mp3', mp3tag.buffer)