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

+21 -9
View File
@@ -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
+2 -2
View File
@@ -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) {
+14 -11
View File
@@ -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()
}