fix: extract stream level metadata

This commit is contained in:
Deluan
2020-02-18 09:57:33 -05:00
parent 894536c8ec
commit 4ceaea7732
2 changed files with 55 additions and 24 deletions
+22 -15
View File
@@ -143,21 +143,21 @@ func isAudioFile(extension string) bool {
var ( var (
tagsRx = map[*regexp.Regexp]string{ tagsRx = map[*regexp.Regexp]string{
regexp.MustCompile(`(?i)^\s{4}compilation\s+:(.*)`): "compilation", regexp.MustCompile(`(?i)^\s{4,6}compilation\s+:(.*)`): "compilation",
regexp.MustCompile(`(?i)^\s{4}genre\s+:\s(.*)`): "genre", regexp.MustCompile(`(?i)^\s{4,6}genre\s+:\s(.*)`): "genre",
regexp.MustCompile(`(?i)^\s{4}title\s+:\s(.*)`): "title", regexp.MustCompile(`(?i)^\s{4,6}title\s+:\s(.*)`): "title",
regexp.MustCompile(`(?i)^\s{4}comment\s+:\s(.*)`): "comment", regexp.MustCompile(`(?i)^\s{4,6}comment\s+:\s(.*)`): "comment",
regexp.MustCompile(`(?i)^\s{4}artist\s+:\s(.*)`): "artist", regexp.MustCompile(`(?i)^\s{4,6}artist\s+:\s(.*)`): "artist",
regexp.MustCompile(`(?i)^\s{4}album_artist\s+:\s(.*)`): "album_artist", regexp.MustCompile(`(?i)^\s{4,6}album_artist\s+:\s(.*)`): "album_artist",
regexp.MustCompile(`(?i)^\s{4}TCM\s+:\s(.*)`): "composer", regexp.MustCompile(`(?i)^\s{4,6}TCM\s+:\s(.*)`): "composer",
regexp.MustCompile(`(?i)^\s{4}album\s+:\s(.*)`): "album", regexp.MustCompile(`(?i)^\s{4,6}album\s+:\s(.*)`): "album",
regexp.MustCompile(`(?i)^\s{4}track\s+:\s(.*)`): "trackNum", regexp.MustCompile(`(?i)^\s{4,6}track\s+:\s(.*)`): "trackNum",
regexp.MustCompile(`(?i)^\s{4}tracktotal\s+:\s(.*)`): "trackTotal", regexp.MustCompile(`(?i)^\s{4,6}tracktotal\s+:\s(.*)`): "trackTotal",
regexp.MustCompile(`(?i)^\s{4}disc\s+:\s(.*)`): "discNum", regexp.MustCompile(`(?i)^\s{4,6}disc\s+:\s(.*)`): "discNum",
regexp.MustCompile(`(?i)^\s{4}disctotal\s+:\s(.*)`): "discTotal", regexp.MustCompile(`(?i)^\s{4,6}disctotal\s+:\s(.*)`): "discTotal",
regexp.MustCompile(`(?i)^\s{4}TPA\s+:\s(.*)`): "discNum", regexp.MustCompile(`(?i)^\s{4,6}TPA\s+:\s(.*)`): "discNum",
regexp.MustCompile(`(?i)^\s{4}date\s+:\s(.*)`): "year", regexp.MustCompile(`(?i)^\s{4,6}date\s+:\s(.*)`): "year",
regexp.MustCompile(`^\s{4}Stream #\d+:\d+: (.+):\s`): "hasPicture", regexp.MustCompile(`^\s{4}Stream #\d+:\d+: (Video):.*pic\)$`): "hasPicture",
} }
durationRx = regexp.MustCompile(`^\s\sDuration: ([\d.:]+).*bitrate: (\d+)`) durationRx = regexp.MustCompile(`^\s\sDuration: ([\d.:]+).*bitrate: (\d+)`)
@@ -168,7 +168,14 @@ func (m *Metadata) parseInfo(info string) {
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
if len(line) == 0 {
continue
}
for rx, tag := range tagsRx { for rx, tag := range tagsRx {
// Skip when the tag was previously found
if _, ok := m.tags[tag]; ok {
continue
}
match := rx.FindStringSubmatch(line) match := rx.FindStringSubmatch(line)
if len(match) > 0 { if len(match) > 0 {
m.tags[tag] = match[1] m.tags[tag] = match[1]
+33 -9
View File
@@ -67,31 +67,55 @@ var _ = Describe("Metadata", func() {
}) })
Context("extractMetadata", func() { Context("extractMetadata", func() {
It("detects embedded cover art correctly", func() {
const output = `
Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/Putumayo Presents Blues Lounge/09 Pablo's Blues.mp3':
Metadata:
compilation : 1
Duration: 00:00:01.02, start: 0.000000, bitrate: 477 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 192 kb/s
Stream #0:1: Video: mjpeg (Baseline), gray(bt470bg/unknown/unknown), 600x600 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc (attached pic)`
md, _ := extractMetadata("tests/fixtures/test.mp3", output)
Expect(md.HasPicture()).To(BeTrue())
})
It("parses correctly the compilation tag", func() { It("parses correctly the compilation tag", func() {
const outputWithOverlappingTitleTag = ` const output = `
Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/Putumayo Presents Blues Lounge/09 Pablo's Blues.mp3': Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/Putumayo Presents Blues Lounge/09 Pablo's Blues.mp3':
Metadata: Metadata:
compilation : 1 compilation : 1
Duration: 00:05:02.63, start: 0.000000, bitrate: 140 kb/s` Duration: 00:05:02.63, start: 0.000000, bitrate: 140 kb/s`
md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag) md, _ := extractMetadata("tests/fixtures/test.mp3", output)
Expect(md.Compilation()).To(BeTrue()) Expect(md.Compilation()).To(BeTrue())
}) })
It("parses correct the title without overlapping with the stream tag", func() { It("parses stream level tags", func() {
const outputWithOverlappingTitleTag = ` const output = `
Input #0, ogg, from './01-02 Drive (Teku).opus':
Metadata:
ALBUM : Hot Wheels Acceleracers Soundtrack
Duration: 00:03:37.37, start: 0.007500, bitrate: 135 kb/s
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp
Metadata:
TITLE : Drive (Teku)`
md, _ := extractMetadata("tests/fixtures/test.mp3", output)
Expect(md.Title()).To(Equal("Drive (Teku)"))
})
It("does not overlap top level tags with the stream level tags", func() {
const output = `
Input #0, mp3, from 'groovin.mp3': Input #0, mp3, from 'groovin.mp3':
Metadata: Metadata:
title : Groovin' (feat. Daniel Sneijers, Susanne Alt) title : Groovin' (feat. Daniel Sneijers, Susanne Alt)
Duration: 00:03:34.28, start: 0.025056, bitrate: 323 kb/s Duration: 00:03:34.28, start: 0.025056, bitrate: 323 kb/s
Metadata: Metadata:
title : cover title : garbage`
At least one output file must be specified` md, _ := extractMetadata("tests/fixtures/test.mp3", output)
md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag)
Expect(md.Title()).To(Equal("Groovin' (feat. Daniel Sneijers, Susanne Alt)")) Expect(md.Title()).To(Equal("Groovin' (feat. Daniel Sneijers, Susanne Alt)"))
}) })
It("ignores case in the tag name", func() { It("ignores case in the tag name", func() {
const outputWithOverlappingTitleTag = ` const output = `
Input #0, flac, from '/Users/deluan/Downloads/06. Back In Black.flac': Input #0, flac, from '/Users/deluan/Downloads/06. Back In Black.flac':
Metadata: Metadata:
ALBUM : Back In Black ALBUM : Back In Black
@@ -103,7 +127,7 @@ Input #0, flac, from '/Users/deluan/Downloads/06. Back In Black.flac':
TRACKTOTAL : 10 TRACKTOTAL : 10
track : 6 track : 6
Duration: 00:04:16.00, start: 0.000000, bitrate: 995 kb/s` Duration: 00:04:16.00, start: 0.000000, bitrate: 995 kb/s`
md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag) md, _ := extractMetadata("tests/fixtures/test.mp3", output)
Expect(md.Title()).To(Equal("Back In Black")) Expect(md.Title()).To(Equal("Back In Black"))
Expect(md.Album()).To(Equal("Back In Black")) Expect(md.Album()).To(Equal("Back In Black"))
Expect(md.Genre()).To(Equal("Hard Rock")) Expect(md.Genre()).To(Equal("Hard Rock"))