From b391bd2bd257db7b0c0809b8947983187ae156a3 Mon Sep 17 00:00:00 2001 From: Earthrot Date: Sun, 14 Jul 2024 13:43:26 +0200 Subject: [PATCH] Reading of genre from MP4 'gnre' atom MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Genre in MP4 files can be present either as ©gen (string) or gnre (uint8) mapping to the default ID3v1 list, 1-indexed. --- mp4.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mp4.go b/mp4.go index 19c1a81..b466a7c 100644 --- a/mp4.go +++ b/mp4.go @@ -30,6 +30,7 @@ var atoms = atomNames(map[string]string{ "aART": "album_artist", "\xa9day": "year", "\xa9nam": "title", + "gnre": "genreid", "\xa9gen": "genre", "trkn": "track", "\xa9wrt": "composer", @@ -183,6 +184,14 @@ func (m metadataMP4) readAtomData(r io.ReadSeeker, name string, size uint32, pro return nil } + if name == "gnre" { + if len(b) < 2 { + return fmt.Errorf("invalid encoding: expected at least %d bytes for genre id, got %d", 2, len(b)) + } + m.data[name] = int(b[1]) + return nil + } + if contentType == "implicit" { if name == "covr" { if bytes.HasPrefix(b, pngHeader) { @@ -325,6 +334,13 @@ func (m metadataMP4) Composer() string { } func (m metadataMP4) Genre() string { + if g, ok := m.data["gnre"]; ok { + gid := g.(int) + if gid > 0 && gid <= len(id3v1Genres) { + return id3v1Genres[gid-1] + } + } + return m.getString(atoms.Name("genre")) }