@@ -23,11 +23,6 @@ type Info struct {
23
23
Disc , Track , Title string
24
24
}
25
25
26
- // Ffprober within context of metadata only needs to implement GetData
27
- type Ffprober interface {
28
- GetData (filePath string ) (* ffprobe.Data , error )
29
- }
30
-
31
26
// filePath used to derive info
32
27
func New (filePath string ) * Metadata {
33
28
var file string
@@ -51,11 +46,63 @@ func New(filePath string) *Metadata {
51
46
52
47
// build info from ffprobe.Tags
53
48
func ProbeTagsToInfo (p * ffprobe.Tags ) * Info {
54
- i := & Info { Artist : p .Artist , Disc : p .Disc ,
55
- Track : p .Track , Title : p .Title }
49
+ return & Info { Artist : p .Artist , Album : p .Album , Disc : p .Disc , Track : p .Track ,
50
+ Title : p .Title }
51
+ }
52
+
53
+ // compare file & path info against ffprobe.Tags info and combine into best
54
+ // return includes boolean if info sources match (no update necessary)
55
+ func (m * Metadata ) MatchBestInfo (c , p * Info ) (* Info , bool ) {
56
+ // set custom artist
57
+ if len (c .Artist ) > 0 {
58
+ m .Info .Artist = c .Artist
59
+ }
60
+
61
+ // set custom album
62
+ if len (c .Album ) > 0 {
63
+ m .Info .Album = c .matchCleanAlbum (c .Album )
64
+ }
56
65
57
- i .Album = strings .TrimSpace (i .MatchCleanAlbum (p .Album ))
58
- return i
66
+ // compare using safeFilename since info is derived from filename
67
+ compare := p
68
+ compare .Album = safeFilename (compare .Album )
69
+ compare .Title = safeFilename (compare .Title )
70
+
71
+ // pull info from album
72
+ p .Album = p .matchCleanAlbum (p .Album )
73
+
74
+ if * m .Info != * compare {
75
+ if len (m .Info .Artist ) == 0 {
76
+ m .Info .Artist = p .Artist
77
+ }
78
+ if len (m .Info .Year ) == 0 {
79
+ m .Info .Year = p .Year
80
+ }
81
+ if len (m .Info .Month ) == 0 {
82
+ m .Info .Month = p .Month
83
+ }
84
+ if len (m .Info .Day ) == 0 {
85
+ m .Info .Day = p .Day
86
+ }
87
+ if len (m .Info .Disc ) == 0 {
88
+ m .Info .Disc = regexp .MustCompile (`^\d+` ).FindString (p .Disc )
89
+ }
90
+ if len (m .Info .Track ) == 0 {
91
+ m .Info .Track = regexp .MustCompile (`^\d+` ).FindString (p .Track )
92
+ }
93
+ // take longer album if custom album not set
94
+ if len (m .Info .Album ) == 0 || (len (c .Album ) == 0 && len (p .Album ) > len (m .Info .Album )) {
95
+ m .Info .Album = p .Album
96
+ }
97
+ // take longer title
98
+ if len (m .Info .Title ) < len (p .Title ) {
99
+ m .Info .Title = p .Title
100
+ }
101
+
102
+ return m .Info , false
103
+ }
104
+
105
+ return m .Info , true
59
106
}
60
107
61
108
// derive info album info from nested folder path
@@ -66,7 +113,7 @@ func (m *Metadata) fromPath(p string) {
66
113
// start inner-most folder, work out
67
114
foundAlbum := false
68
115
for x := len (sa )- 1 ; x >= 0 ; x -- {
69
- sa [x ] = m .Info .MatchCleanAlbum (sa [x ])
116
+ sa [x ] = m .Info .matchCleanAlbum (sa [x ])
70
117
71
118
if sa [x ] != "" {
72
119
// only overwrite album if not yet set
@@ -103,7 +150,7 @@ func (m *Metadata) fromFile(s string) {
103
150
m .Info .Title = matchAlbumOrTitle (s )
104
151
}
105
152
106
- func (i * Info ) MatchCleanAlbum (s string ) string {
153
+ func (i * Info ) matchCleanAlbum (s string ) string {
107
154
s = i .matchDiscOnly (s )
108
155
s = i .matchDate (s )
109
156
s = i .matchYearOnly (s )
@@ -143,61 +190,6 @@ func (i *Info) ToFile() string {
143
190
return out + safeFilename (i .Title )
144
191
}
145
192
146
- // compare file & path info against ffprobe.Tags info and combine into best
147
- // return includes boolean if info sources match (no update necessary)
148
- func (i * Info ) MatchBestInfo (c , p * Info ) (* Info , bool ) {
149
- // set custom artist
150
- if len (c .Artist ) > 0 {
151
- i .Artist = c .Artist
152
- }
153
-
154
- // set custom album
155
- if len (c .Album ) > 0 {
156
- i .Album = c .Album
157
- }
158
-
159
- // compare using safeFilename since info is derived from filename
160
- compare := p
161
- compare .Album = safeFilename (compare .Album )
162
- compare .Title = safeFilename (compare .Title )
163
-
164
- if * i != * compare {
165
- // info overrides probe for Artist
166
- if len (p .Artist ) == 0 || len (c .Artist ) > 0 {
167
- p .Artist = i .Artist
168
- }
169
-
170
- // update Year, Month, Day, Disc, Track if not set
171
- if len (p .Year ) == 0 {
172
- p .Year = i .Year
173
- }
174
- if len (p .Month ) == 0 {
175
- p .Month = i .Month
176
- }
177
- if len (p .Day ) == 0 {
178
- p .Day = i .Day
179
- }
180
- if len (p .Disc ) == 0 || regexp .MustCompile (`^\d+` ).FindString (p .Disc ) != p .Disc {
181
- p .Disc = i .Disc
182
- }
183
- if len (p .Track ) == 0 {
184
- p .Track = i .Track
185
- }
186
-
187
- if (len (p .Album ) < len (i .Album )) || len (c .Album ) > 0 {
188
- p .Album = i .Album
189
- }
190
-
191
- if len (p .Title ) < len (i .Title ) {
192
- p .Title = i .Title
193
- }
194
-
195
- return p , false
196
- }
197
-
198
- return i , true
199
- }
200
-
201
193
// converts roman numeral to int; only needs to support up to 5
202
194
var romanNumeralMap = map [string ]string {
203
195
"I" : "1" , "II" : "2" , "III" : "3" , "IV" : "4" , "V" : "5" ,
0 commit comments