-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflacmover.py
More file actions
executable file
·180 lines (164 loc) · 5.55 KB
/
flacmover.py
File metadata and controls
executable file
·180 lines (164 loc) · 5.55 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
# flacmover.py -i sourcedir -o -destdir -f formatstring
import sys, getopt, os, fnmatch
import shutil, re
from mutagen import File
from mutagen.flac import FLAC, Picture
def walk(root):
for root, dirnames, filenames in os.walk(root):
cover = None
for filename in fnmatch.filter(filenames, '*.flac'):
filepath = root + '/' + filename
print(filename)
tag = File(filepath)
ext = os.path.splitext(filename)[1]
directory, filename = buildpath(tag, ext)
new_path = outpath + directory + '/'
new_filepath = new_path + filename
new_coverpath = new_path + 'folder.jpg'
print(new_filepath)
copy(filepath, new_filepath)
if not os.path.isfile(new_coverpath):
cover = getcover(filepath)
if cover != None:
print('Writing Albumart to {}'.format(new_coverpath))
cdata = open(new_coverpath, 'wb')
cdata.write(cover.data)
cdata.close()
def getcover(filepath):
covers = FLAC(filepath).pictures
for cover in covers:
if cover.type == 3:
print(str(cover))
return(cover)
return None
def buildpath(tag, ext):
parts = filenameformat.split('%')
for index, part in enumerate(parts):
if part != '':
if part in tag:
if part == 'tracknumber':
parts[index] = tag[part][0].zfill(2)
else:
parts[index] = replacer(tag[part][0])
else:
parts[index] = part
else:
parts[index] = part
path = ''.join(parts) + ext
directory = '/'.join(path.split('/')[:-1])
filename = ''.join(path.split('/')[-1])
return directory, filename
def replacer(content):
table = {
ord(u'Å'): 'Aa',
ord(u'Å'): 'aa',
ord(u'Ä'): 'Ae',
ord(u'ä'): 'ae',
ord(u'Æ'): 'Ae',
ord(u'æ'): 'ae',
ord(u'Á'): 'A',
ord(u'á'): 'a',
ord(u'À'): 'A',
ord(u'à'): 'a',
ord(u'Ã'): 'a',
ord(u'ã'): 'a',
ord(u'Ć'): 'C',
ord(u'ć'): 'c',
ord(u'Ç'): 'C',
ord(u'ç'): 'c',
ord(u'¢'): 'c',
ord(u'Ð'): 'D',
ord(u'ð'): 'd',
ord(u'É'): 'E',
ord(u'é'): 'e',
ord(u'È'): 'E',
ord(u'è'): 'e',
ord(u'Ẽ'): 'E',
ord(u'ẽ'): 'e',
ord(u'Í'): 'I',
ord(u'í'): 'i',
ord(u'Í'): 'I',
ord(u'í'): 'i',
ord(u'Ì'): 'I',
ord(u'ì'): 'i',
ord(u'Ĩ'): 'I',
ord(u'ĩ'): 'i',
ord(u'Ñ'): 'N',
ord(u'ñ'): 'n',
ord(u'Ö'): 'Oe',
ord(u'ö'): 'oe',
ord(u'Ø'): 'Oe',
ord(u'ø'): 'oe',
ord(u'Œ'): 'Oe',
ord(u'œ'): 'oe',
ord(u'Ó'): 'O',
ord(u'ó'): 'o',
ord(u'Ò'): 'O',
ord(u'ò'): 'o',
ord(u'Õ'): 'O',
ord(u'õ'): 'o',
ord(u'Ü'): 'Ue',
ord(u'ü'): 'ue',
ord(u'Ú'): 'U',
ord(u'ú'): 'u',
ord(u'Ù'): 'U',
ord(u'ù'): 'u',
ord(u'Ũ'): 'U',
ord(u'ũ'): 'u',
ord(u'ß'): 'ss',
}
content = content.translate(table)
content = re.sub('[^\w\-_\. ]','_', content)
return content
def copy(srcfile, dstfile):
if not os.path.exists(os.path.dirname(dstfile)):
try:
os.makedirs(os.path.dirname(dstfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
print('Copying {} to {}'.format(srcfile, dstfile))
shutil.copy(srcfile, dstfile)
def usage(help = False):
if help:
print('This program is intended to copy FLAC music libraries into another directory using a defined structure.\n')
print('Copies all files within a source directory recursively to another directory and (re)names directories and filenames according to a specified format taken from the files Metadata.\nFor each folder in source an albumart image is exported from a FLAC file if possible.\n')
print('flacmover.py -i SRCDIR -o DESTDIR -f FORMATSTRING \n'
+ ' -i SRCDIR source root of files; defaults to current dir \n'
+ ' -o DESTDIR destination root of the renamed dirs and files \n'
+ ' -f FORMATSTRING any format of directory file structure \n'
+ ' with / as dir delimiter and %variable% for any variable \n'
+ ' found in FLAC file tags as used by mutagen. \n'
+ ' ex.: \n'
+ ' %albumartist%/%albumartist%.%originalyear%.%album%/%discnumber%.%tracknumber%.%artist%.%title%')
exit(1)
def main(argv):
global inpath, outpath, filenameformat
inpath = os.getcwd()
outpath = None
filenameformat = None
try:
opts, args = getopt.getopt(argv,'hi:o:f:',['input=','output=','format='])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage(True)
elif opt in ('-i', '--input'):
inpath = arg
elif opt in ('-o', '--output'):
outpath = arg
elif opt in ('-f', '--format'):
filenameformat = arg
else:
usage()
print('Input path is {}'.format(inpath))
if outpath == None:
print('No output directory set!')
usage()
if not outpath.endswith('/'):
outpath = outpath + '/'
walk(inpath)
if __name__ == '__main__':
main(sys.argv[1:])