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.
113 lines
2.5 KiB
113 lines
2.5 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.0 tags to given io.WriteSeeker.
|
||
3 years ago
|
// NOTE: will not remove already existing ID3v1 tag if it`s present,
|
||
|
// use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!!
|
||
3 years ago
|
func (tags *ID3v1Tags) 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 {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// Song name
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.SongName), 30)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// Artist
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Artist), 30)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// Album
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Album), 30)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// Year
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// Comment
|
||
3 years ago
|
err = util.WriteToExtent(dst, []byte(tags.Comment), 30)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
|
// 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))
|
||
|
|
||
|
_, err = dst.Write(genrebyte)
|
||
|
if err != nil {
|
||
3 years ago
|
return fmt.Errorf("could not write to writer: %s", err)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
return nil
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
// Checks for existing ID3v1 tag in file, if present - removes it and replaces with provided tags
|
||
3 years ago
|
func (tags *ID3v1Tags) WriteToFile(f *os.File) error {
|
||
3 years ago
|
defer f.Close()
|
||
|
|
||
|
// check for existing ID3v1 tag
|
||
3 years ago
|
f.Seek(-int64(ID3v1SIZE), io.SeekEnd)
|
||
3 years ago
|
|
||
3 years ago
|
tag, err := util.Read(f, 3)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
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 {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// does contain ID3v1 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
|
||
|
|
||
|
}
|