From bc0b6c0da4738c3ee8f40525e2ed1d85c51f927a Mon Sep 17 00:00:00 2001 From: sentriz Date: Tue, 30 Sep 2025 14:20:29 +0100 Subject: [PATCH] feat(tags): add Properties.HasCover --- go.mod | 2 +- go.sum | 4 ++-- tags/ffprobe/ffprobe.go | 30 +++++++++++++++++++++--------- tags/taglib/taglib.go | 4 ++-- tags/tags.go | 25 ++++++++++++++----------- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index fa78cf6..640fc8e 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/sentriz/gormstore v0.0.0-20220105134332-64e31f7f6981 github.com/stretchr/testify v1.11.1 go.senan.xyz/flagconf v0.1.9 - go.senan.xyz/taglib v0.8.0 + go.senan.xyz/taglib v0.9.0 go.senan.xyz/wrtag v0.18.1-0.20250930121808-19bf03481e5e golang.org/x/net v0.44.0 golang.org/x/sync v0.17.0 diff --git a/go.sum b/go.sum index 38681e9..9fda16e 100644 --- a/go.sum +++ b/go.sum @@ -146,8 +146,8 @@ github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+C github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.senan.xyz/flagconf v0.1.9 h1:LBDmqiVFgijfqFXDzH97gPn0qDbg1Dq6/vxsxS/TzC4= go.senan.xyz/flagconf v0.1.9/go.mod h1:NqOFfSwJvNWXOTUabcRZ8mPK9+sJmhStJhqtEt74wNQ= -go.senan.xyz/taglib v0.8.0 h1:wV67dr0TXtMcw+19K5xalSm/VDB7+a235GPNWB7bDQ8= -go.senan.xyz/taglib v0.8.0/go.mod h1:UMXxjvVuML3bZ63elOoUFtY2ZjwH88KB+DKWfF50ln8= +go.senan.xyz/taglib v0.9.0 h1:MD+SfrR2o+glb7+ZeLsixUcoGB81pX/AeT9vHDhFGbI= +go.senan.xyz/taglib v0.9.0/go.mod h1:UMXxjvVuML3bZ63elOoUFtY2ZjwH88KB+DKWfF50ln8= go.senan.xyz/wrtag v0.18.1-0.20250930121808-19bf03481e5e h1:Q81nHdKVLExGp7+NosXA63dmPSwhijKZKzAhkC6gY4g= go.senan.xyz/wrtag v0.18.1-0.20250930121808-19bf03481e5e/go.mod h1:iBOnQIdvi55Y47SJxc4e5k1eu1bSeUwsq4hbFZOnXHU= golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/tags/ffprobe/ffprobe.go b/tags/ffprobe/ffprobe.go index a00a2eb..fee52cb 100644 --- a/tags/ffprobe/ffprobe.go +++ b/tags/ffprobe/ffprobe.go @@ -24,28 +24,31 @@ func (Reader) CanRead(absPath string) bool { return false } -func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error) { - out, err := exec.Command("ffprobe", "-hide_banner", "-v", "0", "-i", absPath, "-show_entries", "format", "-of", "json").Output() +func (Reader) Read(absPath string) (tags.Properties, tags.Tags, error) { + out, err := exec.Command("ffprobe", "-hide_banner", "-v", "0", "-i", absPath, "-show_entries", "format:stream=codec_type", "-of", "json").Output() if err != nil { return tags.Properties{}, nil, fmt.Errorf("output: %w", err) } - var format struct { + var d struct { + Streams []struct { + CodecType string `json:"codec_type"` + } `json:"streams"` Format struct { Duration string `json:"duration"` BitRate string `json:"bit_rate"` Tags map[string]string `json:"tags"` } `json:"format"` } - if err := json.Unmarshal(out, &format); err != nil { + if err := json.Unmarshal(out, &d); err != nil { return tags.Properties{}, nil, fmt.Errorf("read json: %w", err) } - durationSecs, _ := strconv.ParseFloat(format.Format.Duration, 64) - bitRateBitsPerSec, _ := strconv.Atoi(format.Format.BitRate) + durationSecs, _ := strconv.ParseFloat(d.Format.Duration, 64) + bitRateBitsPerSec, _ := strconv.Atoi(d.Format.BitRate) var tgs = map[string][]string{} - for k, vs := range format.Format.Tags { + for k, vs := range d.Format.Tags { switch k { case "OK": continue @@ -53,9 +56,18 @@ func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error) tgs[k] = strings.Split(vs, ";") } + var hasCover bool + for _, s := range d.Streams { + if s.CodecType == "video" { + hasCover = true + break + } + } + props := tags.Properties{ - Length: time.Duration(durationSecs) * time.Second, - Bitrate: uint(bitRateBitsPerSec / 1000), + Length: time.Duration(durationSecs) * time.Second, + Bitrate: uint(bitRateBitsPerSec / 1000), + HasCover: hasCover, } return props, tgs, nil diff --git a/tags/taglib/taglib.go b/tags/taglib/taglib.go index 0709fe0..bafad47 100644 --- a/tags/taglib/taglib.go +++ b/tags/taglib/taglib.go @@ -21,7 +21,7 @@ func (Reader) CanRead(absPath string) bool { return false } -func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error) { +func (Reader) Read(absPath string) (tags.Properties, tags.Tags, error) { tp, err := taglib.ReadProperties(absPath) if err != nil { return tags.Properties{}, nil, fmt.Errorf("read properties: %w", err) @@ -32,7 +32,7 @@ func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error) return tags.Properties{}, nil, fmt.Errorf("read tags: %w", err) } - return tags.Properties{Length: tp.Length, Bitrate: tp.Bitrate}, tag, nil + return tags.Properties{Length: tp.Length, Bitrate: tp.Bitrate, HasCover: tp.HasImage}, tag, nil } func (Reader) ReadCover(absPath string) ([]byte, error) { diff --git a/tags/tags.go b/tags/tags.go index 8575ba6..2ac6661 100644 --- a/tags/tags.go +++ b/tags/tags.go @@ -15,13 +15,16 @@ var ErrUnsupported = errors.New("filetype unsupported") type Reader interface { CanRead(absPath string) bool - Read(absPath string) (properties Properties, tags map[string][]string, err error) + Read(absPath string) (properties Properties, tags Tags, err error) ReadCover(absPath string) ([]byte, error) } +type Tags = map[string][]string + type Properties struct { - Length time.Duration - Bitrate uint + Length time.Duration + Bitrate uint + HasCover bool } const ( @@ -30,56 +33,56 @@ const ( FallbackGenre = "Unknown Genre" ) -func MustAlbum(p map[string][]string) string { +func MustAlbum(p Tags) string { if r := normtag.Get(p, normtag.Album); r != "" { return r } return FallbackAlbum } -func MustArtist(p map[string][]string) string { +func MustArtist(p Tags) string { if r := normtag.Get(p, normtag.Artist); r != "" { return r } return FallbackArtist } -func MustArtists(p map[string][]string) []string { +func MustArtists(p Tags) []string { if r := normtag.Values(p, normtag.Artists); len(r) > 0 { return r } return []string{MustArtist(p)} } -func MustAlbumArtist(p map[string][]string) string { +func MustAlbumArtist(p Tags) string { if r := normtag.Get(p, normtag.AlbumArtist); r != "" { return r } return MustArtist(p) } -func MustAlbumArtists(p map[string][]string) []string { +func MustAlbumArtists(p Tags) []string { if r := normtag.Values(p, normtag.AlbumArtists); len(r) > 0 { return r } return []string{MustAlbumArtist(p)} } -func MustGenre(p map[string][]string) string { +func MustGenre(p Tags) string { if r := normtag.Get(p, normtag.Genre); r != "" { return r } return FallbackGenre } -func MustGenres(p map[string][]string) []string { +func MustGenres(p Tags) []string { if r := normtag.Values(p, normtag.Genres); len(r) > 0 { return r } return []string{MustGenre(p)} } -func MustYear(p map[string][]string) int { +func MustYear(p Tags) int { if t := ParseDate(normtag.Get(p, normtag.OriginalDate)); !t.IsZero() { return t.Year() }