-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid3translate.py
More file actions
288 lines (270 loc) · 9.95 KB
/
id3translate.py
File metadata and controls
288 lines (270 loc) · 9.95 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
'''
id3translate
translate id3 tags in your audio files
TO DO
utils:
require ffmpeg
wishlist:
id3OrigObj renamed with original/ API detected language, e.g. -id3-th.txt (for thai)
id3TransObj renamed with translated lagnuage, e.g. -id3-en.txt (for english)
rename files and folders with translations
overwrite option
option for quiet mode
'''
import io
import os
import sys
import time
import types
import argparse
import subprocess
from nltk.tag import pos_tag
from mtranslate import translate
class dotdict(dict):
'''
dot.notation access to dictionary attributes
'''
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def go(ffstr):
'''
runs ffmpeg, returns true is success, error is fail
'''
try:
if os.name == 'posix':
returncode = subprocess.check_output(ffstr, shell=True)
else:
returncode = subprocess.check_output(ffstr)
return True
except subprocess.CalledProcessError, e:
returncode = e.returncode
print returncode
return returncode
def check_id3OrigObj(file):
'''
id3Check takes an input file and verifies if there is ID3 metadata already in there
'''
if not os.path.exists(file.id3OrigObj):
ffstr = 'ffmpeg -i "' + file.inputFullPath + '" -f ffmetadata -y "' + file.id3OrigObj + '"'
ffWorked = go(ffstr)
if ffWorked is not True:
return None
time.sleep(1)
b = os.path.getsize(file.id3OrigObj) #grab the size, in bytes, of the resulting text file
if b < 55:
os.remove(file.id3OrigObj)
return False
else:
return True
else:
return True
def id3file_to_dict(file):
'''
walk line by line through each metadata element in the id3OrigObj
'''
tagsOrig = dotdict({})
contents = [line.rstrip() for line in open(file.id3OrigObj) if "=" in line]
for line in contents:
tag, value = line.split("=")
tagsOrig[tag] = value
return tagsOrig
def translate_tags(args, tagsOrig):
'''
sends tags to google translate
'''
tagsTrans = dotdict({})
for tag, value in tagsOrig.iteritems():
if isinstance(value, types.StringTypes):
tagsTrans[tag] = translate(value, args.d, args.s)
else:
values = []
for v in value:
val = translate(v, args.d, args.s)
values.append(val)
tagsTrans[tag] = values
return tagsTrans
def separate_properNouns(tags):
'''
separates proper nouns from the tag value strings
'''
properNouns = dotdict({})
pnIndicies = {}
for tag, value in tags.iteritems():
tagged_content = pos_tag(value.split())
properNouns[tag] = [word for word, pos in tagged_content if pos == 'NNP']
return properNouns
def replace_properNouns(tagsTrans, properNounsOrig, properNounsTrans):
'''
replaces translated proper nouns with original proper nouns
'''
tagsTransPNreplaced = dotdict({})
for tag, value in tagsTrans.iteritems():
tagsTransPNreplaced[tag] = value
if properNounsOrig[tag]:
for pno in properNounsOrig[tag]:
indx = properNounsOrig[tag].index(pno)
pnt = properNounsTrans[tag][indx]
something = tagsTransPNreplaced[tag].replace(pnt, pno)
tagsTransPNreplaced[tag] = something
return tagsTransPNreplaced
def make_trans_id3str(file, tagsTrans):
'''
makes a string to write the new file
'''
mtdstr = ''
for tag, value in tagsTrans.iteritems():
mtdstr = mtdstr + ' -metadata ' + tag + '="' + value + '"'
ffstr = 'ffmpeg -i "' + file.inputFullPath.decode('utf8') + '" -c copy ' + mtdstr + ' -write_id3v1 1 -id3v2_version 3 -y "' + file.outputFullPath.decode("utf8") + '"'
#ffstr = 'ffmpeg -i "foobar.mp3" -c copy ' + mtdstr + ' -write_id3v1 1 -id3v2_version 3 -y "' + file.outputFullPath + '"'
#ffstr = 'ffmpeg -i "' + file.inputFullPath + '" -c copy ' + mtdstr + ' -write_id3v1 1 -id3v2_version 3 -y "foobar.mp3"'
#ffstr = 'ffmpeg -i "foobar.mp3" -c copy ' + mtdstr + ' -write_id3v1 1 -id3v2_version 3 -y "foobar.mp3"'
return ffstr
def rename(file, tagsTrans):
'''
renaming logic for the file, based on translating from title tag
'''
if tagsTrans.title:
file.outputFullPath = os.path.join(file.outputDir, tagsTrans.title + file.ext)
file.id3TransObj = os.path.join(file.outputDir, tagsTrans.title + file.ext)
file = output_duplicate_check(file)
return file
def output_duplicate_check(file):
'''
checks if there's a duplicate filepath present in destination, prefixes 01- to file.name
'''
count = 1
fbasename = os.path.basename(file.outputFullPath)
id3basename = os.path.basename(file.id3TransObj)
while os.path.exists(file.outputFullPath) or os.path.exists(file.id3TransObj):
if count < 10:
strcount = "0" + str(count) + "-"
else:
strcount = str(count) + "-"
file.outputFullPath = os.path.join(file.outputDir, strcount + fbasename)
file.id3TransObj = os.path.join(file.outputDir, strcount + id3basename)
count = count + 1
return file
def write_translated_id3file(file, tagsTrans):
'''
writes the translated tags to an ;FFMETADATA1 file
'''
id3file = open(file.id3TransObj,'a')
id3file.write(";FFMETADATA1")
for key, value in tagsTrans.iteritems():
id3file.write(key + "=" + value + "\n")
id3file.close()
def cleanup(args, file, tagsTrans):
'''
handles printing/ deleting/ renaming things
'''
if args.p:
write_translated_id3file(file, tagsTrans)
else:
os.remove(file.id3OrigObj)
def process_single_file(args, file):
'''
run a single file through the whole process
'''
id3Exists = check_id3OrigObj(file)
if id3Exists is None:
print 'id3translate encountered an error exporting ID3 metadata from input file: ' + file.name
return False
if id3Exists is False:
print 'id3translate could not locate any ID3 metadata in file: ' + file.name
print 'please check to make sure ID3 metadata exists'
return False
tagsOrig = id3file_to_dict(file)
tagsTrans = translate_tags(args, tagsOrig)
if args.names is True:
properNounsOrig = separate_properNouns(tagsOrig)
properNounsTrans = translate_tags(args, properNounsOrig)
tagsTrans = replace_properNouns(tagsTrans, properNounsOrig, properNounsTrans)
if args.fnames == 'rename':
file = rename(file, tagsTrans)
ffstr = make_trans_id3str(file, tagsTrans)
ffworked = go(ffstr)
if ffworked is not True:
print ffworked
return False
else:
cleanup(args, file, tagsTrans)
return True
def parse_input(args):
'''
returns a dictionary of file attributes/ paths
'''
file = dotdict({})
file.i = args.i.strip()
file.inputFullPath = os.path.abspath(file.i)
file.inputDir = os.path.dirname(file.inputFullPath)
file.name, file.ext = os.path.splitext(os.path.basename(file.inputFullPath))
if args.o is None:
file.outputDir = file.inputDir
else:
file.outputDir = os.path.abspath(args.o.strip())
if not os.path.exists(file.outputDir):
os.makedirs(file.outputDir)
if args.fnames == 'translate':
f = translate_tags(args, dotdict({'name':file.name}))
file.outputFullPath = os.path.join(file.outputDir, f.name + file.ext)
file.id3OrigObj = os.path.join(file.inputDir, file.name + '-id3.txt')
file.id3TransObj = os.path.join(file.outputDir, f.name + '-id3.txt')
if args.fnames is None or args.fnames == 'rename' or file.outputFullPath == file.inputFullPath:
file.outputFullPath = os.path.join(file.outputDir, file.name + "-trans" + file.ext)
file.id3OrigObj = os.path.join(file.inputDir, file.name + '-id3-orig.txt')
file.id3TransObj = os.path.join(file.outputDir, file.name + '-id3-trans.txt')
file = output_duplicate_check(file)
return file
def init_args():
'''
initialize arguments from the CLI
'''
parser = argparse.ArgumentParser(description="translates ID3 metadata embedded in an audio file")
parser.add_argument('-i', '--input', dest='i', help="the path to the file or folder to be translated")
parser.add_argument('-o', '--output', dest='o', default=None, help="the output folder path for the translated files")
parser.add_argument('-srce', '--source-language', dest='s', default=None, help="the source language (ISO 639-1), if unspecified id3translate will guess")
parser.add_argument('-dest', '--destination-language', dest='d', default='en', help="the destination language (ISO 639-1), default is English (en)")
parser.add_argument('-p', '--print', dest='p', action='store_true', default=False, help="print sidecar ;FFMETADATA1 text files, default is False")
#parser.add_argument('--translate-filenames', dest='fnames', action='store_true', default=False, help="don't translate filenames, default will translate")
parser.add_argument('--ignore-names', dest='names', action='store_true', default=False, help="don't translate words identified as proper nouns, default will translate")
parser.add_argument('--filenames-mode', dest='fnames', choices=['translate','rename', None], const=None, nargs='?', help="filenames mode, translate will translate the input directly, rename will rename output file with translated Title tag. default leaves original names")
args = parser.parse_args()
return args
def main():
'''
do the thing
'''
args = init_args()
if os.path.isfile(args.i):
file = parse_input(args)
'''print file.inputFullPath
print file.inputFullPath.decode('UTF-8')
print file.inputFullPath.encode('utf-8')
foo = raw_input("eh")'''
processWorked = process_single_file(args, file)
if processWorked is not True:
print 'id3translate encountered an error'
print 'id3translate is exiting...'
sys.exit()
elif os.path.isdir(args.i):
for dirs, subdirs, files in os.walk(args.i):
for f in files:
if not f.startswith ('.'):
file = parse_input(dotdict({"i":os.path.join(dirs, f), "o":args.o}))
processWorked = process_single_file(args, file)
if processWorked is not True:
print 'id3translate encountered an error'
print 'id3translate is exiting...'
sys.exit()
foo = raw_input("Press any key to process the next file")
else:
print "id3translate could not locate the file or folder specified"
print "or, the file is of an unknown type"
print "please check the file/ folder path and try again"
print 'id3translate encountered an error'
print 'id3translate is exiting...'
sys.exit()
if __name__ == "__main__":
main()