-
Notifications
You must be signed in to change notification settings - Fork 1
/
flactools.go
77 lines (70 loc) · 1.42 KB
/
flactools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package flactools
import (
"fmt"
"strings"
"unicode"
"github.com/mewkiz/flac"
"github.com/mewkiz/flac/meta"
)
func Coalesce(args ...string) string {
for _, arg := range args {
if arg != "" {
return arg
}
}
return ""
}
func GetTags(fname string) (map[string]string, error) {
vc, err := GetVorbisComment(fname)
if err != nil {
return nil, err
}
return TagMap(vc), nil
}
func GetVorbisComment(fname string) (*meta.VorbisComment, error) {
stream, err := flac.ParseFile(fname)
if err != nil {
return nil, fmt.Errorf("can't parse %s: %w", fname, err)
}
defer stream.Close()
mdblocks := stream.Blocks
for _, blk := range mdblocks {
if blk.Type == meta.TypeVorbisComment {
vc := blk.Body.(*meta.VorbisComment)
return vc, nil
}
}
return nil, fmt.Errorf("no tags found in %s", fname)
}
func TagMap(vc *meta.VorbisComment) map[string]string {
tuplelist := vc.Tags
tags := make(map[string]string)
for _, tuple := range tuplelist {
k := tuple[0]
v := tuple[1]
tags[strings.ToUpper(k)] = v
}
return tags
}
func CleanName(x string) string {
var dn strings.Builder
spc := false // was the last character a space?
for _, r := range x {
if unicode.IsDigit(r) || unicode.IsLetter(r) {
dn.WriteRune(r)
spc = false
}
if r == ' ' && !spc {
dn.WriteRune('_')
spc = true
}
if r == '&' {
if !spc {
dn.WriteRune('_')
}
dn.WriteString("and_")
spc = true
}
}
return dn.String()
}