-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir OPTIVORBIS ogg.py
More file actions
149 lines (114 loc) · 4.52 KB
/
dir OPTIVORBIS ogg.py
File metadata and controls
149 lines (114 loc) · 4.52 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
#!/usr/bin/env python3
"""Batch recompression of .ogg files in selected folder, recursively,
by means of `optivorbis.exe`_
Current script 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, or create a shortcut
(use exact files addresses).
.. warning:: optivorbis.exe location is hardcoded directly,
change it to match you computer.
----
**More Python freeware**: `The Toad's Slimy Mudhole`_
.. _The Toad's Slimy Mudhole: https://dnyarri.github.io/
**Optivorbis download**: `optivorbis.exe`_
.. _optivorbis.exe: https://github.com/OptiVorbis/OptiVorbis/
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024-2026 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '26.4.25.17'
__maintainer__ = 'Ilya Razmanov'
__email__ = 'ilyarazmanov@gmail.com'
__status__ = 'Production'
import subprocess
from pathlib import Path
from sys import argv
from tkinter import Button, Label, Tk, filedialog
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import Progressbar
if len(argv) == 2:
try_open = argv[1]
if Path(try_open).exists():
if Path(try_open).is_file():
try_open = Path(try_open).parent
else:
try_open = Path(try_open).parent
if Path(try_open).exists():
try_open = try_open
else:
try_open = Path.cwd()
else:
try_open = None # Normally makes it start in MRU
# Creating dialog
sortir = Tk()
sortir.title('Recompressing .OGG...')
zanyato = Label(sortir, wraplength=700, text='Starting...', font=('arial', 12), padx=16, pady=10, justify='center')
zanyato.pack()
progressbar = Progressbar(sortir, orient='horizontal')
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),
height=2,
cursor='wait',
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
source_dir = filedialog.askdirectory(title='DIR to optimize OGG files', initialdir=try_open, mustexist=True)
if source_dir == '':
sortir.destroy()
# Creating file list
path = Path(source_dir)
file_list = [p for p in path.rglob('*.ogg', case_sensitive=False)] # list of OGG files in subfolders
file_number = len(file_list)
progressbar['maximum'] = file_number
counter = 0
# Updating dialog
sortir.deiconify()
# Center window horizontally, +100 vertically
sortir.update()
sortir.maxsize(9 * sortir.winfo_screenwidth() // 10, 9 * sortir.winfo_screenheight() // 10)
sortir.geometry(f'+{(sortir.winfo_screenwidth() - sortir.winfo_width()) // 2}+100')
# Updating scrolled text
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 file_list: # cycle through OGG files in subfolders
zanyato.config(text=f' Processing {filename}... ') # Updating UI, showing processed file name
progressbar['value'] = counter
counter += 1
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
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 {source_dir.replace("/", "\\")}\\')
progressbar['value'] = progressbar['maximum']
sortir.after(1000, lambda: progressbar.stop())
butt.config(text='Finished, Dismissed!', bg='spring green', cursor='hand2', state='normal')
sortir.mainloop()