|
|
@ -10,10 +10,10 @@ import ( |
|
|
|
"github.com/Unbewohnte/id3ed/util" |
|
|
|
"github.com/Unbewohnte/id3ed/util" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Writes given ID3v1.1 tags to dst
|
|
|
|
// Writes given ID3v1.1 tag to dst
|
|
|
|
// NOTE: will not remove already existing ID3v1.1 tag if it`s present,
|
|
|
|
// NOTE: will not remove already existing ID3v1.1 tag if it`s present,
|
|
|
|
// use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!!
|
|
|
|
// use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!!
|
|
|
|
func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { |
|
|
|
func (tag *ID3v11Tag) Write(dst io.WriteSeeker) error { |
|
|
|
dst.Seek(0, io.SeekEnd) |
|
|
|
dst.Seek(0, io.SeekEnd) |
|
|
|
|
|
|
|
|
|
|
|
// TAG
|
|
|
|
// TAG
|
|
|
@ -23,31 +23,31 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Song name
|
|
|
|
// Song name
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.SongName), 30) |
|
|
|
err = util.WriteToExtent(dst, []byte(tag.SongName), 30) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Artist
|
|
|
|
// Artist
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Artist), 30) |
|
|
|
err = util.WriteToExtent(dst, []byte(tag.Artist), 30) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Album
|
|
|
|
// Album
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Album), 30) |
|
|
|
err = util.WriteToExtent(dst, []byte(tag.Album), 30) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Year
|
|
|
|
// Year
|
|
|
|
err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4) |
|
|
|
err = util.WriteToExtent(dst, []byte(fmt.Sprint(tag.Year)), 4) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Comment
|
|
|
|
// Comment
|
|
|
|
err = util.WriteToExtent(dst, []byte(tags.Comment), 28) |
|
|
|
err = util.WriteToExtent(dst, []byte(tag.Comment), 28) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -59,14 +59,14 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { |
|
|
|
|
|
|
|
|
|
|
|
// Track
|
|
|
|
// Track
|
|
|
|
trackBytes := make([]byte, 1) |
|
|
|
trackBytes := make([]byte, 1) |
|
|
|
binary.PutVarint(trackBytes, int64(tags.Track)) |
|
|
|
binary.PutVarint(trackBytes, int64(tag.Track)) |
|
|
|
_, err = dst.Write(trackBytes) |
|
|
|
_, err = dst.Write(trackBytes) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
return fmt.Errorf("could not write to dst: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Genre
|
|
|
|
//Genre
|
|
|
|
genreCode := util.GetKey(id3v1genres, tags.Genre) |
|
|
|
genreCode := util.GetKey(id3v1genres, tag.Genre) |
|
|
|
if genreCode == -1 { |
|
|
|
if genreCode == -1 { |
|
|
|
// if no genre found - encode genre code as 255
|
|
|
|
// if no genre found - encode genre code as 255
|
|
|
|
genreCode = ID3v1INVALIDGENRE |
|
|
|
genreCode = ID3v1INVALIDGENRE |
|
|
@ -82,8 +82,8 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tags
|
|
|
|
// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tag
|
|
|
|
func (tags *ID3v11Tags) WriteToFile(f *os.File) error { |
|
|
|
func (tag *ID3v11Tag) WriteToFile(f *os.File) error { |
|
|
|
defer f.Close() |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
|
|
// check for existing ID3v1.1 tag
|
|
|
|
// check for existing ID3v1.1 tag
|
|
|
@ -92,16 +92,16 @@ func (tags *ID3v11Tags) WriteToFile(f *os.File) error { |
|
|
|
return fmt.Errorf("could not seek: %s", err) |
|
|
|
return fmt.Errorf("could not seek: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tag, err := util.Read(f, 3) |
|
|
|
identifier, err := util.Read(f, 3) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
// return err
|
|
|
|
// return err
|
|
|
|
return err |
|
|
|
return err |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(tag, []byte(ID3v1IDENTIFIER)) { |
|
|
|
if !bytes.Equal(identifier, []byte(ID3v1IDENTIFIER)) { |
|
|
|
// no existing tag, just write given tags
|
|
|
|
// no existing identifier, just write given tag
|
|
|
|
err = tags.Write(f) |
|
|
|
err = tag.Write(f) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to writer: %s", err) |
|
|
|
return fmt.Errorf("could not write to writer: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -119,8 +119,8 @@ func (tags *ID3v11Tags) WriteToFile(f *os.File) error { |
|
|
|
return fmt.Errorf("could not truncate file %s", err) |
|
|
|
return fmt.Errorf("could not truncate file %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// writing new tags
|
|
|
|
// writing new tag
|
|
|
|
err = tags.Write(f) |
|
|
|
err = tag.Write(f) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not write to writer: %s", err) |
|
|
|
return fmt.Errorf("could not write to writer: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|