-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (140 loc) · 5.39 KB
/
main.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
import os
from pathlib import Path
import sys
import webbrowser
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QIcon
from PyQt6.QtWidgets import (
QFileDialog,
QApplication,
QListWidget,
QListWidgetItem,
QPushButton,
QVBoxLayout,
QWidget
)
app = QApplication(sys.argv)
window = QWidget()
window.resize(650, 250)
window.setWindowIcon(QIcon(str(Path(Path(__file__).parent, 'docs/icon.png'))))
window.setWindowTitle("Subtitle Audio Description Removal")
listWidget = QListWidget()
sub_dic = {}
CONFIRMATION_TEXT = 'AUDIO DESC. REMOVED'
''' SINGLE SUB '''
def button_single_subtitle_clicked():
dialog_add_subtitle = QFileDialog()
dialog_add_subtitle.setWindowTitle("Select an SRT file")
dialog_add_subtitle.setNameFilter("SRT files (*.srt)")
dialog_add_subtitle.exec()
if dialog_add_subtitle.result():
sub_path = dialog_add_subtitle.selectedFiles()[0]
grouped_actions(sub_path, CONFIRMATION_TEXT)
''' MULTIPLE SUBS - ALL SUBS IN THE GIVEN DIRECTORY AND IT`S SUBDIRECTORIES '''
def button_bulk_subtitle_clicked():
dialog_add_directory = QFileDialog()
dialog_add_directory.setFileMode(QFileDialog.FileMode.Directory)
dialog_add_directory.setWindowTitle("Select a directory")
dialog_add_directory.exec()
if dialog_add_directory.result():
for dir_path, dir_names, file_names in os.walk(dialog_add_directory.selectedFiles()[0]):
bulk_subs(dir_path, file_names)
def bulk_subs(dir_path, file_names):
for file in file_names:
if file[-4:] == '.srt':
sub_path = Path(dir_path, file)
grouped_actions(sub_path, CONFIRMATION_TEXT)
''' NAMING / REMOVAL / LISTING '''
def grouped_actions(sub_path, CONFIRMATION_TEXT):
sub_name = Path(sub_path).stem
if CONFIRMATION_TEXT in sub_name:
sub_list_name = f'{sub_name} \u2713' # \u2713 = check mark
sub_new_name = f'{sub_name}.srt'
else:
sub_list_name = f'{CONFIRMATION_TEXT} - {sub_name} \u2713'
sub_new_name = f'{CONFIRMATION_TEXT} - {sub_name}.srt'
try:
sub_ad_removal(sub_path, sub_list_name, sub_new_name)
QListWidgetItem(sub_list_name, listWidget)
except:
sub_list_name = f'{sub_name} - ERROR \u2573'
QListWidgetItem(sub_list_name, listWidget)
''' AUDIO DESCRIPTION REMOVAL '''
def sub_ad_removal(sub_path, sub_list_name, sub_new_name):
parent_directory = Path(sub_path).parent
file = open(sub_path, errors='ignore') # avoiding text encoding errors
newFile = open(Path(parent_directory, sub_new_name),'w')
sub_dic[sub_list_name] = parent_directory
fileList = list(file)
slicedList = []
words_to_remove = []
rows_to_remove = []
spec_char_look_for = '( ) [ ]'.split()
for i in range(len(fileList)):
sliced = fileList[i].split()
slicedList.append(sliced)
'''
Removing the whole row/line containing
audio description like: "(CHAINS RATTLING)"
'''
for row in slicedList:
for word in row:
if ('(' in word and ')' not in word or
'[' in word and ']' not in word):
rows_to_remove.append(row)
for _ in rows_to_remove:
slicedList.remove(_)
rows_to_remove.clear()
'''
Removing one word commentary from line like:
"(SCOFFS) Don't be ridiculous."
'''
for row_index, row in enumerate(slicedList):
for word in row:
if (any(spec_char in spec_char_look_for[:2] for spec_char in word) or
any(spec_char in spec_char_look_for[2:] for spec_char in word)) :
words_to_remove.append([row_index, word])
for _ in words_to_remove:
row_index, word = _[0], _[1]
slicedList[row_index].remove(word)
'''
5
00:00:51,079 --> 00:00:54,449
-
to
5
00:00:51,079 --> 00:00:54,449
'''
if len(slicedList[row_index]) == 1:
word_or_symbol = slicedList[row_index][0]
if len(word_or_symbol) == 1 and not word_or_symbol.isdecimal():
rows_to_remove.append(slicedList[row_index])
for _ in rows_to_remove:
slicedList.remove(_)
''' File compiling '''
for i in slicedList:
newFile.writelines(' '.join(i))
newFile.write('\n')
file.close()
newFile.close()
''' OPEN THE SUB`S DIRECTORY '''
def open_directory():
webbrowser.open(sub_dic[listWidget.currentItem().text()])
listWidget.itemDoubleClicked.connect(open_directory)
''' BUTTONS '''
button_single_subtitle = QPushButton(window, text='Select Subtitle - Single Sub')
button_single_subtitle.setCursor(Qt.CursorShape.PointingHandCursor)
button_single_subtitle.clicked.connect(button_single_subtitle_clicked)
button_single_subtitle.setFont(QFont('Times', 10, 500))
button_bulk_subtitle = QPushButton(window, text='Select Directory - Multiple Subs')
button_bulk_subtitle.setCursor(Qt.CursorShape.PointingHandCursor)
button_bulk_subtitle.clicked.connect(button_bulk_subtitle_clicked)
button_bulk_subtitle.setFont(QFont('Times', 10, 500))
''' LAYOUT '''
window_layout = QVBoxLayout(window)
window_layout.addWidget(button_single_subtitle)
window_layout.addWidget(button_bulk_subtitle)
window_layout.addWidget(listWidget)
window.setLayout(window_layout)
window.show()
sys.exit(app.exec())