-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
295 lines (253 loc) · 10.4 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
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
import banana_dev as banana
import asyncio
import os
import random
import re
import websockets
import json
# import whisperx
import datetime
# from whisperx.utils import write_srt
# from whisperx.utils import write_ass
async def ConvertSubtitleTheme(subtitle_file, new_subtitle_file, comment=None):
with open(subtitle_file, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(new_subtitle_file, "w", encoding="utf-8") as f:
for line in lines:
if line.startswith("Dialogue:"):
if comment is not None:
f.write(
"Dialogue: 0,0:00:0.00,00:00:8.00,Default,,0,0,0,,{\\fad(0,150)\\pos(960,850)}"
+ comment
+ "\n"
)
break
f.write(line)
last_line = None
last_text = None
last_time_end = datetime.timedelta(seconds=0)
last_time_start = datetime.timedelta(seconds=0)
last_raw_line = None
for line in lines:
if line.startswith("Dialogue:"):
match = re.search("}(.*?){", line)
if not match:
continue
text = match.group(1)
if last_line is not None:
time_fields = line.strip().split(",")
epoch_time = datetime.datetime.strptime("0", "%f")
hours, minutes, seconds = map(float, time_fields[1].split(":"))
start_time_ms = datetime.timedelta(
hours=hours, minutes=minutes, seconds=seconds
)
hours, minutes, seconds = map(float, time_fields[2].split(":"))
end_time_ms = datetime.timedelta(
hours=hours, minutes=minutes, seconds=seconds
)
end_time = (epoch_time + end_time_ms).strftime("%H:%M:%S.%f")[:-4]
temp_text = last_text + " " + text
if (
len(temp_text) <= 12
and "." not in last_text
and text in last_raw_line
and "!" not in last_text
and "?" not in last_text
and not last_text.rstrip().endswith(",")
):
last_text_fields = last_line.strip().split(",,")
last_time_fields = last_line.strip().split(",")
last_temp_fields = last_text_fields[0].strip().split(",")
last_raw_line = line
last_text = temp_text
last_line = (
last_temp_fields[0]
+ ","
+ last_temp_fields[1]
+ ","
+ end_time
+ ","
+ last_temp_fields[3]
+ ",,"
+ last_text_fields[1]
+ ",,"
+ last_text
+ "\n"
)
continue
timeAmount = last_time_end - last_time_start
last_time_end = last_time_end + max(
datetime.timedelta(seconds=0),
datetime.timedelta(microseconds=1250000) - timeAmount,
)
last_time_end = min(last_time_end, start_time_ms)
last_time_end_normal = (epoch_time + last_time_end).strftime(
"%H:%M:%S.%f"
)[:-4]
temp_fields = last_line.strip().split(",,")
last_text = (
last_text.replace(".", "")
.replace(",", "")
.replace("!", "")
.upper()
)
timingNew_fields = temp_fields[0].strip().split(",")
margin_fields = temp_fields[1].strip().split(",")
last_line = (
timingNew_fields[0]
+ ","
+ timingNew_fields[1]
+ ","
+ last_time_end_normal
+ ","
+ timingNew_fields[3]
+ ",,"
+ margin_fields[0]
+ ","
+ margin_fields[1]
+ ","
+ margin_fields[2]
+ ",,"
+ "{\\fad(20,20)\pos(960,500)}"
+ last_text
+ "\n"
)
last_time_end = end_time_ms
last_time_start = start_time_ms
f.write(last_line)
last_line = line
last_text = text
last_raw_line = line
async def generateClips(ws, result, clip_length, clipping_start, clipping_end):
id = str(random.randint(1, 10000))
with open(f"subtitles{id}.ass", "w", encoding="utf-8") as file:
write_ass(
result["segments"],
file=file,
resolution="word",
font="Mercadillo Bold",
font_size=80,
underline=False,
xRes="1920",
yRes="1080",
**{"Bold": "1", "Alignment": "5", "Outline": "6", "Shadow": "6"},
)
with open(f"subtitles{id}.srt", "w", encoding="utf-8") as file:
write_srt(result["segments"], file=file)
# Open the SRT file and read its contents
with open(f"subtitles{id}.srt", "r") as f:
lines = f.readlines()
# Initialize variables
clip_start_time = None
clip_start_times = []
clip_end_times = []
wave = 0
epoch_time = datetime.datetime.strptime("0", "%f")
# Iterate over each line in the SRT file
for index, line in enumerate(lines):
if " --> " not in line:
continue
# Strip any leading/trailing whitespace characters from the line
line = line.strip()
good_phrases = [
r"why",
r"how",
r"when",
r"where",
r"who",
r"is it",
r"does it",
r"are there",
r"is there",
r"what",
r"would",
r"can i",
r"i believe",
r"however",
r"i think",
]
phrase = lines[index + 1]
pattern = "|".join(good_phrases)
matchGood = re.search(pattern, phrase.lower())
if clip_start_time is not None:
elapsed_time = (
datetime.datetime.strptime(line[-12:], "%H:%M:%S,%f") - clip_start_time
).total_seconds()
if elapsed_time >= clip_length or index + 4 > len(lines):
clip_end_time = (
datetime.datetime.strptime(line[-12:], "%H:%M:%S,%f") - epoch_time
).total_seconds()
clip_end_times.append(clip_end_time)
clip_start_time = None
# Check if the line contains a question mark
elif index + 1 <= len(lines) and ("?" in phrase or matchGood):
slang_phrases = [
r", you know\?",
r", huh\?",
r", right\?",
r"though\?",
r", really\?",
r"who cares",
r"you know what i\'m trying to say\?",
r"you know what i mean\?",
r"you know what i\'m saying\?",
]
pattern = "|".join(slang_phrases)
match = re.search(pattern, phrase.lower())
# filter slag question marks and short ones
if match or len(lines[index + 1]) < 10:
continue
# If so, extract the start time of the previous line and subtract one second from it
start_time = datetime.datetime.strptime(line[:12], "%H:%M:%S,%f")
clipping_start_datetime = datetime.datetime.strptime(
str(clipping_start), "%H:%M:%S,%f"
)
clipping_end_datetime = datetime.datetime.strptime(
str(clipping_end), "%H:%M:%S,%f"
)
if clipping_start_datetime > start_time:
continue
if clipping_end_datetime <= start_time:
break
clip_start_time = start_time
print(lines[index + 1])
clip_start_times.append((clip_start_time - epoch_time).total_seconds())
# combine clip start and end times into list of tuples
clips = list(zip(clip_start_times, clip_end_times))
result_json = json.dumps(clips)
return result_json
async def handle_banana_dev(model_inputs):
api_key = "c9a5be2f-b57b-4419-8599-d89f4b24bfa3"
model_key_medium = "4a4f2fe0-017a-46bf-b8dc-5c1c6b8458a9"
# Run the model
response = banana.run(api_key, model_key_medium, model_inputs)
# Convert the dictionary to a JSON-formatted string
result_json = json.dumps(response)
return result_json
async def handle_websocket(websocket, path):
async for message in websocket:
print(f"Received message: {message}")
try:
data = json.loads(message)
type = data["type"]
if type == "export_video" or type == "transcribe_audio":
result_json = await handle_banana_dev(data)
await websocket.send(result_json)
elif type == "gen_clips":
clip_length = data["clip_length"]
start_time = data["start_time"]
end_time = data["end_time"]
segments_stuff = await handle_banana_dev(data)
await websocket.send(segments_stuff) # "transcribed audio")
# output = await generateClips(
# websocket, segments_stuff, clip_length, start_time, end_time
# )
# await websocket.send(output)
except json.JSONDecodeError:
print("Invalid JSON received")
await websocket.send("Invalid JSON format")
async def main():
async with websockets.serve(handle_websocket, "0.0.0.0", 443):
print("WebSocket server started")
await asyncio.Future() # Keep the event loop running
asyncio.run(main())