|
|
|
package id3ed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ID3v11Tags struct {
|
|
|
|
SongName string
|
|
|
|
Artist string
|
|
|
|
Album string
|
|
|
|
Year int
|
|
|
|
Comment string
|
|
|
|
Track int
|
|
|
|
Genre string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves ID3v1.1 field values of provided io.ReadSeeker (usually a file)
|
|
|
|
func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
|
|
|
|
// set reader to the last 128 bytes
|
|
|
|
_, err := rs.Seek(-int64(ID3V1SIZE), io.SeekEnd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not seek: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := read(rs, 3)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(tag, []byte("TAG")) {
|
|
|
|
// no TAG, given file does not use ID3v1
|
|
|
|
return nil, fmt.Errorf("does not use ID3v1")
|
|
|
|
}
|
|
|
|
|
|
|
|
songname, err := readToString(rs, 30)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
artist, err := readToString(rs, 30)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
album, err := readToString(rs, 30)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
yearBytes, err := read(rs, 4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
year, err := strconv.Atoi(string(yearBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
comment, err := readToString(rs, 28)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip 1 null byte
|
|
|
|
_, err = read(rs, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
trackByte, err := read(rs, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// track is one byte by specification
|
|
|
|
track := int(trackByte[0])
|
|
|
|
|
|
|
|
genreByte, err := read(rs, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// genre is one byte by specification
|
|
|
|
genre, exists := id3v1genres[int(genreByte[0])]
|
|
|
|
if !exists {
|
|
|
|
genre = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ID3v11Tags{
|
|
|
|
SongName: songname,
|
|
|
|
Artist: artist,
|
|
|
|
Album: album,
|
|
|
|
Year: year,
|
|
|
|
Comment: comment,
|
|
|
|
Track: track,
|
|
|
|
Genre: genre,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetSongName() string {
|
|
|
|
return t.SongName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetArtist() string {
|
|
|
|
return t.Artist
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetAlbum() string {
|
|
|
|
return t.Album
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetYear() int {
|
|
|
|
return t.Year
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetComment() string {
|
|
|
|
return t.Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetTrack() int {
|
|
|
|
return t.Track
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ID3v11Tags) GetGenre() string {
|
|
|
|
return t.Genre
|
|
|
|
}
|