You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.1 KiB
110 lines
2.1 KiB
3 years ago
|
package v1
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/Unbewohnte/id3ed/util"
|
||
|
)
|
||
|
|
||
3 years ago
|
// Retrieves ID3v1 field values of provided io.ReadSeeker (usually a file)
|
||
|
func Getv1Tag(rs io.ReadSeeker) (*ID3v1Tag, error) {
|
||
|
var tag ID3v1Tag
|
||
|
|
||
3 years ago
|
// 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)
|
||
|
}
|
||
|
|
||
3 years ago
|
// ID
|
||
3 years ago
|
identifier, err := util.Read(rs, 3)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
if !bytes.Equal(identifier, []byte(ID3v1IDENTIFIER)) {
|
||
|
// no identifier, given file does not use ID3v1
|
||
|
return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", ID3v1IDENTIFIER, identifier)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// Songname
|
||
3 years ago
|
songname, err := util.ReadToString(rs, 30)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
tag.SongName = songname
|
||
3 years ago
|
|
||
3 years ago
|
// Artist
|
||
3 years ago
|
artist, err := util.ReadToString(rs, 30)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
tag.Artist = artist
|
||
3 years ago
|
|
||
3 years ago
|
// Album name
|
||
3 years ago
|
album, err := util.ReadToString(rs, 30)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
tag.Album = album
|
||
3 years ago
|
|
||
3 years ago
|
// Year
|
||
3 years ago
|
yearStr, err := util.ReadToString(rs, 4)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
year, err := strconv.Atoi(yearStr)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err)
|
||
|
}
|
||
3 years ago
|
tag.Year = year
|
||
3 years ago
|
|
||
3 years ago
|
// Comment and Track
|
||
|
comment, err := util.Read(rs, 30)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
tag.Comment = util.ToString(comment)
|
||
|
tag.Track = 0
|
||
3 years ago
|
|
||
3 years ago
|
var track int = 0
|
||
|
// check if 29th byte is null byte (v1.0 or v1.1)
|
||
|
if comment[28] == 0 {
|
||
|
// it is v1.1, track number exists
|
||
|
track, err = util.ByteToInt(comment[29])
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not get int from byte: %s", err)
|
||
|
}
|
||
|
tag.Track = uint8(track)
|
||
3 years ago
|
|
||
3 years ago
|
comment = comment[0:28]
|
||
|
tag.Comment = util.ToString(comment)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// Genre
|
||
3 years ago
|
genreByte, err := util.Read(rs, 1)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
genreInt, err := util.ByteToInt(genreByte[0])
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, fmt.Errorf("cannot convert bytes to int: %s", err)
|
||
|
}
|
||
|
genre, exists := id3v1genres[int(genreInt)]
|
||
|
if !exists {
|
||
|
genre = ""
|
||
|
}
|
||
3 years ago
|
tag.Genre = genre
|
||
|
|
||
|
if track == 0 {
|
||
|
tag.Version = V1_0
|
||
|
} else {
|
||
|
tag.Version = V1_1
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return &tag, nil
|
||
3 years ago
|
}
|