2021-07-24 15:53:17 +02:00
|
|
|
package taglib
|
|
|
|
|
|
|
|
import (
|
2021-08-21 03:33:12 +02:00
|
|
|
"strconv"
|
|
|
|
|
2021-07-24 15:53:17 +02:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Parser struct{}
|
|
|
|
|
|
|
|
type parsedTags = map[string][]string
|
|
|
|
|
|
|
|
func (e *Parser) Parse(paths ...string) (map[string]parsedTags, error) {
|
|
|
|
fileTags := map[string]parsedTags{}
|
|
|
|
for _, path := range paths {
|
2021-09-21 00:09:39 +02:00
|
|
|
fileTags[path] = e.extractMetadata(path)
|
2021-07-24 15:53:17 +02:00
|
|
|
}
|
|
|
|
return fileTags, nil
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:09:39 +02:00
|
|
|
func (e *Parser) extractMetadata(filePath string) parsedTags {
|
2021-07-24 15:53:17 +02:00
|
|
|
tags, err := Read(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error reading metadata from file. Skipping", "filePath", filePath, err)
|
2021-09-21 00:09:39 +02:00
|
|
|
return nil
|
2021-07-24 15:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
alternativeTags := map[string][]string{
|
|
|
|
"title": {"titlesort"},
|
|
|
|
"album": {"albumsort"},
|
|
|
|
"artist": {"artistsort"},
|
|
|
|
"tracknumber": {"trck", "_track"},
|
|
|
|
}
|
|
|
|
|
2021-08-21 03:33:12 +02:00
|
|
|
if length, ok := tags["lengthinmilliseconds"]; ok && len(length) > 0 {
|
|
|
|
millis, _ := strconv.Atoi(length[0])
|
|
|
|
if duration := float64(millis) / 1000.0; duration > 0 {
|
|
|
|
tags["duration"] = []string{strconv.FormatFloat(duration, 'f', 2, 32)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:53:17 +02:00
|
|
|
for tagName, alternatives := range alternativeTags {
|
|
|
|
for _, altName := range alternatives {
|
|
|
|
if altValue, ok := tags[altName]; ok {
|
|
|
|
tags[tagName] = append(tags[tagName], altValue...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 00:09:39 +02:00
|
|
|
return tags
|
2021-07-24 15:53:17 +02:00
|
|
|
}
|