-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHardSubber.py
230 lines (202 loc) · 6.56 KB
/
HardSubber.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
# pyinstaller --noconfirm --onefile --noconsole --add-data 'background.png;.' --add-data 'ico.ico;.' --icon=ico.ico HardSubber.py
from PIL import Image, ImageTk
from typing import Dict
import tkinter
import os
import pysubs2
import ffmpeg
import tkinter.messagebox
from threading import Thread
import sys
import tkinter.ttk as ttk
from ttkbootstrap import Style
import json
from configs import (
get_config,
get_value,
save_options,
SUBS_LANGS_DICT,
RESOLUTIONS_LIST,
)
from fileworks import choice_files
from tktooltip import ToolTip
from helptexts import HELP_DICT
def hardsubber(master: tkinter.Tk) -> None:
config = get_config()
master.nametowidget('start').config(state='disabled')
video_ext, subs_ext, folder, video, subs = choice_files(master)
if video_ext and subs_ext and folder and video and subs:
master.nametowidget('pb').start()
video_file = ffmpeg.input(f'{folder}{video}')
disk = folder.split(':')[0].lower()
folder_path = folder.split(':')[1]
options = {'vcodec': 'libx264'}
output_steams = [video_file['v']]
if subs_ext == '.ass':
param = 'ass'
else:
param = 'subtitles'
options['vf'] = f"{param}='{disk}\:{folder_path}{subs}'"
if get_value('audio'):
options['acodec'] = 'copy'
output_steams.append(video_file['a'])
if config['OPTIONS']['bitrate']:
options['video_bitrate'] = f"{config['OPTIONS']['bitrate']}k"
if config['OPTIONS']['resolution'] != 'ORIGINAL':
options['s'] = f"{config['OPTIONS']['resolution']}"
if get_value('convert_to_mp4'):
video_ext = '.mp4'
output = f'{folder}HRDSBBD_{os.path.splitext(video)[0]}{video_ext}'
output = ffmpeg.output(
*output_steams,
output,
**options,
)
ffmpeg.run(output)
os.remove(f'{folder}{subs}')
master.nametowidget('pb').stop()
master.nametowidget('start').config(state='normal')
master.wm_attributes('-topmost', 1)
def main(checkboxes: Dict, master: tkinter.Tk) -> None:
save_options(checkboxes, master)
hardsubber(master)
def on_save_click(checkboxes: Dict, master: tkinter.Tk) -> None:
thread = Thread(target=main, args=(checkboxes, master))
thread.start()
def resource_path(path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath('.')
return os.path.join(base_path, path)
def get_subs_langs():
if not os.path.exists('subs_langs.json'):
with open('subs_langs.json', 'w+') as json_file:
data = {}
for key, value in SUBS_LANGS_DICT.items():
data[key] = value
json.dump(data, json_file)
with open('subs_langs.json', 'r') as json_file:
data = json.load(json_file)
return data
def menu_setup(menu, key):
if key == 'subs_lang':
LIST = list(get_subs_langs().keys())
else:
LIST = RESOLUTIONS_LIST
try:
if config['OPTIONS'][key] in LIST:
menu.set(config['OPTIONS'][key])
else:
menu.set(LIST[0])
except KeyError:
menu.set(LIST[0])
def set_geometry(master: tkinter.Tk):
width = 300
height = 236
upper = master.winfo_screenheight() // 5
x = (master.winfo_screenwidth() - width) // 2
y = (master.winfo_screenheight() - height) // 2
return f'{width}x{height}+{x}+{y - upper}'
config = get_config()
master = tkinter.Tk(className='HARDSUBBER.main')
master.geometry(set_geometry(master))
master.resizable(False, False)
master.title('HARDSUBBER v1.81')
master.iconbitmap(default=resource_path('ico.ico'))
img = Image.open(resource_path('background.png'))
raw_img = ImageTk.PhotoImage(img)
background_label = tkinter.Label(master, image=raw_img)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
style = Style(theme='pulse')
style.configure('TCheckbutton', background='#ffc0cb')
style.configure('TButton', background='#ffc0cb', foreground='black')
style.configure('TLabel', background='#ffc0cb')
style.configure('Horizontal.TProgressbar', thickness=25)
subs_extract_ = ttk.Label(master, text='Select subtitles to extract:')
subs_extract_.grid(row=0, column=0, sticky=tkinter.W, padx=6, pady=6)
select_resolution = ttk.Label(master, text='Select resolution:')
select_resolution.grid(row=1, column=0, sticky=tkinter.W, padx=6, pady=6)
enter_bitrate = ttk.Label(master, text='Enter bitrate:')
enter_bitrate.grid(row=2, column=0, sticky=tkinter.W, padx=6, pady=6)
subs_menu = ttk.Combobox(
master,
values=list(get_subs_langs().keys()),
name='subs_menu',
state='readonly',
width=12,
)
subs_menu.place(relx=0.5, rely=1.0, anchor="s", x=95, y=-204)
menu_setup(subs_menu, 'subs_lang')
ToolTip(subs_menu, HELP_DICT['subs_menu'], 1)
resolution_menu = ttk.Combobox(
master,
values=RESOLUTIONS_LIST,
name='resolution_menu',
state='readonly',
width=12,
)
resolution_menu.place(relx=0.5, rely=1.0, anchor="s", x=95, y=-173)
menu_setup(resolution_menu, 'resolution')
ToolTip(resolution_menu, HELP_DICT['resolution_menu'], 1)
bitrate_entry = ttk.Entry(
master,
name='bitrate_entry',
width=14,
)
bitrate_entry.place(relx=0.5, rely=1.0, anchor="s", x=95, y=-142)
try:
if config['OPTIONS']['bitrate']:
bitrate_entry.insert(1, config['OPTIONS']['bitrate'])
except KeyError:
pass
ToolTip(bitrate_entry, HELP_DICT['bitrate_entry'], 1)
OPTIONS = [
'audio',
'subs_extract',
'subs_cleaner',
'convert_to_mp4',
]
checkboxes = {}
if 'OPTIONS' not in config:
config['OPTIONS'] = {}
for i, option in enumerate(OPTIONS):
var = tkinter.BooleanVar()
if option in config['OPTIONS']:
var.set(config['OPTIONS'].getboolean(option))
else:
var.set(False)
checkbox = ttk.Checkbutton(
master,
text=option,
variable=var,
padding=6,
)
checkbox.grid(
row=i + 3,
column=0,
sticky=tkinter.W
)
checkboxes[option] = var
ToolTip(checkbox, HELP_DICT[option], 1)
save_button = ttk.Button(
master,
name='start',
text='START',
width=7,
padding=5,
command=lambda: on_save_click(checkboxes, master),
)
save_button.place(relx=0.5, rely=1.0, anchor="center", y=-21, x=116)
ToolTip(save_button, HELP_DICT['start'], 1)
progressbar = ttk.Progressbar(
master,
mode='determinate',
name='pb',
length=224,
style='Horizontal.TProgressbar',
)
progressbar.place(relx=0.5, rely=1.0, anchor="center", y=-21, x=-31)
if __name__ == '__main__':
master.focus_force()
master.mainloop()