n10v / id3v2

🎵 ID3 decoding and encoding library for Go
https://pkg.go.dev/github.com/bogem/id3v2/v2
MIT License
339 stars 52 forks source link

AddAttachedPicture not working as expected #14

Closed stve closed 7 years ago

stve commented 7 years ago

I've been working on a command line utility to replace a bunch of Ruby scripts i've had to modify mp3 tags/artwork/etc and your library has made this a breeze - thanks!

All has been working great but I've been having some difficulty setting the artwork on a file. Basically, the problem i'm seeing is that there appears to be a discrepancy between bogem/id3v2 and what taglib is showing. Here's what i'm observing:

I have an image which I've added to my mp3 using the following code:

const coverFilename string = "cover.jpg"

func addPicture() {
    filename := "Blure - Branches.mp3"

    frontCover, err := os.Open(coverFilename)
    if err != nil {
        log.Fatal("error opening cover file", err)
    }
    defer frontCover.Close()

    frontCoverBytes, err := ioutil.ReadAll(frontCover)
    if err != nil {
        log.Fatal("error reading cover file", err)
    }

    pic := id3v2.PictureFrame{
        Encoding:    id3v2.ENUTF8,
        MimeType:    "image/jpeg",
        Picture:     frontCoverBytes,
        PictureType: id3v2.PTFrontCover,
        Description: "Cover",
    }

    // Open file and find tag in it
    tag, err := id3v2.Open(filename)
    if err != nil {
        log.Fatal("error opening mp3 file", err)
    }
    defer tag.Close()

    tag.AddAttachedPicture(pic)
    if err = tag.Save(); err != nil {
        log.Fatal("Error while saving a tag:", err)
    }
}

As best I can tell, the attachment has been added. However, Finder (i'm on a mac) wasn't showing the cover on the file in Preview. That was my first indication that something wasn't right so I started to dig a little deeper. I made a few scripts in Ruby and Go to see what frames are included and this is where I'm seeing a difference.

Using Go:

Detecting MP3 files...
********************
File: Blure - Branches.mp3:
TCON
APIC

Using Ruby (taglib):

Detecting MP3 files...
********************
File: Blure - Branches.mp3
TCON

Using exiftool:

ExifTool Version Number         : 10.36
File Name                       : Blure - Branches.mp3
Directory                       : .
File Size                       : 7.1 MB
File Modification Date/Time     : 2017:01:06 11:08:58-05:00
File Access Date/Time           : 2017:01:06 11:09:12-05:00
File Inode Change Date/Time     : 2017:01:06 11:08:59-05:00
File Permissions                : rw-r--r--
File Type                       : MP3
File Type Extension             : mp3
MIME Type                       : audio/mpeg
MPEG Audio Version              : 1
Audio Layer                     : 3
Audio Bitrate                   : 320 kbps
Sample Rate                     : 44100
Channel Mode                    : Stereo
MS Stereo                       : Off
Intensity Stereo                : Off
Copyright Flag                  : False
Original Media                  : False
Emphasis                        : None
ID3 Size                        : 51131
Genre                           : Blues
Title                           :
Artist                          :
Album                           :
Year                            :
Comment                         :
Duration                        : 0:03:05 (approx)

If the file were to include an image, we'd see something like the following:

Picture MIME Type               : image/jpeg
Picture Type                    : Front Cover
Picture Description             : Cover
Picture                         : (Binary data 50939 bytes, use -b option to extract)

As you can see, the only way that I'm able to see evidence of an APIC frame is with my Go code. Am I doing something incorrectly when adding the picture frame? I'm fairly new to Go so it's entirely possible that this is something i'm doing incorrectly.

I created an example repository here with pared down versions of the scripts i've been using and the files from the above examples. Thanks!

mro commented 7 years ago

Hi @stve,

I add artwork like this https://github.com/mro/internet-radio-recorder/blob/master/src/enclosure-tag-cmd/tag.go#L94 and Finder©®™ loves it. As far as I can see, I just removed the 'description' (for reasons I can't remember).

Hope this helps.

n10v commented 7 years ago

The track initially was tagged incorrectly. That's why id3v2 read it not as expected. So at first you should delete all frames in mp3 file. Just execute this:

package main

import (
    "log"

    "github.com/bogem/id3v2"
)

func main() {
    filename := "Blure - Branches.mp3"

    // Open file and find tag in it
    tag, err := id3v2.Open(filename)
    if err != nil {
        log.Fatal("error opening mp3 file", err)
    }
    defer tag.Close()

    tag.DeleteAllFrames()
    if err = tag.Save(); err != nil {
        log.Fatal("Error while saving a tag:", err)
    }
}

and then execute addPicture.go. Everything will work fine: 2017-01-07 13 38 08

stve commented 7 years ago

That did the trick, thanks @bogem!

n10v commented 7 years ago

@stve, you are welcome!