95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.velvettear.de/velvettear/id3tool/internal/config"
|
|
"git.velvettear.de/velvettear/loggo"
|
|
"github.com/bogem/id3v2/v2"
|
|
)
|
|
|
|
func main() {
|
|
loggo.SetLogFormat("[$LOGLEVEL$] > $MESSAGE$ ($EXTRAS$) [$TIMEDIFF$]")
|
|
|
|
config.CheckArguments()
|
|
|
|
if len(config.Directory) == 0 {
|
|
loggo.Fatal("no directory provided")
|
|
}
|
|
stats, err := os.Stat(config.Directory)
|
|
if err != nil {
|
|
loggo.Fatal("could not get stats for provided directory", err.Error())
|
|
}
|
|
if !stats.IsDir() {
|
|
loggo.Fatal("provided argument is not a valid directory")
|
|
}
|
|
|
|
filepath.WalkDir(config.Directory, filterValidFiles)
|
|
}
|
|
|
|
func filterValidFiles(path string, dir fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmp := filepath.Clean(path)
|
|
loggo.Debug(tmp)
|
|
extension := filepath.Ext(path)
|
|
if dir.IsDir() || (extension != ".flac" && extension != ".mp3") {
|
|
return nil
|
|
}
|
|
cleanFrames(path)
|
|
return nil
|
|
}
|
|
|
|
func cleanFrames(file string) {
|
|
if len(file) == 0 {
|
|
return
|
|
}
|
|
|
|
timestamp := time.Now()
|
|
loggo.Info("cleaning file's '" + file + "' id3 tags...")
|
|
|
|
tag, err := id3v2.Open(file, id3v2.Options{Parse: true})
|
|
if err != nil {
|
|
loggo.Error("encountered an error opening the file '"+file+"' for reading the id3 tags", err.Error())
|
|
return
|
|
}
|
|
defer tag.Close()
|
|
|
|
deleted := 0
|
|
for key, _ := range tag.AllFrames() {
|
|
filter := frameFilter(key)
|
|
if !filter {
|
|
continue
|
|
}
|
|
tag.DeleteFrames(key)
|
|
deleted++
|
|
|
|
}
|
|
if deleted > 0 {
|
|
loggo.Debug("saving modified id3 tags (" + strconv.Itoa(deleted) + " deleted) to file '" + file + "'...")
|
|
err := tag.Save()
|
|
if err != nil {
|
|
loggo.Error("encountered an error saving the modified id3 tags to file '"+file+"'", err.Error())
|
|
return
|
|
}
|
|
loggo.DebugTimed("deleted "+strconv.Itoa(deleted)+" tags", timestamp.UnixMilli())
|
|
}
|
|
|
|
loggo.DebugTimed("finished cleaning file", timestamp.UnixMilli())
|
|
}
|
|
|
|
func frameFilter(frame string) bool {
|
|
for _, frameToKeep := range config.FramesToKeep {
|
|
if strings.EqualFold(frameToKeep, frame) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|