feat(tags): add Properties.HasCover

This commit is contained in:
sentriz
2025-09-30 14:20:29 +01:00
parent c69cb8cf35
commit bc0b6c0da4
5 changed files with 40 additions and 25 deletions

No files matched your search

+1 -1
View File
@@ -30,7 +30,7 @@ require (
github.com/sentriz/gormstore v0.0.0-20220105134332-64e31f7f6981 github.com/sentriz/gormstore v0.0.0-20220105134332-64e31f7f6981
github.com/stretchr/testify v1.11.1 github.com/stretchr/testify v1.11.1
go.senan.xyz/flagconf v0.1.9 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 go.senan.xyz/wrtag v0.18.1-0.20250930121808-19bf03481e5e
golang.org/x/net v0.44.0 golang.org/x/net v0.44.0
golang.org/x/sync v0.17.0 golang.org/x/sync v0.17.0
+2 -2
View File
@@ -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= 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 h1:LBDmqiVFgijfqFXDzH97gPn0qDbg1Dq6/vxsxS/TzC4=
go.senan.xyz/flagconf v0.1.9/go.mod h1:NqOFfSwJvNWXOTUabcRZ8mPK9+sJmhStJhqtEt74wNQ= 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.9.0 h1:MD+SfrR2o+glb7+ZeLsixUcoGB81pX/AeT9vHDhFGbI=
go.senan.xyz/taglib v0.8.0/go.mod h1:UMXxjvVuML3bZ63elOoUFtY2ZjwH88KB+DKWfF50ln8= 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 h1:Q81nHdKVLExGp7+NosXA63dmPSwhijKZKzAhkC6gY4g=
go.senan.xyz/wrtag v0.18.1-0.20250930121808-19bf03481e5e/go.mod h1:iBOnQIdvi55Y47SJxc4e5k1eu1bSeUwsq4hbFZOnXHU= 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= golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
+21 -9
View File
@@ -24,28 +24,31 @@ func (Reader) CanRead(absPath string) bool {
return false return false
} }
func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error) { func (Reader) Read(absPath string) (tags.Properties, tags.Tags, error) {
out, err := exec.Command("ffprobe", "-hide_banner", "-v", "0", "-i", absPath, "-show_entries", "format", "-of", "json").Output() out, err := exec.Command("ffprobe", "-hide_banner", "-v", "0", "-i", absPath, "-show_entries", "format:stream=codec_type", "-of", "json").Output()
if err != nil { if err != nil {
return tags.Properties{}, nil, fmt.Errorf("output: %w", err) 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 { Format struct {
Duration string `json:"duration"` Duration string `json:"duration"`
BitRate string `json:"bit_rate"` BitRate string `json:"bit_rate"`
Tags map[string]string `json:"tags"` Tags map[string]string `json:"tags"`
} `json:"format"` } `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) return tags.Properties{}, nil, fmt.Errorf("read json: %w", err)
} }
durationSecs, _ := strconv.ParseFloat(format.Format.Duration, 64) durationSecs, _ := strconv.ParseFloat(d.Format.Duration, 64)
bitRateBitsPerSec, _ := strconv.Atoi(format.Format.BitRate) bitRateBitsPerSec, _ := strconv.Atoi(d.Format.BitRate)
var tgs = map[string][]string{} var tgs = map[string][]string{}
for k, vs := range format.Format.Tags { for k, vs := range d.Format.Tags {
switch k { switch k {
case "OK": case "OK":
continue continue
@@ -53,9 +56,18 @@ func (Reader) Read(absPath string) (tags.Properties, map[string][]string, error)
tgs[k] = strings.Split(vs, ";") tgs[k] = strings.Split(vs, ";")
} }
var hasCover bool
for _, s := range d.Streams {
if s.CodecType == "video" {
hasCover = true
break
}
}
props := tags.Properties{ props := tags.Properties{
Length: time.Duration(durationSecs) * time.Second, Length: time.Duration(durationSecs) * time.Second,
Bitrate: uint(bitRateBitsPerSec / 1000), Bitrate: uint(bitRateBitsPerSec / 1000),
HasCover: hasCover,
} }
return props, tgs, nil return props, tgs, nil
+2 -2
View File
@@ -21,7 +21,7 @@ func (Reader) CanRead(absPath string) bool {
return false 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) tp, err := taglib.ReadProperties(absPath)
if err != nil { if err != nil {
return tags.Properties{}, nil, fmt.Errorf("read properties: %w", err) 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{}, 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) { func (Reader) ReadCover(absPath string) ([]byte, error) {
+14 -11
View File
@@ -15,13 +15,16 @@ var ErrUnsupported = errors.New("filetype unsupported")
type Reader interface { type Reader interface {
CanRead(absPath string) bool 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) ReadCover(absPath string) ([]byte, error)
} }
type Tags = map[string][]string
type Properties struct { type Properties struct {
Length time.Duration Length time.Duration
Bitrate uint Bitrate uint
HasCover bool
} }
const ( const (
@@ -30,56 +33,56 @@ const (
FallbackGenre = "Unknown Genre" FallbackGenre = "Unknown Genre"
) )
func MustAlbum(p map[string][]string) string { func MustAlbum(p Tags) string {
if r := normtag.Get(p, normtag.Album); r != "" { if r := normtag.Get(p, normtag.Album); r != "" {
return r return r
} }
return FallbackAlbum return FallbackAlbum
} }
func MustArtist(p map[string][]string) string { func MustArtist(p Tags) string {
if r := normtag.Get(p, normtag.Artist); r != "" { if r := normtag.Get(p, normtag.Artist); r != "" {
return r return r
} }
return FallbackArtist return FallbackArtist
} }
func MustArtists(p map[string][]string) []string { func MustArtists(p Tags) []string {
if r := normtag.Values(p, normtag.Artists); len(r) > 0 { if r := normtag.Values(p, normtag.Artists); len(r) > 0 {
return r return r
} }
return []string{MustArtist(p)} return []string{MustArtist(p)}
} }
func MustAlbumArtist(p map[string][]string) string { func MustAlbumArtist(p Tags) string {
if r := normtag.Get(p, normtag.AlbumArtist); r != "" { if r := normtag.Get(p, normtag.AlbumArtist); r != "" {
return r return r
} }
return MustArtist(p) 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 { if r := normtag.Values(p, normtag.AlbumArtists); len(r) > 0 {
return r return r
} }
return []string{MustAlbumArtist(p)} return []string{MustAlbumArtist(p)}
} }
func MustGenre(p map[string][]string) string { func MustGenre(p Tags) string {
if r := normtag.Get(p, normtag.Genre); r != "" { if r := normtag.Get(p, normtag.Genre); r != "" {
return r return r
} }
return FallbackGenre return FallbackGenre
} }
func MustGenres(p map[string][]string) []string { func MustGenres(p Tags) []string {
if r := normtag.Values(p, normtag.Genres); len(r) > 0 { if r := normtag.Values(p, normtag.Genres); len(r) > 0 {
return r return r
} }
return []string{MustGenre(p)} 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() { if t := ParseDate(normtag.Get(p, normtag.OriginalDate)); !t.IsZero() {
return t.Year() return t.Year()
} }