This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
py3id3.py
executable file
·641 lines (601 loc) · 21.6 KB
/
py3id3.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env python3
__author__ = "Jelmerro"
# See README.md for more details
__license__ = "MIT"
# See LICENSE for more details
__version__ = "0.2.1"
# See Jelmerro/py3id3 on github for updates
import os
import stagger
import tkinter as tk
import webbrowser
from tkinter import messagebox
from tkinter import filedialog
from tkinter import Menu
# The ID3 fields
ID3_FIELDS = (
"title",
"artist",
"date",
"album_artist",
"album",
"track",
"track_total",
"disc",
"disc_total",
"composer",
"genre",
"comment",
"grouping")
# Picture and version are special fields
# They are implemented differently
# A dictionary of Field objects
# Field class is found at the end of this file
FIELDS = {}
# A list of opened files
FILES = []
def close_window_callback(root):
"""
Close the window if that's requested
Ask for a confirmation if files have been opened
"""
if not FILES:
root.destroy()
elif messagebox.askokcancel("Quit", "Do you really wish to quit?"):
root.destroy()
class Application:
"""
Main application class
"""
def __init__(self, root):
# Init
self.frame = tk.Frame(root)
self.frame.pack()
# Menu bar
menubar = Menu(root)
# File menu
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="Open", command=self.browse_files_popup)
file_menu.add_command(label="List", command=self.list_files_popup)
file_menu.add_command(label="About", command=self.about_popup)
file_menu.add_command(
label="Exit",
command=lambda: close_window_callback(root))
menubar.add_cascade(label="File", menu=file_menu)
# Convert menu
write_menu = Menu(menubar, tearoff=0)
write_menu.add_command(
label="As v2.2",
command=lambda: self.write_tags(2))
write_menu.add_command(
label="As v2.3",
command=lambda: self.write_tags(3))
write_menu.add_command(
label="As v2.4",
command=lambda: self.write_tags(4))
write_menu.add_command(
label="As original",
command=lambda: self.write_tags(0))
menubar.add_cascade(label="Write", menu=write_menu)
root.config(menu=menubar)
# Add the fields to the frame
self.create_fields()
root.bind("<Control-q>", lambda e: close_window_callback(root))
root.bind("<Control-Q>", lambda e: close_window_callback(root))
def browse_files_popup(self):
"""
Show a file browser and expand the list of files
Duplicates won't be added
Menu: File > Open
"""
self.files_opened = []
# Limit the selection to mp3 only
self.files_opened = filedialog.askopenfilenames(
filetypes=(("Mp3 files", "*.mp3"),))
# If any files were opened, add them to files
if self.files_opened:
for file in self.files_opened:
# Only add new files, to prevent duplicates
if file not in FILES:
FILES.append(file)
# Update the list of old values
self.update_fields()
def list_files_popup(self):
"""
Show a popup with the list of opened files
Menu: File > List
"""
file_list = ""
for file in FILES:
file_list += "{}\n".format(file)
if file_list:
Popup(self.frame, "Files", file_list)
else:
Popup(self.frame, "Files", "No files have been opened yet\n")
def about_popup(self):
"""
Show a popup with details about the program
Menu: File > About
"""
Popup(
self.frame,
"About",
"Py3ID3 {}\n"
"Created by Jelmerro\n"
"MIT License\n".format(__version__),
15,
"Github",
"https://github.com/Jelmerro/py3id3",
13)
def write_tags(self, requested_version):
"""
Read the tags and write them to each file
A list of failed files will be shown afterwards
Menu: All options in "Write"
"""
skipped = {}
for file in FILES:
tag = ""
try:
tag = stagger.read_tag(file)
except FileNotFoundError:
skipped[file] = "missing file: {}".format(file)
except stagger.errors.NoTagError:
skipped[file] = "missing id3 tag: {}".format(file)
if tag:
version = self.file_version(tag.version, requested_version)
error = self.write_tag_to_file(tag, file, version)
if error:
skipped[file] = error
self.show_results(skipped)
def show_results(self, skipped):
"""
Show a popup with the list of failed files (if any)
Also shows the number of processed files
"""
success_number = len(FILES) - len(skipped)
if FILES:
if success_number == 0:
message = "All {} files failed:\n".format(len(FILES))
for file, error in skipped.items():
message += "{} - {}\n".format(file, error)
elif skipped:
message = "{} of the {} succeeded, but some failed:\n".format(
success_number,
len(FILES))
for file, error in skipped.items():
message += "{} - {}\n".format(file, error)
else:
message = "Replaced tags for all {} files " \
"with success\n".format(len(FILES))
else:
message = "No files have been opened yet\n"
Popup(self.frame, "Done", message)
# Update the fields afterwards
self.update_fields()
def write_tag_to_file(self, old_tag, file, version):
"""
Write the fields to the file as part of a tag
Returns the error message (if any)
"""
# Create a tag with the correct version
if version not in [0, 2, 3, 4]:
return "Invalid version {}".format(version)
if version == 0:
new_tag = old_tag
if version == 2:
new_tag = stagger.tags.Tag22()
if version == 3:
new_tag = stagger.tags.Tag23()
if version == 4:
new_tag = stagger.tags.Tag24()
# Set the regular fields
for field in ID3_FIELDS:
if field not in ["track", "track_total"]:
if FIELDS[field].checked():
try:
setattr(
new_tag,
field,
FIELDS[field].text())
except ValueError or KeyError:
return "Invalid tag error"
else:
try:
setattr(
new_tag,
field,
getattr(old_tag, field))
except ValueError or KeyError:
return "Invalid tag error"
# Set the numbering if enabled
if FIELDS["track"].checked():
track_total = len(FILES)
track = str(FILES.index(file) + 1)
if FIELDS["track_total"].checked():
track = track.zfill(len(str(track_total)))
else:
track_total = getattr(old_tag, "track_total")
track = getattr(old_tag, "track")
try:
setattr(
new_tag,
"track_total",
track_total)
except ValueError or KeyError:
return "Invalid tag error"
try:
setattr(
new_tag,
"track",
track)
except ValueError or KeyError:
return "Invalid tag error"
# Picture
if self.picture_enabled_var.get():
if self.picture_var.get():
new_tag.picture = self.picture_var.get()
else:
new_tag.picture = ""
else:
picture_data = ""
if "PIC" in old_tag:
picture_data = old_tag["PIC"][0].data
elif "APIC" in old_tag:
picture_data = old_tag["APIC"][0].data
if picture_data:
# This is required, for details see:
# https://github.com/lorentey/stagger/issues/48
with open("temp_image_file_for_stagger.png", "wb") as f:
f.write(picture_data)
new_tag.picture = "temp_image_file_for_stagger.png"
else:
new_tag.picture = ""
try:
os.remove("temp_image_file_for_stagger.png")
except OSError:
pass
# Write to file
try:
new_tag.write(file)
except FileNotFoundError:
return "Write error"
return ""
def file_version(self, tag_version, requested_version):
"""
Calculates the required version for the new tag
"""
# If the versions match and the obscure tags should be kept, return 0
if tag_version == requested_version:
if self.keep_obscure.get():
return 0
# If the requested version is not set, keep the original version
if requested_version == 0:
return tag_version
# Else set the version to the requested version
return requested_version
def create_fields(self):
"""
Creates a label, checkbox and textfields
Every field in the tag will have them
"""
# regular fields
row = 1
for field in ID3_FIELDS:
FIELDS[field] = Field(field, row, self.frame)
row += 1
# special fields
version_frame = tk.Frame(self.frame)
version_frame.grid(column=1, row=row)
# version field
self.version_label = tk.Label(self.frame, text="ID3 Version")
self.version_label.grid(column=0, row=row)
self.version_var = tk.StringVar()
self.version = tk.Entry(
version_frame,
width=50,
state=tk.DISABLED,
textvariable=self.version_var)
self.version.grid(column=0, row=0)
# version settings
keep_obscure_frame = tk.Frame(version_frame)
keep_obscure_frame.grid(column=0, row=1)
self.keep_obscure = tk.IntVar()
check = tk.Checkbutton(
keep_obscure_frame,
variable=self.keep_obscure)
check.grid(column=0, row=0)
label = tk.Label(
keep_obscure_frame,
text="Keep obscure tags for files with unchanged ID3 versions")
label.grid(column=1, row=0)
# picture settings
self.picture_enabled_var = tk.IntVar()
self.picture_enabled = tk.Checkbutton(
self.frame,
variable=self.picture_enabled_var)
self.picture_enabled.grid(column=2, row=row)
self.picture_enabled.bind("<Button-1>", self.update_picture)
self.picture_enabled.bind("<Return>", self.update_picture)
self.picture_enabled.bind("<space>", self.update_picture)
picture_settings_frame = tk.Frame(self.frame)
picture_settings_frame.grid(column=3, row=row)
browse_button = tk.Button(picture_settings_frame, text="Browse")
browse_button.grid(column=0, row=0)
browse_button.bind("<Button-1>", self.open_picture)
browse_button.bind("<Return>", self.open_picture)
browse_button.bind("<space>", self.open_picture)
self.picture_var = tk.StringVar()
self.picture = tk.Entry(
picture_settings_frame,
width=30,
state=tk.DISABLED,
textvariable=self.picture_var)
self.picture.grid(column=1, row=0)
self.picture_status_var = tk.StringVar()
self.picture_status_var.set("All files will keep the current picture")
picture_status = tk.Label(
picture_settings_frame,
textvariable=self.picture_status_var)
picture_status.grid(column=1, row=1)
clear_button = tk.Button(picture_settings_frame, text="Clear")
clear_button.grid(column=2, row=0)
clear_button.bind("<Button-1>", self.clear_picture)
clear_button.bind("<Return>", self.clear_picture)
clear_button.bind("<space>", self.clear_picture)
def clear_picture(self, event=None):
"""
Clears the picture field
"""
self.picture_var.set("")
self.update_picture()
def open_picture(self, event=None):
if event:
event.widget.after_idle(self.open_picture_callback)
else:
self.open_picture_callback()
def open_picture_callback(self, e=None):
"""
Show a file browser and set the new picture
"""
# Limit the selection to png and jpg only
self.file_opened = filedialog.askopenfilename(
filetypes=(("PNG files", "*.png"),))
# If any files were opened, add them to files
if self.file_opened:
self.picture_var.set(self.file_opened)
self.update_picture()
def update_picture(self, event=None):
if event:
event.widget.after_idle(self.update_picture_callback)
else:
self.update_picture_callback()
def update_picture_callback(self, e=None):
"""
Update the picture status label to match new the settings
"""
if self.picture_enabled_var.get():
if self.picture_var.get():
self.picture_status_var.set(
"All files will have this picture added")
else:
self.picture_status_var.set(
"All files will have the picture removed")
else:
self.picture_status_var.set(
"All files will keep the current picture")
def update_fields(self):
"""
Update the original values for each field
"""
separator = ""
last_value = {}
all_the_same = {"version": True}
self.version_var.set("")
for field in ID3_FIELDS:
FIELDS[field].clear_original()
all_the_same[field] = True
remove_list = []
for file in FILES:
tag = ""
try:
tag = stagger.read_tag(file)
except FileNotFoundError:
Popup(
self.frame,
"Warning",
"missing file: {}".format(file))
remove_list.append(file)
except stagger.errors.NoTagError:
Popup(
self.frame,
"Warning",
"missing id3 tag: {}".format(file))
remove_list.append(file)
if tag:
for field in ID3_FIELDS:
value = getattr(tag, field)
FIELDS[field].set_original("{}{}{}".format(
FIELDS[field].original(),
separator,
value))
if field in last_value:
if last_value[field] != value:
all_the_same[field] = False
last_value[field] = value
value = getattr(tag, "version")
self.version_var.set("{}{}{}".format(
self.version_var.get(),
separator,
value))
if "version" in last_value:
if last_value["version"] != value:
all_the_same["version"] = False
last_value["version"] = value
separator = ";"
for file in remove_list:
FILES.remove(file)
if FILES:
for field in ID3_FIELDS:
if all_the_same[field]:
FIELDS[field].set_original(last_value[field])
FIELDS[field].update_output()
if all_the_same["version"]:
self.version_var.set(last_value["version"])
class Popup:
"""
Popup class creates a toplevel with labels
It's not resizable, grabs the focus and stays on top
Optionally adds a link to it
"""
def __init__(self,
frame,
title,
message,
size=12,
link_title=None,
link=None,
link_size=None):
popup = tk.Toplevel(frame, padx="40", pady="40")
# Add a link if provided
if link_title:
label = tk.Label(popup, text=message, font="-size {}".format(size))
label.grid(row=0)
link_label = tk.Label(
popup,
text=link_title,
fg="blue",
cursor="hand2",
font="-size {}".format(link_size))
link_label.bind(
"<Button-1>",
lambda e: webbrowser.open_new(link))
link_label.grid(row=1)
else:
tk.Label(popup, text=message, font="-size {}".format(size)).pack()
popup.grab_set()
popup.title(title)
popup.resizable(False, False)
popup.wm_attributes("-topmost", True)
popup.bind("<Escape>", lambda e: popup.destroy())
class Field(tk.Frame):
"""
Field class consists of a label, checkbox and three entry fields
A field will occupy a single row in the application
For every id3 field, a Field object will be created
(except for the picture and version)
"""
def __init__(self, name, row, master):
self.name = name
self.master = master
super(Field, self).__init__(master=master)
# label - title Label
self.label = tk.Label(self.master, text=self.title(name))
self.label.grid(column=0, row=row)
# original - old Entry
self.old_var = tk.StringVar()
self.old = tk.Entry(
self.master,
width=50,
state=tk.DISABLED,
textvariable=self.old_var)
self.old.grid(column=1, row=row)
# checkbox - enabled Checkbutton
self.checkbox_var = tk.IntVar()
self.checkbox = tk.Checkbutton(
self.master,
variable=self.checkbox_var)
self.checkbox.grid(column=2, row=row)
self.checkbox.bind("<Button-1>", self.update_output)
self.checkbox.bind("<Return>", self.update_output)
self.checkbox.bind("<space>", self.update_output)
# entry - input Entry
if name not in ["track", "track_total"]:
self.input_var = tk.StringVar()
self.input = tk.Entry(
self.master,
width=50,
textvariable=self.input_var)
self.input.grid(column=3, row=row)
self.input.bind("<Any-KeyPress>", self.update_output)
elif name == "track":
self.input = tk.Label(
self.master,
text="Automatically number the tracks")
self.input.grid(column=3, row=row)
elif name == "track_total":
self.input = tk.Label(
self.master,
text="Pad zeros to match the total number of tracks")
self.input.grid(column=3, row=row)
# output - new Entry
self.output_var = tk.StringVar()
self.output = tk.Entry(
self.master,
width=50,
state=tk.DISABLED,
textvariable=self.output_var)
self.output.grid(column=4, row=row)
def title(self, text):
return text.title().replace("_", " ")
def set_original(self, text):
self.old_var.set(text)
def clear_original(self):
self.old_var.set("")
def original(self):
return self.old_var.get()
def checked(self):
return bool(self.checkbox_var.get())
def text(self):
return self.input_var.get()
def set_output(self, text):
self.output_var.set(text)
def update_output(self, event=None):
if event:
event.widget.after_idle(self.update_output_callback)
else:
self.update_output_callback()
def update_output_callback(self):
"""
Update the output fields to match the new settings
"""
if self.name in ["track", "track_total"]:
if FIELDS["track"].checked():
padding = FIELDS["track_total"].checked()
track, track_total = self.get_numbering(padding)
FIELDS["track"].set_output(track)
FIELDS["track_total"].set_output(track_total)
else:
FIELDS["track"].set_output(FIELDS["track"].original())
FIELDS["track_total"].set_output(
FIELDS["track_total"].original())
else:
if not self.checked():
self.set_output(self.original())
else:
self.set_output(self.text())
def get_numbering(self, padding):
"""
Returns a formatted string version of the numbering
"""
if not FILES:
return "", ""
track = ""
track_total = str(len(FILES))
id = 1
for file in FILES:
if padding:
track += "{};".format(str(id).zfill(len(track_total)))
else:
track += "{};".format(id)
id += 1
track = track[:-1]
return track, track_total
if __name__ == "__main__":
# Create a root Tk object and start the application with it
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", lambda: close_window_callback(root))
root.resizable(False, False)
root.title("Py3ID3")
Application(root)
root.mainloop()