Skip to content

Commit

Permalink
Add support for multi-line tags
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Nov 11, 2020
1 parent 99d454d commit aee4eb7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
26 changes: 21 additions & 5 deletions scanner/metadata/ffmpeg.go
Expand Up @@ -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+)`)

Expand Down Expand Up @@ -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 {
Expand All @@ -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])
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
}
}

// Skip when the tag was previously found
if _, ok := m.tags[tagName]; !ok {
m.tags[tagName] = tagValue
if lastTag != "" {
match = continuationRx.FindStringSubmatch(line)
if len(match) > 0 {
tagValue := m.tags[lastTag]
m.tags[lastTag] = tagValue + "\n" + strings.TrimSpace(match[1])
continue
}
continue
}

lastTag = ""
match = coverRx.FindStringSubmatch(line)
if len(match) > 0 {
m.tags["has_picture"] = "true"
Expand Down
6 changes: 2 additions & 4 deletions scanner/metadata/ffmpeg_test.go
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
})
Expand Down
1 change: 1 addition & 0 deletions scanner/metadata/taglib_test.go
Expand Up @@ -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())
Expand Down
Binary file modified tests/fixtures/test.mp3
Binary file not shown.

0 comments on commit aee4eb7

Please sign in to comment.