n10v / id3v2

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

Add support for `SetTrack` #38

Closed dimuls closed 6 years ago

dimuls commented 6 years ago

You have nice library, but i miss SetTrack method to set track number. Can you implement it please?

dimuls commented 6 years ago

I tried to implement it in this way but it is not working (track field leaves empty):

const trackFrameID = "TRCK"

// Track number id3v2.4 frame
type trackFrame struct {
    TrackNumber int
    TotalTracks int
}

func (tf trackFrame) encode() []byte {
    return []byte(fmt.Sprintf("%d/%d", tf.TrackNumber, tf.TotalTracks))
}

func (tf trackFrame) Size() int {
    return len(tf.encode())
}

func (tf trackFrame) WriteTo(w io.Writer) (int64, error) {
    n, err := w.Write(tf.encode())
    return int64(n), err
}
dimuls commented 6 years ago

This works as needed:

mp3.AddFrame(trackFrameID, id3v2.TextFrame{Encoding: id3v2.EncodingUTF8,
    Text: fmt.Sprintf("%d/%d", t.Number, len(a.Tracks))})

Issue can be closed, but consider to add SetTrack. Thank you!

n10v commented 6 years ago

I will not add SetTrack method, because it's pretty rarely used. I will keep methods only for artist, album, title and year.

But I added a convenience method for adding text frames in 7e43476:

tag.AddTextFrame(tag.CommonID("Track number/Position in set"), tag.DefaultEncoding(), fmt.Sprintf("%d/%d", t.Number, len(a.Tracks))}))