eidoriantan / mp3tag.js

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

[BUG] Year tag #634

Closed RedHeadEmile closed 9 months ago

RedHeadEmile commented 9 months ago

I try to use the "Year" tag but it seems saving the setter of the year tag struggle a bit.

Here is my code:

    const buffer = fs.readFileSync(file.absolutePath);
    const mp3tag = new MP3Tag(buffer);

    mp3tag.read({
      id3v1: false,
      id3v2: true
    });

    if (!!mp3tag.error)
      throw new Error(mp3tag.error);

    mp3tag.tags.title = tag.title ?? '';
    mp3tag.tags.artist = tag.artist ?? '';
    mp3tag.tags.album = tag.album ?? '';
    mp3tag.tags.year = tag.year ?? '';
    mp3tag.tags.track = tag.track ?? '';

    if (!tag.coverImageBase64)
      mp3tag.tags.v2!.APIC = undefined;
    else
      mp3tag.tags.v2!.APIC = [{
        data: this._base64ToArray(tag.coverImageBase64),
        format: tag.coverImageMimeType,
        type: 3,
        description: 'Cover'
      }] as any;

    const newBuffer = mp3tag.save({
      strict: true,
      id3v1: { include: false },
      id3v2: {
        include: true,
        unsynch: false,
        version: 4,
        padding: 4096
      }
    });
    if (!!mp3tag.error) {
      console.log(tag);
      throw new Error(mp3tag.error);
    }

    fs.writeFileSync(file.absolutePath, Buffer.from(newBuffer));

As you can see, I read the original file, I patch it with the data contained in my object tag then I save the patched file.

The problem is: If I set id3v2 version to 3, I get TDRC is not supported in ID3v2.3. And if I set id3v2 version to 4, I get TYER is not supported in ID3v2.4.

Note that to fill the tag object, I used the getter mp3tag.tags.year.

I think the problem is my original file as it already contains both TDRC and TYER tag and when mp3tag.js try to save it in strict mode, it messes up :(

I don't know what would be the better way to fix it, maybe having an option in the save function to delete the wrong frames ?

Thanks in advance :)

eidoriantan commented 9 months ago

I'm thinking the same thing. Adding a new option to delete unsupported frames instead of throwing errors is best for this kind of situations.

eidoriantan commented 9 months ago

Added in v3.8.0