From 34f7f1e3c8f6e0f3573f7e7501c82b13eff5dc27 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Sun, 4 Nov 2018 21:56:00 +0100 Subject: [PATCH] Add comment metadata (#44) --- README.md | 1 + cmd/tag/main.go | 1 + id3v1.go | 1 + id3v2metadata.go | 13 ++++++++++++ mp4.go | 8 ++++++++ tag.go | 3 +++ tag_test.go | 29 ++++++++++++++++----------- testdata/with_tags/sample.id3v11.mp3 | Bin 55298 -> 55298 bytes vorbis.go | 7 +++++++ 9 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ba2e3e6..de81883 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ type Metadata interface { Picture() *Picture // Artwork Lyrics() string + Comment() string Raw() map[string]interface{} // NB: raw tag names are not consistent across formats. } diff --git a/cmd/tag/main.go b/cmd/tag/main.go index 61ba0db..c34e319 100644 --- a/cmd/tag/main.go +++ b/cmd/tag/main.go @@ -95,4 +95,5 @@ func printMetadata(m tag.Metadata) { fmt.Printf(" Picture: %v\n", m.Picture()) fmt.Printf(" Lyrics: %v\n", m.Lyrics()) + fmt.Printf(" Comment: %v\n", m.Comment()) } diff --git a/id3v1.go b/id3v1.go index d333500..0953f0b 100644 --- a/id3v1.go +++ b/id3v1.go @@ -141,3 +141,4 @@ func (m metadataID3v1) Composer() string { return "" } func (metadataID3v1) Disc() (int, int) { return 0, 0 } func (m metadataID3v1) Picture() *Picture { return nil } func (m metadataID3v1) Lyrics() string { return "" } +func (m metadataID3v1) Comment() string { return m["comment"].(string) } diff --git a/id3v2metadata.go b/id3v2metadata.go index b5dfd05..6185963 100644 --- a/id3v2metadata.go +++ b/id3v2metadata.go @@ -43,6 +43,7 @@ var frames = frameNames(map[string][2]string{ "genre": [2]string{"TCO", "TCON"}, "picture": [2]string{"PIC", "APIC"}, "lyrics": [2]string{"", "USLT"}, + "comment": [2]string{"COM", "COMM"}, }) // metadataID3v2 is the implementation of Metadata used for ID3v2 tags. @@ -119,6 +120,18 @@ func (m metadataID3v2) Lyrics() string { return t.(*Comm).Text } +func (m metadataID3v2) Comment() string { + t, ok := m.frames[frames.Name("comment", m.Format())] + if !ok { + return "" + } + // id3v23 has Text, id3v24 has Description + if t.(*Comm).Description == "" { + return trimString(t.(*Comm).Text) + } + return trimString(t.(*Comm).Description) +} + func (m metadataID3v2) Picture() *Picture { v, ok := m.frames[frames.Name("picture", m.Format())] if !ok { diff --git a/mp4.go b/mp4.go index a6e3472..3970d91 100644 --- a/mp4.go +++ b/mp4.go @@ -344,6 +344,14 @@ func (m metadataMP4) Lyrics() string { return t.(string) } +func (m metadataMP4) Comment() string { + t, ok := m.data["\xa9cmt"] + if !ok { + return "" + } + return t.(string) +} + func (m metadataMP4) Picture() *Picture { v, ok := m.data["covr"] if !ok { diff --git a/tag.go b/tag.go index edffac1..3837682 100644 --- a/tag.go +++ b/tag.go @@ -134,6 +134,9 @@ type Metadata interface { // Lyrics returns the lyrics, or an empty string if unavailable. Lyrics() string + // Comment returns the comment, or an empty string if unavailable. + Comment() string + // Raw returns the raw mapping of retrieved tag names and associated values. // NB: tag/atom names are not standardised between formats. Raw() map[string]interface{} diff --git a/tag_test.go b/tag_test.go index c80211e..043d995 100644 --- a/tag_test.go +++ b/tag_test.go @@ -39,15 +39,17 @@ var fullMetadata = testMetadata{ Track: 3, TrackTotal: 6, Year: 2000, + Comment: "Test Comment", } var mp3id3v11Metadata = testMetadata{ - Album: "Test Album", - Artist: "Test Artist", - Genre: "Jazz", - Lyrics: "", - Title: "Test Title", - Track: 3, - Year: 2000, + Album: "Test Album", + Artist: "Test Artist", + Genre: "Jazz", + Lyrics: "", + Title: "Test Title", + Track: 3, + Year: 2000, + Comment: "Test Comment", } func TestReadFrom(t *testing.T) { @@ -60,11 +62,12 @@ func TestReadFrom(t *testing.T) { "with_tags/sample.m4a": fullMetadata, "with_tags/sample.mp4": fullMetadata, "with_tags/sample.ogg": fullMetadata, - "without_tags/sample.flac": emptyMetadata, - "without_tags/sample.m4a": emptyMetadata, - "without_tags/sample.mp3": emptyMetadata, - "without_tags/sample.mp4": emptyMetadata, - "without_tags/sample.ogg": emptyMetadata, + + "without_tags/sample.flac": emptyMetadata, + "without_tags/sample.m4a": emptyMetadata, + "without_tags/sample.mp3": emptyMetadata, + "without_tags/sample.mp4": emptyMetadata, + "without_tags/sample.ogg": emptyMetadata, } for path, metadata := range testdata { @@ -80,6 +83,7 @@ func TestReadFrom(t *testing.T) { } func test(t *testing.T, path string, metadata testMetadata) error { + t.Log("testing " + path) f, err := os.Open("testdata/" + path) if err != nil { return err @@ -103,6 +107,7 @@ func compareMetadata(t *testing.T, m Metadata, tt testMetadata) { testValue(t, tt.Lyrics, m.Lyrics()) testValue(t, tt.Title, m.Title()) testValue(t, tt.Year, m.Year()) + testValue(t, tt.Comment, m.Comment()) disc, discTotal := m.Disc() testValue(t, tt.Disc, disc) diff --git a/testdata/with_tags/sample.id3v11.mp3 b/testdata/with_tags/sample.id3v11.mp3 index cd85493c87a4a2617ed9e2aa0a8a519aff3abe11..2c6aa4df4db23f0bdeca6a3754670345d956f995 100644 GIT binary patch delta 125 zcmZqLz}&Qfc|-s8Vg)Q9B(=Ci!Lg_$6G&kKh-^+$X)dOmf`XBOfdN>BbAE1aY959G H49pw=d}|w= delta 125 ycmZqLz}&Qfc|-s8Vg@WAB(=Ci!Lg_$v$zDOY)(>XE*41!BLf2iY~pY+W)1+x84alb diff --git a/vorbis.go b/vorbis.go index 07f8903..9f5ecb8 100644 --- a/vorbis.go +++ b/vorbis.go @@ -243,6 +243,13 @@ func (m *metadataVorbis) Lyrics() string { return m.c["lyrics"] } +func (m *metadataVorbis) Comment() string { + if m.c["comment"] != "" { + return m.c["comment"] + } + return m.c["description"] +} + func (m *metadataVorbis) Picture() *Picture { return m.p }