Use file name for unknown tag title retrieval.

When taglib get empty or null title from file/album, use file name or
folder name as the tag_title field. It could be confuse to user, but at
leaset it is better than tracks/albums can't be search in ID3 mode API.
This commit is contained in:
2024-06-24 16:17:45 +08:00
parent 332f00ff7a
commit 8914c59978
3 changed files with 24 additions and 7 deletions

View File

@@ -465,8 +465,9 @@ func populateTrack(tx *db.DB, album *db.Album, track *db.Track, trags tagcommon.
track.Size = size track.Size = size
track.AlbumID = album.ID track.AlbumID = album.ID
track.TagTitle = trags.Title() tagTitle := tagcommon.MustTitle(trags)
track.TagTitleUDec = decoded(trags.Title()) track.TagTitle = tagTitle
track.TagTitleUDec = decoded(tagTitle)
track.TagTrackArtist = tagcommon.MustArtist(trags) track.TagTrackArtist = tagcommon.MustArtist(trags)
track.TagTrackNumber = trags.TrackNumber() track.TagTrackNumber = trags.TrackNumber()
track.TagDiscNumber = trags.DiscNumber() track.TagDiscNumber = trags.DiscNumber()

View File

@@ -2,6 +2,7 @@ package tagcommon
import ( import (
"errors" "errors"
"path"
) )
var ErrUnsupported = errors.New("filetype unsupported") var ErrUnsupported = errors.New("filetype unsupported")
@@ -33,19 +34,31 @@ type Info interface {
Length() int Length() int
Bitrate() int Bitrate() int
AbsPath() string
} }
const ( const (
FallbackAlbum = "Unknown Album"
FallbackArtist = "Unknown Artist" FallbackArtist = "Unknown Artist"
FallbackGenre = "Unknown Genre" FallbackGenre = "Unknown Genre"
) )
func MustTitle(p Info) string {
if r := p.Title(); r != "" {
return r
}
// return the file name for title name
return path.Base(p.AbsPath())
}
func MustAlbum(p Info) string { func MustAlbum(p Info) string {
if r := p.Album(); r != "" { if r := p.Album(); r != "" {
return r return r
} }
return FallbackAlbum
// return the dir name for album name
return path.Base(path.Dir(p.AbsPath()))
} }
func MustArtist(p Info) string { func MustArtist(p Info) string {

View File

@@ -28,12 +28,13 @@ func (TagLib) Read(absPath string) (tagcommon.Info, error) {
defer f.Close() defer f.Close()
props := f.ReadAudioProperties() props := f.ReadAudioProperties()
raw := f.ReadTags() raw := f.ReadTags()
return &info{raw, props}, nil return &info{raw, props, absPath}, nil
} }
type info struct { type info struct {
raw map[string][]string raw map[string][]string
props *audiotags.AudioProperties props *audiotags.AudioProperties
abspath string
} }
// https://picard-docs.musicbrainz.org/downloads/MusicBrainz_Picard_Tag_Map.html // https://picard-docs.musicbrainz.org/downloads/MusicBrainz_Picard_Tag_Map.html
@@ -60,6 +61,8 @@ func (i *info) ReplayGainAlbumPeak() float32 { return flt(first(find(i.raw, "rep
func (i *info) Length() int { return i.props.Length } func (i *info) Length() int { return i.props.Length }
func (i *info) Bitrate() int { return i.props.Bitrate } func (i *info) Bitrate() int { return i.props.Bitrate }
func (i *info) AbsPath() string { return i.abspath }
func first[T comparable](is []T) T { func first[T comparable](is []T) T {
var z T var z T
for _, i := range is { for _, i := range is {