1- #!/usr/bin/env python
21# ORIGINAL AUTOSUB IMPORTS
32from __future__ import absolute_import , print_function , unicode_literals
43import argparse
2625import PySimpleGUI as sg
2726import asyncio
2827import httpx
29- import warnings
30- warnings .filterwarnings ("ignore" , category = DeprecationWarning )
31- warnings .filterwarnings ("ignore" , category = RuntimeWarning )
3228
3329sys .tracebacklimit = 0
3430
@@ -583,10 +579,11 @@ def GoogleTranslate(text, src, dst):
583579 return translation
584580 return
585581
582+
586583class SentenceTranslator (object ):
587- def __init__ (self , src , dest , patience = - 1 ):
584+ def __init__ (self , src , dst , patience = - 1 ):
588585 self .src = src
589- self .dest = dest
586+ self .dst = dst
590587 self .patience = patience
591588
592589 def __call__ (self , sentence ):
@@ -595,11 +592,11 @@ def __call__(self, sentence):
595592 if not sentence :
596593 return None
597594
598- translated_sentence = GoogleTranslate (sentence , src = self .src , dst = self .dest )
595+ translated_sentence = GoogleTranslate (sentence , src = self .src , dst = self .dst )
599596
600597 fail_to_translate = translated_sentence [- 1 ] == '\n '
601598 while fail_to_translate and patience :
602- translated_sentence = translator .translate (translated_sentence , src = self .src , dest = self .dest ).text
599+ translated_sentence = translator .translate (translated_sentence , src = self .src , dst = self .dst ).text
603600 if translated_sentence [- 1 ] == '\n ' :
604601 if patience == - 1 :
605602 continue
@@ -609,7 +606,7 @@ def __call__(self, sentence):
609606 return translated_sentence
610607
611608
612- def transcribe (src , dest , filename , subtitle_format , main_window ):
609+ def transcribe (src , dst , filename , subtitle_format , main_window ):
613610 global thread_transcribe , not_transcribing , pool
614611
615612 if not_transcribing : return
@@ -692,7 +689,7 @@ def transcribe(src, dest, filename, subtitle_format, main_window):
692689
693690 if not_transcribing : return
694691
695- if (not is_same_language (src , dest )):
692+ if (not is_same_language (src , dst )):
696693 translated_subtitle_file = subtitle_file [ :- 4 ] + '.translated.' + subtitle_format
697694
698695 if not_transcribing : return
@@ -705,7 +702,7 @@ def transcribe(src, dest, filename, subtitle_format, main_window):
705702
706703 if not_transcribing : return
707704
708- transcript_translator = SentenceTranslator (src = src , dest = dest )
705+ transcript_translator = SentenceTranslator (src = src , dst = dst )
709706 translated_transcriptions = []
710707 for i , translated_transcription in enumerate (pool .imap (transcript_translator , created_transcripts )):
711708 if not_transcribing :
@@ -714,8 +711,8 @@ def transcribe(src, dest, filename, subtitle_format, main_window):
714711 pool = None
715712 return
716713 translated_transcriptions .append (translated_transcription )
717- pBar (i , len (timed_subtitles ), 'Translating from %5s to %5s : ' % (src , dest ), main_window )
718- pBar (len (timed_subtitles ), len (timed_subtitles ), 'Translating from %5s to %5s : ' % (src , dest ), main_window )
714+ pBar (i , len (timed_subtitles ), 'Translating from %5s to %5s : ' % (src , dst ), main_window )
715+ pBar (len (timed_subtitles ), len (timed_subtitles ), 'Translating from %5s to %5s : ' % (src , dst ), main_window )
719716
720717 if not_transcribing : return
721718
@@ -734,7 +731,7 @@ def transcribe(src, dest, filename, subtitle_format, main_window):
734731
735732 main_window ['-ML1-' ].update ('\n \n Done.\n \n ' , append = True )
736733 main_window ['-ML1-' ].update ("Subtitles file created at : {}\n " .format (subtitle_file ), append = True )
737- if (not is_same_language (src , dest )):
734+ if (not is_same_language (src , dst )):
738735 main_window ['-ML1-' ].update ("\n Translated subtitles file created at : {}\n " .format (translated_subtitle_file ), append = True )
739736
740737 if not_transcribing : return
@@ -766,7 +763,6 @@ def popup_yes_no(text, title=None):
766763 return sg .Window (title if title else text , layout , resizable = True ).read (close = True )
767764
768765
769-
770766#--------------------------------------------------------------MAIN FUNCTIONS------------------------------------------------------------#
771767
772768
@@ -778,38 +774,17 @@ def main():
778774 parser .add_argument ('-S' , '--src-language' , help = "Voice language" , default = "en" )
779775 parser .add_argument ('-D' , '--dst-language' , help = "Desired language for translation" , default = "en" )
780776 parser .add_argument ('-F' , '--format' , help = "Destination subtitle format" , default = "srt" )
781- parser .add_argument ('-v' , '--version' , action = 'version' , version = '0.0.5 ' )
777+ parser .add_argument ('-v' , '--version' , action = 'version' , version = '0.0.7 ' )
782778 parser .add_argument ('-lf' , '--list-formats' , help = "List all available subtitle formats" , action = 'store_true' )
783- parser .add_argument ('-ll' , '--list-languages' , help = "List all available source/destination languages" , action = 'store_true' )
779+ parser .add_argument ('-ll' , '--list-languages' , help = "List all available source/translation languages" , action = 'store_true' )
784780
785781 args = parser .parse_args ()
786782
787- if args .src_language not in map_language_of_code .keys ():
788- print ("Source language not supported. Run with --list-languages to see all supported languages." )
789- sys .exit (0 )
790-
791- if args .dst_language not in map_language_of_code .keys ():
792- print ("Destination language not supported. Run with --list-languages to see all supported languages." )
793- sys .exit (0 )
794-
795- if not args .src_language :
796- src = "en"
797-
798- if args .src_language :
799- src = args .src_language
800-
801- if args .dst_language :
802- dest = args .dst_language
803-
804- if not args .dst_language :
805- dst = "en"
806-
807-
808783 if args .list_formats :
809784 print ("List of formats:" )
810785 for subtitle_format in FORMATTERS .keys ():
811786 print ("{format}" .format (format = subtitle_format ))
812- return 0
787+ sys . exit ( 0 )
813788
814789 if args .list_languages :
815790 print ("List of all languages:" )
@@ -820,6 +795,11 @@ def main():
820795
821796#--------------------------------------------------------------MAIN WINDOW--------------------------------------------------------------#
822797
798+ not_transcribing = True
799+ filepath = None
800+ src = None
801+ dst = None
802+ subtitle_format = None
823803
824804 xmax ,ymax = sg .Window .get_screen_size ()
825805 wsizex = int (0.6 * xmax )
@@ -829,53 +809,77 @@ def main():
829809 wx = int ((xmax - wsizex )/ 2 )
830810 wy = int ((ymax - wsizey )/ 2 )
831811
832- if not src == None :
833- combo_src = map_language_of_code [src ]
834- else :
835- combo_src = 'Indonesian'
836- if not dest == None :
837- combo_dest = map_language_of_code [dest ]
838- else :
839- combo_dest = 'English'
840-
841812 #sg.set_options(font=('Courier New', 10))
842813 #sg.set_options(font=('Monospaced', 9))
843814 font = ('Consolas' , 9 )
844815
845816 layout = [[sg .Text ('Voice language :' ),
846- sg .Combo (list (map_code_of_language ), default_value = combo_src , enable_events = True , key = '-SRC-' )],
817+ sg .Combo (list (map_code_of_language ), default_value = 'English' , enable_events = True , key = '-SRC-' )],
847818 [sg .Text ('Translation language :' ),
848- sg .Combo (list (map_code_of_language ), default_value = combo_dest , enable_events = True , key = '-DST-' )],
819+ sg .Combo (list (map_code_of_language ), default_value = 'Indonesian' , enable_events = True , key = '-DST-' )],
849820 [sg .Text ('Subtitle format :' ),
850821 sg .Combo (list (arraylist_subtitle_format ), default_value = 'srt' , enable_events = True , key = '-SUBTITLE-FORMAT-' )],
851- [sg .Text ('Filepath :' ,), sg .InputText (key = '-FILEPATH-' , expand_x = True , expand_y = True ), sg .FileBrowse ()],
822+ [sg .Text ('Filepath :' ,), sg .InputText (key = '-FILEPATH-' , expand_x = True , expand_y = True , enable_events = True ), sg .FileBrowse ()],
852823 [sg .Button ('Start' , expand_x = True , expand_y = True , key = '-START-' )],
853824 [sg .Multiline (size = (mlszx , mlszy ), expand_x = True , expand_y = True , key = '-ML1-' )],
854825 [sg .Button ('Exit' , expand_x = True , expand_y = True )]]
855826
856827 main_window = sg .Window ('PyAutoSRT' , layout , font = font , resizable = True , keep_on_top = True , finalize = True )
857828 main_window ['-SRC-' ].block_focus ()
858829
859- not_transcribing = True
860-
861830 if args .source_path :
862831 if not os .sep in args .source_path :
863- filepath = os .path .join (os .getcwd (),args .source_path )
832+ if os .path .isfile (args .source_path ):
833+ filepath = os .path .join (os .getcwd (),args .source_path )
834+ main_window ['-FILEPATH-' ].update (filepath )
835+ else :
836+ filepath = None
837+ main_window ['-ML1-' ].update ('File path you typed is not exist, please browse it\n \n ' )
864838 else :
865- filepath = args .source_path
866- if os .path .isfile (filepath ):
867- main_window ['-FILEPATH-' ].update (filepath )
839+ if os .path .isfile (args .source_path ):
840+ filepath = args .source_path
841+ main_window ['-FILEPATH-' ].update (filepath )
842+ else :
843+ filepath = None
844+ main_window ['-ML1-' ].update ('File path you typed is not exist, please browse it\n \n ' )
845+
846+
847+ if args .src_language :
848+ if args .src_language not in map_language_of_code .keys ():
849+ main_window ['-ML1-' ].update ('Source language you typed is not supported\n Please select one from combobox\n \n ' , append = True )
850+ elif args .src_language in map_language_of_code .keys ():
851+ src = args .src_language
852+ main_window ['-SRC-' ].update (map_language_of_code [src ])
853+
854+ if not args .src_language :
855+ src = 'en'
856+ main_window ['-SRC-' ].update (map_language_of_code [src ])
857+
858+
859+ if args .dst_language :
860+ if args .dst_language not in map_language_of_code .keys ():
861+ main_window ['-ML1-' ].update ('Translation language you typed is not supported\n Please select one from combobox\n \n ' , append = True )
862+ elif args .dst_language in map_language_of_code .keys ():
863+ dst = args .dst_language
864+ main_window ['-DST-' ].update (map_language_of_code [dst ])
865+
866+ if not args .dst_language :
867+ dst = "id"
868+ main_window ['-DST-' ].update (map_language_of_code [dst ])
869+
870+ if args .format :
871+ if args .format not in FORMATTERS .keys ():
872+ main_window ['-ML1-' ].update ('Subtitle format you typed is not supported\n Please select one from combobox' , append = True )
868873 else :
869- main_window [ '-FILEPATH-' ]. update ( '' )
870- main_window ['-ML1- ' ].update ('File path you typed is not exist, please browse it \n \n ' )
874+ subtitle_format = args . format
875+ main_window ['-SUBTITLE-FORMAT- ' ].update (subtitle_format )
871876
872- if args .format not in FORMATTERS .keys ():
873- main_window ['-ML1-' ].update ("Subtitle format you typed is not supported, select one from combobox" , append = True )
874- else :
875- subtitle_format = args .format
877+ if not args .format :
878+ subtitle_format = 'srt'
876879 main_window ['-SUBTITLE-FORMAT-' ].update (subtitle_format )
877880
878881
882+
879883 if (sys .platform == "win32" ):
880884 main_window .TKroot .attributes ('-topmost' , True )
881885 main_window .TKroot .attributes ('-topmost' , False )
@@ -885,8 +889,10 @@ def main():
885889 main_window .TKroot .attributes ('-topmost' , 0 )
886890
887891
892+
888893#---------------------------------------------------------------MAIN LOOP--------------------------------------------------------------#
889894
895+
890896 while True :
891897 event , values = main_window .read ()
892898
@@ -898,6 +904,10 @@ def main():
898904 else :
899905 break
900906
907+ elif event == '-FILEPATH-' :
908+ if not values ['-FILEPATH-' ]== '' and os .path .isfile (values ['-FILEPATH-' ]):
909+ main_window ['-ML1-' ].update ('' )
910+
901911 elif event == '-SRC-' :
902912 src = map_code_of_language [str (main_window ['-SRC-' ].get ())]
903913 dst = map_code_of_language [str (main_window ['-DST-' ].get ())]
@@ -942,4 +952,3 @@ def main():
942952if __name__ == "__main__" :
943953 multiprocessing .freeze_support ()
944954 main ()
945-
0 commit comments