caption translator

 avatar
JO00
python
4 days ago
3.3 kB
1
Indexable
import tkinter as tk
import math

from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from googletrans import Translator



window = tk.Tk()
window.title("Caption Translator")

lang_sel = ["Chinese - Simplified", "Chinese - Traditional", "Bulgarian","Cantonese","French","German","Greek","Italian","Japanese","Korean","Polish","Sicilian","Spanish","Ukraine","Vietnam"]
lang_name = ["zh-cn", "zh-tw", "bg","yue","Frfrench","de","el","it","ja","ko","pl","scn","es","uk","vi"]

import time
start = time.time()

import datetime
now = datetime.datetime.now()

def handle_button_press(event):
    list_files = filedialog.askopenfilename(initialdir = "/", title = "Select a Caption File", filetypes = (("Text files", "*.txt"), ("All files", "*.*")), multiple = False)
    just_file_name=list_files.rstrip(".txt")
    lang_suffix=str(lang_name[cb.current()])
    lang_name_full=str(lang_sel[cb.current()])
    new_file=just_file_name+"_"+lang_name_full+".txt"
    #print("wrote done done done")
    try:
        with open(list_files, "r+") as file:
            lines = file.readlines()  
        original_count=len(lines)+1
        line_count = len(lines)
        line_count=line_count/4 
        line_count=math.ceil(line_count) 
        line_count=line_count+1
        #print(line_count)
        line_count_list = [2,3]

        for i in range(line_count):
            if i > 1:
                mathing_no_add = i * 4-1
                if mathing_no_add < original_count: 
                    line_count_list.append(mathing_no_add)
        now = datetime.datetime.now()
        start = time.time()
        print("Translating file: "+ list_files)
        print("Translating lines now please wait")
        print("This process may take some time to complete a 30 minute recording takes ~4minutes in chinese, other languages may take longer, do not close this window")
        print("You started the process at " + str(now))
        print("Do not close this window or the small dialogue!")

        file.close()
        for i, line in enumerate(lines):
            if i in line_count_list:
                #print(line)
                translator = Translator()
                number_line=str(i)
                translation = translator.translate(line, dest=lang_suffix)
                #print(translation.text)
                lines[i] = translation.text + "\n" 

        print("Writing lines please wait")
        with open(new_file, "w", encoding="utf-8") as file:
            for line in lines:
                file.write(line)
        print("Completed all work now in "+lang_name_full)
        print("Wrote " +str(line_count) +" lines")
        print("File: "+ new_file +" is now in "+lang_name_full)
        end = time.time()
        runtime=round((end - start), 2)
        print("It took " + str(round((runtime/60), 2)) +" minutes")
    except:
        print("Failure to translate file")
text_1=tk.Label(text="Translate English caption to what language? ")
button = tk.Button(text="Select Caption File")
cb = ttk.Combobox(window, values=lang_sel)
button.bind("<Button-1>", handle_button_press)
text_1.pack()
cb.pack()
button.pack()
cb.set(lang_sel[0])



#start loop
window.mainloop()
Editor is loading...
Leave a Comment