Skip to content

Commit

Permalink
metadata: ensure non-zero padding on tags, but zero padding on filena…
Browse files Browse the repository at this point in the history
…me for disc/track
  • Loading branch information
rolyatsats committed Dec 9, 2019
1 parent 1d2d2d3 commit a06547e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 26 additions & 16 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,23 @@ func (m *Metadata) NewInfo(ffp Ffprober) (*Metadata, *Info, error) {
i := &Info{}
i.FromPath(m.Infodir)

// info from file name
_, file := filepath.Split(m.Fullpath)
file = strings.TrimSuffix(file, filepath.Ext(file))
i.FromFile(file)

// info from embedded tags within audio file
d, err := ffp.GetData(m.Fullpath)
if err != nil {
return m, i, err
}

// choose best artist
if i.Artist == "" {
if m.Artist != "" {
i.Artist = m.Artist
} else {
i.Artist = d.Format.Tags.Artist
}
// artist specified or artist within tags
if m.Artist != "" {
i.Artist = m.Artist
} else {
i.Artist = d.Format.Tags.Artist
}

// info from file name
_, file := filepath.Split(m.Fullpath)
i.FromFile(strings.TrimSuffix(file, filepath.Ext(file)))

// combine info w/ embedded tags
i, m.Match = i.MatchProbeTags(d.Format.Tags)

Expand All @@ -94,10 +91,10 @@ func (i *Info) ToAlbum() string {
func (i *Info) ToFile() string {
var out string
if len(i.Disc) > 0 {
out += i.Disc + "-"
out += paddedDiscTrack(i.Disc) + "-"
}

return out + i.Track + " " + safeFilename(i.Title)
return out + paddedDiscTrack(i.Track) + " " + safeFilename(i.Title)
}

// compare file path info against ffprobe.Tags and combine into best info
Expand Down Expand Up @@ -170,6 +167,10 @@ func (i *Info) FromFile(s string) *Info {
s = i.matchDiscTrack(s)
i.Title = matchAlbumOrTitle(s)

// remove various prefixes
i.Title = strings.TrimLeft(i.Title, i.Artist + " - ")
i.Title = strings.TrimLeft(i.Title, i.Artist + "-")

return i
}

Expand Down Expand Up @@ -299,6 +300,15 @@ var discTrackRegexps = []string{
`[sd](?P<disc>\d{1})[-. _t]*(?P<track>\d{1})`,
}

func paddedDiscTrack(s string) string {
d, _ := strconv.Atoi(s)
if d == 0 {
return ""
}

return fmt.Sprintf("%02d", d)
}

func (i *Info) matchDiscTrack(s string) string {
for _, regExpStr := range discTrackRegexps {
m, r := regexpMatch(s, regExpStr)
Expand Down Expand Up @@ -364,7 +374,7 @@ func matchAlbumOrTitle(s string) string {
// replace _ with space
s = regexp.MustCompile(`[_]+`).ReplaceAllString(s, " ")

return fixWhitespace(s)
return fixWhitespace(safeFilename(s))
}

// replace whitespaces with one space
Expand All @@ -374,5 +384,5 @@ func fixWhitespace(s string) string {

// strip out characters from filename
func safeFilename(f string) string {
return regexp.MustCompile(`[^A-Za-z0-9-'!?& _()]+`).ReplaceAllString(f, "")
return regexp.MustCompile(`[^A-Za-z0-9-,'!?& _()]+`).ReplaceAllString(f, "")
}
4 changes: 2 additions & 2 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestToFile(t *testing.T) {
result string
}{
{ i: &Info{ Disc: "", Track: "6", Title: "After Midnight" },
result: "6 After Midnight",
result: "06 After Midnight",
},{
i: &Info{ Disc: "2", Track: "03", Title: "Russian Lullaby" },
result: "2-03 Russian Lullaby",
result: "02-03 Russian Lullaby",
},
}

Expand Down

0 comments on commit a06547e

Please sign in to comment.