-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsubtitles.py
More file actions
78 lines (67 loc) · 2.65 KB
/
subtitles.py
File metadata and controls
78 lines (67 loc) · 2.65 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
"""
Minimal function to transform Whisper's bracketed output to SRT format
"""
import os
import re
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
def whisper_to_srt(whisper_output):
"""
Convert Whisper output to SRT format with minimal changes.
"""
# Split into lines and process each line
lines = whisper_output.strip().split('\n')
srt_parts = []
counter = 1
for line in lines:
# Match the timestamp pattern
match = re.match(r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)', line.strip())
if match:
start_time, end_time, text = match.groups()
# Convert dots to commas
start_time = start_time.replace('.', ',')
end_time = end_time.replace('.', ',')
# Format as SRT entry
srt_parts.append(f"{counter}")
srt_parts.append(f"{start_time} --> {end_time}")
srt_parts.append(f"{text.strip()}")
srt_parts.append("") # Empty line
counter += 1
return "\n".join(srt_parts)
def save_whisper_as_srt(whisper_output, original_file_path, parent_window=None, status_callback=None):
"""Save Whisper output as SRT with minimal conversion."""
if not whisper_output or not original_file_path:
if status_callback:
status_callback("No transcription data available", "red")
return False
# Prepare file dialog
filetypes = [('SubRip Subtitle', '*.srt')]
initial_filename = os.path.splitext(os.path.basename(original_file_path))[0] + '.srt'
initial_dir = os.path.dirname(original_file_path)
save_path = filedialog.asksaveasfilename(
title="Save SRT Subtitle File",
defaultextension=".srt",
initialfile=initial_filename,
initialdir=initial_dir,
filetypes=filetypes,
parent=parent_window
)
if save_path:
try:
srt_content = whisper_to_srt(whisper_output)
with open(save_path, 'w', encoding='utf-8') as srt_file:
srt_file.write(srt_content)
if status_callback:
status_callback(f"SRT file saved to {save_path}", "green")
return True
except Exception as e:
error_msg = f"Error saving SRT file: {str(e)}"
if status_callback:
status_callback(error_msg, "red")
else:
messagebox.showerror("SRT Saving Error", error_msg)
return False
else:
if status_callback:
status_callback("SRT file saving cancelled", "blue")
return False