Use TagLib to detect whether a media file has embedded cover or not

This commit is contained in:
Deluan
2021-07-24 01:36:04 -04:00
parent 91325071a6
commit 6c550819fd
2 changed files with 54 additions and 30 deletions
-29
View File
@@ -1,9 +1,6 @@
package metadata
import (
"os"
"github.com/dhowden/tag"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/scanner/metadata/taglib"
)
@@ -25,10 +22,6 @@ func (e *taglibExtractor) extractMetadata(filePath string) (*Tags, error) {
parsedTags, err := taglib.Read(filePath)
if err != nil {
log.Warn("Error reading metadata from file. Skipping", "filePath", filePath, err)
} else {
if hasEmbeddedImage(filePath) {
parsedTags["has_picture"] = []string{"true"}
}
}
tags := NewTags(filePath, parsedTags, map[string][]string{
@@ -41,25 +34,3 @@ func (e *taglibExtractor) extractMetadata(filePath string) (*Tags, error) {
return tags, nil
}
func hasEmbeddedImage(path string) bool {
defer func() {
if r := recover(); r != nil {
log.Error("Panic while checking for images. Please report this error with a copy of the file", "path", path, r)
}
}()
f, err := os.Open(path)
if err != nil {
log.Warn("Error opening file", "filePath", path, err)
return false
}
defer f.Close()
m, err := tag.ReadFrom(f)
if err != nil {
log.Warn("Error reading picture tag from file", "filePath", path, err)
return false
}
return m.Picture() != nil
}