Add comment metadata (#44)

This commit is contained in:
Jonas L 2018-11-04 21:56:00 +01:00 committed by David Howden
parent c555fff0b7
commit 34f7f1e3c8
9 changed files with 51 additions and 12 deletions

View File

@ -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.
}

View File

@ -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())
}

View File

@ -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) }

View File

@ -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 {

8
mp4.go
View File

@ -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 {

3
tag.go
View File

@ -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{}

View File

@ -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)

Binary file not shown.

View File

@ -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
}