feat(server/m7-379): extract MusicBrainz IDs from audio tags during scan
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package library
|
||||
|
||||
// MusicBrainz tag keys vary by audio format. dhowden/tag's Raw() map exposes
|
||||
// the format-specific entries as-is — so we look up multiple candidates and
|
||||
// take the first non-empty match.
|
||||
//
|
||||
// References:
|
||||
// - ID3v2 (MP3): TXXX frames keyed by description.
|
||||
// - Vorbis (FLAC/OGG): all-caps comment names.
|
||||
// - MP4 (M4A): iTunes freeform atoms ("----:com.apple.iTunes:...").
|
||||
|
||||
var albumMBIDKeys = []string{
|
||||
"TXXX:MusicBrainz Album Id",
|
||||
"MUSICBRAINZ_ALBUMID",
|
||||
"----:com.apple.iTunes:MusicBrainz Album Id",
|
||||
}
|
||||
|
||||
var artistMBIDKeys = []string{
|
||||
"TXXX:MusicBrainz Artist Id",
|
||||
"MUSICBRAINZ_ARTISTID",
|
||||
"----:com.apple.iTunes:MusicBrainz Artist Id",
|
||||
}
|
||||
|
||||
// extractMBIDs reads MusicBrainz album and artist IDs from a tag.Metadata
|
||||
// Raw() map. Returns ("", "") when the keys are absent. Multi-value keys
|
||||
// (some formats list multiple artist IDs for collaborations) take only the
|
||||
// first value — Minstrel uses the primary release artist for MBCAA lookup.
|
||||
func extractMBIDs(raw map[string]interface{}) (albumMBID, artistMBID string) {
|
||||
albumMBID = firstStringValue(raw, albumMBIDKeys)
|
||||
artistMBID = firstStringValue(raw, artistMBIDKeys)
|
||||
return
|
||||
}
|
||||
|
||||
func firstStringValue(raw map[string]interface{}, keys []string) string {
|
||||
for _, k := range keys {
|
||||
v, ok := raw[k]
|
||||
if !ok || v == nil {
|
||||
continue
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
if x != "" {
|
||||
return x
|
||||
}
|
||||
case []string:
|
||||
if len(x) > 0 && x[0] != "" {
|
||||
return x[0]
|
||||
}
|
||||
case []interface{}:
|
||||
if len(x) == 0 {
|
||||
continue
|
||||
}
|
||||
if s, ok := x[0].(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user