|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package id3ed |
|
|
|
|
package v1 |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
@ -7,6 +7,8 @@ import (
|
|
|
|
|
"io" |
|
|
|
|
"os" |
|
|
|
|
"strconv" |
|
|
|
|
|
|
|
|
|
"github.com/Unbewohnte/id3ed/util" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type ID3v11Tags struct { |
|
|
|
@ -27,7 +29,7 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
|
|
|
|
|
return nil, fmt.Errorf("could not seek: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tag, err := read(rs, 3) |
|
|
|
|
tag, err := util.Read(rs, 3) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -37,22 +39,22 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
|
|
|
|
|
return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", ID3v1IDENTIFIER, tag) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
songname, err := readToString(rs, 30) |
|
|
|
|
songname, err := util.ReadToString(rs, 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
artist, err := readToString(rs, 30) |
|
|
|
|
artist, err := util.ReadToString(rs, 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
album, err := readToString(rs, 30) |
|
|
|
|
album, err := util.ReadToString(rs, 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
yearStr, err := readToString(rs, 4) |
|
|
|
|
yearStr, err := util.ReadToString(rs, 4) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -61,32 +63,32 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
|
|
|
|
|
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
comment, err := readToString(rs, 28) |
|
|
|
|
comment, err := util.ReadToString(rs, 28) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// skip 1 null byte
|
|
|
|
|
_, err = read(rs, 1) |
|
|
|
|
_, err = util.Read(rs, 1) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
trackByte, err := read(rs, 1) |
|
|
|
|
trackByte, err := util.Read(rs, 1) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
track, err := bytesToInt(trackByte) |
|
|
|
|
track, err := util.BytesToInt(trackByte) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
genreByte, err := read(rs, 1) |
|
|
|
|
genreByte, err := util.Read(rs, 1) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
genreInt, err := bytesToInt(genreByte) |
|
|
|
|
genreInt, err := util.BytesToInt(genreByte) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -119,31 +121,31 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Song name
|
|
|
|
|
err = writeToExtent(dst, []byte(tags.SongName), 30) |
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.SongName), 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Artist
|
|
|
|
|
err = writeToExtent(dst, []byte(tags.Artist), 30) |
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Artist), 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Album
|
|
|
|
|
err = writeToExtent(dst, []byte(tags.Album), 30) |
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Album), 30) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Year
|
|
|
|
|
err = writeToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4) |
|
|
|
|
err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Comment
|
|
|
|
|
err = writeToExtent(dst, []byte(tags.Comment), 28) |
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Comment), 28) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
@ -156,14 +158,13 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error {
|
|
|
|
|
// Track
|
|
|
|
|
trackBytes := make([]byte, 1) |
|
|
|
|
binary.PutVarint(trackBytes, int64(tags.Track)) |
|
|
|
|
// binary.BigEndian.PutUint16(trackBytes, uint16(tags.Track))
|
|
|
|
|
_, err = dst.Write(trackBytes) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Genre
|
|
|
|
|
genreCode := getKey(id3v1genres, tags.Genre) |
|
|
|
|
genreCode := util.GetKey(id3v1genres, tags.Genre) |
|
|
|
|
if genreCode == -1 { |
|
|
|
|
// if no genre found - encode genre code as 255
|
|
|
|
|
genreCode = ID3v1INVALIDGENRE |
|
|
|
@ -171,7 +172,7 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error {
|
|
|
|
|
genrebyte := make([]byte, 1) |
|
|
|
|
binary.PutVarint(genrebyte, int64(genreCode)) |
|
|
|
|
|
|
|
|
|
err = writeToExtent(dst, genrebyte, 1) |
|
|
|
|
err = util.WriteToExtent(dst, genrebyte, 1) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
|
} |
|
|
|
@ -186,7 +187,7 @@ func (tags *ID3v11Tags) WriteToFile(f *os.File) error {
|
|
|
|
|
// check for existing ID3v1.1 tag
|
|
|
|
|
f.Seek(-int64(ID3v1SIZE), io.SeekEnd) |
|
|
|
|
|
|
|
|
|
tag, err := read(f, 3) |
|
|
|
|
tag, err := util.Read(f, 3) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |