-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir OPTIVORBIS ogg.py
138 lines (107 loc) · 4.02 KB
/
dir OPTIVORBIS ogg.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
#!/usr/bin/env python3
"""
Opens a folder, and recursively feeds all OGG files in it to
optivorbis.exe
for recompression and reducing file size.
optivorbis.exe is available from https://github.com/OptiVorbis/OptiVorbis/
WARNING:
Source files are replaced! No backup, no mercy!
Also supports commandline arguments. Run:
``pythonw.exe "dir OPTIVORBIS ogg.py" "target_name"``
to open in "target_name" dir, or add
``pythonw.exe "dir OPTIVORBIS ogg.py" "%1"``
to "Send to" or right-click or .bat (use exact addresses of pythonw and this file)
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '2024.08.06'
__maintainer__ = 'Ilya Razmanov'
__email__ = 'ilyarazmanov@gmail.com'
__status__ = 'Production'
import subprocess
from pathlib import Path
from sys import argv
from tkinter import BOTH, BOTTOM, TOP, Button, Label, PhotoImage, Tk, X, filedialog
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import Progressbar
"""
run:
``python "dir OPTIVORBIS ogg.py" "target_name"``
to open in "target_name" dir
"""
if len(argv) == 2:
tryopen = argv[1]
if Path(tryopen).exists():
if Path(tryopen).is_file():
tryopen = Path(tryopen).parent
else:
tryopen = Path(tryopen).parent
if Path(tryopen).exists():
tryopen = tryopen
else:
tryopen = Path.cwd()
else:
tryopen = Path.cwd()
# Creating dialog
sortir = Tk()
sortir.title('Recompressing .OGG...')
sortir.geometry('+100+100')
sortir.maxsize(800, 600)
sortir.iconphoto(True, PhotoImage(data=b'P6\n2 2\n255\n\xff\x00\x00\xff\xff\x00\x00\x00\xff\x00\xff\x00'))
zanyato = Label(sortir, wraplength=700, text='Starting...', font=('arial', 12), padx=16, pady=10, justify='center')
zanyato.pack()
progressbar = Progressbar(sortir, orient='horizontal', mode='indeterminate')
progressbar.pack(fill=X, side=TOP, expand=True)
pogovorit = ScrolledText(sortir, height=26, wrap='word', state='normal')
pogovorit.pack(fill=BOTH, expand=True)
butt = Button(
sortir,
text='Busy...',
font=('arial', 14),
cursor='hand2',
justify='center',
state='disabled',
command=sortir.destroy,
)
butt.pack(fill=X, side=BOTTOM, expand=True)
pogovorit.insert('1.0', 'Allons-y!\n')
sortir.withdraw() # Main dialog created and hidden
# Open source dir
sourcedir = filedialog.askdirectory(title='DIR to optimize OGG files', initialdir=tryopen, mustexist=True)
if sourcedir == '':
sortir.destroy()
quit()
path = Path(sourcedir)
# Updating dialog
sortir.deiconify()
zanyato.config(text='Allons-y!')
pogovorit.focus()
sortir.update()
sortir.update_idletasks()
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Process file list
for filename in path.rglob('*.ogg', case_sensitive=False): # cycle through OGG files in subfolders
zanyato.config(text=f' Processing {filename}... ') # Updating UI, showing processed file name
progressbar.start(50)
pogovorit.insert('end -1 chars', f' Starting {filename}... ')
pogovorit.see('end')
sortir.update()
sortir.update_idletasks()
currentfile = Path(filename).resolve() # file to be processed
tempfile = Path(filename.resolve().parent / 'hujwam.ogg') # temp file hujwam.ogg
currentfile.replace(tempfile) # move file to temp
# Note: output in quotes below for paths with spaces
subprocess.run(f'optivorbis.exe --quiet --vendor_string_action empty "{tempfile}" "{filename}"', startupinfo=startupinfo)
# optivorbis.exe writes result from temp back to source location
progressbar.start(50)
pogovorit.insert('end -1 chars', ' Done\n')
sortir.update()
sortir.update_idletasks()
tempfile.unlink(missing_ok=True) # removing temp file
zanyato.config(text=f'Finished {sourcedir}')
progressbar.stop()
butt.config(text='Finished, Dismissed!', state='normal')
sortir.mainloop()