From aee4eb71c40ff36e18baf57d22466502658e8a95 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 11 Nov 2020 09:45:46 -0500 Subject: [PATCH] Add support for multi-line tags --- scanner/metadata/ffmpeg.go | 28 ++++++++++++++++++++++------ scanner/metadata/ffmpeg_test.go | 6 ++---- scanner/metadata/taglib_test.go | 1 + tests/fixtures/test.mp3 | Bin 60845 -> 60845 bytes 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/scanner/metadata/ffmpeg.go b/scanner/metadata/ffmpeg.go index 79a246c43..d1e4e7099 100644 --- a/scanner/metadata/ffmpeg.go +++ b/scanner/metadata/ffmpeg.go @@ -53,6 +53,9 @@ var ( // TITLE : Back In Black tagsRx = regexp.MustCompile(`(?i)^\s{4,6}([\w\s-]+)\s*:(.*)`) + // : Second comment line + continuationRx = regexp.MustCompile(`(?i)^\s+:(.*)`) + // Duration: 00:04:16.00, start: 0.000000, bitrate: 995 kb/s` durationRx = regexp.MustCompile(`^\s\sDuration: ([\d.:]+).*bitrate: (\d+)`) @@ -107,6 +110,7 @@ func (e *ffmpegExtractor) extractMetadata(filePath, info string) (*ffmpegMetadat func (m *ffmpegMetadata) parseInfo(info string) { reader := strings.NewReader(info) scanner := bufio.NewScanner(reader) + lastTag := "" for scanner.Scan() { line := scanner.Text() if len(line) == 0 { @@ -115,15 +119,27 @@ func (m *ffmpegMetadata) parseInfo(info string) { match := tagsRx.FindStringSubmatch(line) if len(match) > 0 { tagName := strings.TrimSpace(strings.ToLower(match[1])) - tagValue := strings.TrimSpace(match[2]) - - // Skip when the tag was previously found - if _, ok := m.tags[tagName]; !ok { - m.tags[tagName] = tagValue + if tagName != "" { + tagValue := strings.TrimSpace(match[2]) + // Skip when the tag was previously found + if _, ok := m.tags[tagName]; !ok { + m.tags[tagName] = tagValue + lastTag = tagName + } + continue } - continue } + if lastTag != "" { + match = continuationRx.FindStringSubmatch(line) + if len(match) > 0 { + tagValue := m.tags[lastTag] + m.tags[lastTag] = tagValue + "\n" + strings.TrimSpace(match[1]) + continue + } + } + + lastTag = "" match = coverRx.FindStringSubmatch(line) if len(match) > 0 { m.tags["has_picture"] = "true" diff --git a/scanner/metadata/ffmpeg_test.go b/scanner/metadata/ffmpeg_test.go index 0ccc8ca68..b9f1cb581 100644 --- a/scanner/metadata/ffmpeg_test.go +++ b/scanner/metadata/ffmpeg_test.go @@ -183,8 +183,7 @@ Input #0, flac, from '/Users/deluan/Downloads/06. Back In Black.flac': Expect(md.Year()).To(Equal(1980)) }) - // TODO Handle multiline tags - XIt("parses multiline tags", func() { + It("parses multiline tags", func() { const outputWithMultilineComment = ` Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'modulo.m4a': Metadata: @@ -212,8 +211,7 @@ Tracklist: 05. Vírus de Sírius 06. Doktor Fritz 07. Wunderbar -08. Quarta Dimensão -` +08. Quarta Dimensão` md, _ := e.extractMetadata("tests/fixtures/test.mp3", outputWithMultilineComment) Expect(md.Comment()).To(Equal(expectedComment)) }) diff --git a/scanner/metadata/taglib_test.go b/scanner/metadata/taglib_test.go index ec91a387d..2bdfdd7a8 100644 --- a/scanner/metadata/taglib_test.go +++ b/scanner/metadata/taglib_test.go @@ -34,6 +34,7 @@ var _ = Describe("taglibExtractor", func() { Expect(m.FilePath()).To(Equal("tests/fixtures/test.mp3")) Expect(m.Suffix()).To(Equal("mp3")) Expect(m.Size()).To(Equal(int64(60845))) + Expect(m.Comment()).To(Equal("Comment1\nComment2")) m = mds["tests/fixtures/test.ogg"] Expect(err).To(BeNil()) diff --git a/tests/fixtures/test.mp3 b/tests/fixtures/test.mp3 index 5fd9572b64ce366c360b41c9ad4c8f5573406462..e6941d360609765e8c895dbae1edff05969f5a95 100644 GIT binary patch delta 90 zcmZ2`n|bYR<_$MeokE-g7#Nrt3>iXwLKqmt8B+7o8GI^>GLwrH40)jpBL-)GUzntG eer|4RUWp+Wlwq`)G3}!IWP$lCo8R~Diw6MWG94-a delta 55 zcmZ2`n|bYR<_$MerJenK85npOQuERoobz*YQ}ap~LYxB_7?>FhH!G%{QlD&)rZ-t& LKFj9!z5C(;6iE|D