Untitled
unknown
plain_text
2 years ago
12 kB
6
Indexable
import tkinter as tk
import os
from tkinter import font as tkfont
from tkinter import filedialog, messagebox
from concurrent.futures import ThreadPoolExecutor
from deep_translator import GoogleTranslator
from PIL import Image, ImageTk
class SRTTranslatorGUI:
def __init__(self, root):
self.root = root
# icon_path = "./translate.png" # Replace with the path to your icon image file
# Create a PhotoImage object from the icon image file
# icon_image = tk.PhotoImage(file=icon_path)
# Set the icon for the tkinter window
# root.iconphoto(True, icon_image)
self.root.title("SRT Translator")
self.file_path = None
self.root.configure(bg='black')
# Tạo và thiết lập giao diện người dùng
self.setup_ui()
def setup_ui(self):
# Thiết lập màu sắc cyberpunk
fontName = 'Arial';
bg_color = "#000000" # Đen cho nền
text_color = "#00FF00" # Xanh lơ cho text
button_color = "#FFA500" # Cam cho nút
font_style = tkfont.Font(family=fontName, weight="bold", size=16)
# Tạo khung cho nội dung SRT
self.srt_frame = tk.Frame(self.root, bg=bg_color)
self.srt_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
# Tiêu đề và thanh cuộn cho nội dung SRT
self.srt_text_label = tk.Label(self.srt_frame, text="File SRT chưa dịch:", bg=bg_color, fg=text_color, font=font_style)
self.srt_text_label.pack()
self.srt_scrollbar = tk.Scrollbar(self.srt_frame)
self.srt_text = tk.Text(self.srt_frame, height=10, width=50, wrap=tk.WORD, yscrollcommand=self.srt_scrollbar.set)
self.srt_text.config(bg=bg_color, fg=text_color, font=font_style)
self.srt_scrollbar.config(command=self.srt_text.yview)
self.srt_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.srt_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# self.button_frame = tk.Frame(self.root)
# self.button_frame.pack(padx=10, pady=(0, 10), fill=tk.X, expand=False)
self.navigation_frame = tk.Frame(self.root)
self.navigation_frame.pack(padx=20, pady=20, expand=True)
# Nút tải lên file SRT
self.upload_button = tk.Button(self.root, text="Tải lên File SRT", command=self.upload_srt, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.upload_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.upload_button.pack()
# Lên đầu và xuống cuối
self.go_to_beginning_button = tk.Button(self.navigation_frame, text="Đầu file SRT", command=self.go_to_beginning_srt, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.go_to_beginning_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.go_to_beginning_button.pack(side=tk.LEFT)
self.go_to_end_button = tk.Button(self.navigation_frame, text="Cuối file SRT", command=self.go_to_end_srt, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.go_to_end_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.go_to_end_button.pack(side=tk.LEFT)
self.go_to_translated_beginning_button = tk.Button(self.navigation_frame, text="Đầu file SRT đã Dịch", command=self.go_to_translated_beginning, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.go_to_translated_beginning_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.go_to_translated_beginning_button.pack(side=tk.LEFT)
self.go_to_translated_end_button = tk.Button(self.navigation_frame, text="Cuối file SRT đã Dịch", command=self.go_to_translated_end, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.go_to_translated_end_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.go_to_translated_end_button.pack(side=tk.LEFT)
# Nút dịch
self.translate_button = tk.Button(self.navigation_frame, text="Dịch", command=self.translate_srt, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.translate_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.translate_button.pack(side=tk.LEFT)
# Nút lưu
self.save_button = tk.Button(self.root, text=f"Lưu File SRT đã dịch", command=self.save_translated_srt, bg=bg_color, fg=text_color, font=font_style, borderwidth=6)
self.save_button.config(
activebackground="#0d0d0d", # Set the background color when the button is active
activeforeground="#0bba0b", # Set the text color when the button is active
)
self.save_button.pack()
# Tiến độ dịch
self.progress_bar = tk.Label(self.root, text="", width=50, bg=bg_color)
self.progress_bar.pack(pady=(0, 10))
# Dịch SRT và lưu kết quả
self.translated_frame = tk.Frame(self.root, bg=bg_color)
self.translated_frame.pack(padx=10, fill=tk.BOTH, expand=True)
self.translated_text_label = tk.Label(self.translated_frame, text="File SRT đã dịch:", bg=bg_color, fg=text_color, font=font_style)
self.translated_text_label.pack()
self.translated_text = tk.Text(self.translated_frame, height=10, width=50, wrap=tk.WORD)
self.translated_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.translated_scrollbar = tk.Scrollbar(self.translated_frame)
self.translated_text.config(bg=bg_color, fg=text_color, font=font_style)
self.translated_text.config(yscrollcommand=self.translated_scrollbar.set)
self.translated_scrollbar.config(command=self.translated_text.yview)
self.translated_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
def upload_srt(self):
self.file_path = filedialog.askopenfilename(filetypes=[("SRT Files", "*.srt")])
if self.file_path:
with open(self.file_path, "r", encoding="utf-8") as file:
srt_content = file.read()
self.srt_text.delete(1.0, tk.END)
self.srt_text.insert(tk.END, srt_content)
def translate_srt(self):
if self.file_path:
srt_content = self.srt_text.get(1.0, tk.END).strip()
if srt_content:
chunks = self.split_text(srt_content)
total_chunks = len(chunks)
def translate_chunk(chunk):
try:
return GoogleTranslator(source='auto', target='vi').translate(chunk)
except Exception as e:
return f"[Lỗi dịch]: {str(e)}"
translated_chunks = []
with ThreadPoolExecutor() as executor:
for index, translated_chunk in enumerate(executor.map(translate_chunk, chunks), start=1):
text_color = 'red'
custom_font = ("Helvetica", 14, 'bold')
bg = "black"
translated_chunks.append(translated_chunk)
completion_percentage = (index/ total_chunks) * 100
self.progress_bar.config(text=f"Đã dịch được {completion_percentage:.2f}%", fg=text_color, font=custom_font, bg=bg)
self.progress_bar.update()
translated_content = "\n".join(translated_chunks)
# Xóa đoạn mã không cần thiết
translated_content = translated_content.replace('<font color="#FFFFFF" size="16">', '')
translated_content = translated_content.replace('</font>', '')
self.translated_text.delete(1.0, tk.END)
self.translated_text.insert(tk.END, translated_content)
self.progress_bar.config(text="Đã dịch xong")
else:
messagebox.showwarning("Cảnh báo", "Vui lòng tải lên file SRT để dịch.")
def split_text(self, text, chunk_size=5000):
lines = text.split('\n')
chunks = []
current_chunk = lines[0]+ '\n' # Bắt đầu với dòng đầu tiên
current_chunk_length = len(current_chunk)
for line in lines[1:]:
if current_chunk_length + len(line) + 1 <= chunk_size:
current_chunk += line + '\n'
current_chunk_length += len(line) + 1
else:
# Tìm dấu câu gần nhất trước chunk_size để chia đoạn văn
last_punctuation = max(current_chunk.rfind('.'), current_chunk.rfind('!'), current_chunk.rfind('?'))
if last_punctuation > 0:
chunks.append(current_chunk[:last_punctuation + 1])
current_chunk = current_chunk[last_punctuation + 1:]
current_chunk_length = len(current_chunk)
else:
# Nếu không có dấu câu trong chunk_size, chia tại vị trí cuối cùng trong chunk_size
last_space = current_chunk.rfind(' ', 0, chunk_size)
if last_space > 0:
chunks.append(current_chunk[:last_space + 1])
current_chunk = current_chunk[last_space + 1:]
current_chunk_length = len(current_chunk)
else:
# Nếu không tìm thấy khoảng trắng, chia tại chunk_size
chunks.append(current_chunk)
current_chunk = line + '\n'
current_chunk_length = len(current_chunk)
chunks.append(current_chunk)
return chunks
def go_to_beginning_srt(self):
self.srt_text.yview(tk.MOVETO, 0.0)
def go_to_end_srt(self):
self.srt_text.yview(tk.MOVETO, 1.0)
def go_to_translated_beginning(self):
self.translated_text.yview(tk.MOVETO, 0.0)
def go_to_translated_end(self):
self.translated_text.yview(tk.MOVETO, 1.0)
def save_translated_srt(self):
if self.translated_text.get(1.0, tk.END).strip():
if self.file_path:
file_name, file_extension = os.path.splitext(os.path.basename(self.file_path))
save_path = filedialog.asksaveasfilename(defaultextension=".srt", initialfile=f"{file_name}_translated", filetypes=[("SRT Files", "*.srt")])
if save_path:
with open(save_path, "w", encoding="utf-8") as file:
translated_content = self.translated_text.get(1.0, tk.END)
file.write(translated_content)
messagebox.showinfo("Thông báo", "File SRT đã được lưu thành công.")
else:
messagebox.showwarning("Cảnh báo", "Vui lòng chọn vị trí để lưu file SRT dịch.")
else:
messagebox.showwarning("Cảnh báo", "Vui lòng tải lên file SRT trước khi lưu.")
else:
messagebox.showwarning("Cảnh báo", "Không có nội dung dịch để lưu.")
root = tk.Tk()
app = SRTTranslatorGUI(root)
root.mainloop()
Editor is loading...