-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
166 lines (134 loc) · 4.98 KB
/
utils.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
import re
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os
import configparser
from openai_func import transcribe_audio_to_srt, get_tts
from subclip import create_video_with_subtitles
CONFIG_FILE = "settings.ini"
# Check if the config file exists
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
tts_output_folder = ""
def select_file(entry):
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3")])
if file_path:
entry.delete(0, tk.END)
entry.insert(0, file_path)
def select_srt_file(entry):
file_path = filedialog.askopenfilename(filetypes=[("SRT Files", "*.srt")])
if file_path:
entry.delete(0, tk.END)
entry.insert(0, file_path)
def select_folder(label_folder_path):
global tts_output_folder
tts_output_folder = filedialog.askdirectory()
if tts_output_folder:
label_folder_path.config(text=f"Selected folder: {tts_output_folder}")
def convert_audio(entry_file_path):
audio_path = entry_file_path.get()
if not audio_path:
messagebox.showwarning("Warning", "Please select an audio file.")
return
try:
srt_path = transcribe_audio_to_srt(audio_path)
messagebox.showinfo(
"Success", f"Transcription complete! SRT file saved at: {srt_path}"
)
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
def generate_tts(text_input, label_folder_path):
global tts_output_folder
text = text_input.get("1.0", tk.END).strip()
if not text:
messagebox.showwarning("Warning", "Please enter some text.")
return
if not tts_output_folder:
messagebox.showwarning("Warning", "Please select an output folder.")
return
try:
audio_path = get_tts(text, tts_output_folder)
messagebox.showinfo(
"Success", f"TTS generation complete! MP3 file saved at: {audio_path}"
)
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
def batch_generate_tts(text, output_folder, response_format="mp3", add_index=True):
lines = text.strip().split("\n")
voices = ["Alloy", "Echo", "Fable", "Onyx", "Nova", "Shimmer"]
success_count = 0
failed_count = 0
file_count = 0
for line in lines:
if not line.strip():
continue
match = re.match(r"(\w+):\s*(.*)", line)
if match and match.group(1) in voices:
voice = match.group(1)
text_to_convert = match.group(2)
else:
voice = "onyx" # Use a default voice if none is specified
text_to_convert = line
try:
voice = voice.lower()
filename_index = None
file_count += 1
if add_index:
filename_index = str(file_count).zfill(3)
audio_path = get_tts(
text_to_convert,
output_folder,
voice=voice,
response_format=response_format,
filename_index=filename_index,
)
success_count += 1
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
failed_count += 1
messagebox.showinfo(
"Batch TTS Generation Complete",
f"Success: {success_count}, Failed: {failed_count}",
)
def create_subtitled_video(
entry_srt_path, entry_mp3_path, entry_width, entry_height, entry_font_size
):
srt_path = entry_srt_path.get()
mp3_path = entry_mp3_path.get()
width = int(entry_width.get())
height = int(entry_height.get())
font_size = int(entry_font_size.get())
if not srt_path or not mp3_path:
messagebox.showwarning("Warning", "Please select both SRT and MP3 files.")
return
output_path = os.path.splitext(mp3_path)[0] + ".mp4"
try:
create_video_with_subtitles(
srt_path, mp3_path, output_path, width, height, font_size
)
messagebox.showinfo(
"Success", f"Video creation complete! MP4 file saved at: {output_path}"
)
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
def save_settings(entry_api_key):
api_key = entry_api_key.get()
if not config.has_section("Settings"):
config.add_section("Settings")
config.set("Settings", "openai_key", api_key)
with open(CONFIG_FILE, "w") as configfile:
config.write(configfile)
messagebox.showinfo("Success", "OpenAI key saved!")
def load_settings(entry_api_key):
if config.has_option("Settings", "openai_key"):
entry_api_key.insert(0, config.get("Settings", "openai_key"))
def set_resolution(
entry_width, entry_height, entry_font_size, width, height, font_size
):
entry_width.delete(0, tk.END)
entry_width.insert(0, width)
entry_height.delete(0, tk.END)
entry_height.insert(0, height)
entry_font_size.delete(0, tk.END)
entry_font_size.insert(0, font_size)