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.
130 lines
2.9 KiB
130 lines
2.9 KiB
3 years ago
|
package v1
|
||
3 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
3 years ago
|
"encoding/binary"
|
||
3 years ago
|
"fmt"
|
||
|
"io"
|
||
3 years ago
|
"os"
|
||
3 years ago
|
|
||
|
"github.com/Unbewohnte/id3ed/util"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
// Writes given ID3v1.1 tags to dst
|
||
3 years ago
|
// NOTE: will not remove already existing ID3v1.1 tag if it`s present,
|
||
|
// use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!!
|
||
3 years ago
|
func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error {
|
||
3 years ago
|
dst.Seek(0, io.SeekEnd)
|
||
3 years ago
|
|
||
3 years ago
|
// TAG
|
||
3 years ago
|
_, err := dst.Write([]byte(ID3v1IDENTIFIER))
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Song name
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.SongName), 30)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Artist
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Artist), 30)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Album
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Album), 30)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Year
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Comment
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Comment), 28)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
_, err = dst.Write([]byte{0})
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
// Track
|
||
|
trackBytes := make([]byte, 1)
|
||
|
binary.PutVarint(trackBytes, int64(tags.Track))
|
||
|
_, err = dst.Write(trackBytes)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
|
//Genre
|
||
3 years ago
|
genreCode := util.GetKey(id3v1genres, tags.Genre)
|
||
3 years ago
|
if genreCode == -1 {
|
||
|
// if no genre found - encode genre code as 255
|
||
3 years ago
|
genreCode = ID3v1INVALIDGENRE
|
||
3 years ago
|
}
|
||
|
genrebyte := make([]byte, 1)
|
||
|
binary.PutVarint(genrebyte, int64(genreCode))
|
||
|
|
||
3 years ago
|
err = util.WriteToExtent(dst, genrebyte, 1)
|
||
3 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not write to dst: %s", err)
|
||
|
}
|
||
|
|
||
3 years ago
|
return nil
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tags
|
||
3 years ago
|
func (tags *ID3v11Tags) WriteToFile(f *os.File) error {
|
||
3 years ago
|
defer f.Close()
|
||
|
|
||
|
// check for existing ID3v1.1 tag
|
||
3 years ago
|
_, err := f.Seek(-int64(ID3v1SIZE), io.SeekEnd)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not seek: %s", err)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
tag, err := util.Read(f, 3)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
// return err
|
||
3 years ago
|
return err
|
||
3 years ago
|
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
if !bytes.Equal(tag, []byte(ID3v1IDENTIFIER)) {
|
||
3 years ago
|
// no existing tag, just write given tags
|
||
3 years ago
|
err = tags.Write(f)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// does contain ID3v1.1 tag. Removing it
|
||
|
fStats, err := f.Stat()
|
||
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("cannot get file stats: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
err = f.Truncate(fStats.Size() - int64(ID3v1SIZE))
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not truncate file %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// writing new tags
|
||
3 years ago
|
err = tags.Write(f)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|