Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
botbahlul authored Jan 31, 2023
1 parent 4abe1b4 commit c91433e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 18 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/android/autosrt/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ private void transcribe() {

// ALTERNATIVE 1 : run a single function transcribe() of autosrt.py
// WE NEED TO USE SOME time.sleep() FUNCTION ON PYTHON SCRIPT TO AVOID CRASHED
if (!canceled && mediaURI != null && sourceCopy != null) {
/*if (!canceled && mediaURI != null && sourceCopy != null) {
if (canceled) {
String m = "Process has been canceled";
textview_debug.setText(m);
Expand All @@ -948,10 +948,10 @@ private void transcribe() {
}
}
}
}
}*/

// ALTERNATIVE 2 : run split functions of transcibe() in autosrt.py
/*if (canceled) {
if (canceled) {
String m = "Process has been canceled";
textview_debug.setText(m);
if (runpy != null) {
Expand Down Expand Up @@ -987,7 +987,7 @@ private void transcribe() {
runpy = null;
}
}
}*/
}
/*if (canceled) {
String m = "Process has been canceled";
textview_debug.setText(m);
Expand Down
99 changes: 85 additions & 14 deletions app/src/main/python/autosrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def __call__(self, data):
return



def extract_audio(filename, channels=1, rate=16000):
temp = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
if not os.path.isfile(filename):
Expand Down Expand Up @@ -555,8 +556,56 @@ def translate(entries, src, dest, patience, verbose):
yield number_in_sequence, timecode, translated_subtitles, count_failure, count_entries



class SubtitleTranslator(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def __call__(self, entries):
translator = Translator()
translated_subtitles = []
number_in_sequence, timecode, subtitles = entries

#translated_subtitles = translator.translate(subtitles, src=self.src, dest=self.dest).text

for i, subtitle in enumerate(subtitles, 1):
# handle the special case: empty string.
if not subtitle:
translated_subtitles.append(subtitle)
translated_subtitle = translator.translate(subtitle, src=self.src, dest=self.dest).text
translated_subtitle = translator.translate(translated_subtitle, src=self.src, dest=self.dest).text
#if translated_subtitle[-1] == '\n':
#fail_to_translate = False
#if subtitles: translated_subtitles = translator.translate(subtitles, src=self.src, dest=self.dest).text
#translated_subtitle = translator.translate(translated_subtitle, src=self.src, dest=self.dest).text
translated_subtitles.append(translated_subtitle + '\n')

#translated_entries = (number_in_sequence, timecode, [translated_subtitles])

#return translated_entries
return number_in_sequence, timecode, translated_subtitles
#yield number_in_sequence, timecode, translated_subtitles, count_failure, count_entries


class TranscriptionTranslator(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def __call__(self, transcription):
try:
translated_transcription = Translator().translate(transcription, src=self.src, dest=self.dest).text
return translated_transcription

except KeyboardInterrupt:
return



def transcribe(src, dest, filename, activity, textView_debug):
wav_filename, audio_rate = extract_audio(filename)
pool = multiprocessing.pool.ThreadPool(10)

if not os.path.isfile(cancel_file):

Expand Down Expand Up @@ -618,7 +667,6 @@ def run(self):
check_cancel_file()

if not os.path.isfile(cancel_file):
pool = multiprocessing.pool.ThreadPool(10)
converter = FLACConverter(source_path=wav_filename)
recognizer = SpeechRecognizer(language=src, rate=audio_rate, api_key=GOOGLE_SPEECH_API_KEY)
transcriptions = []
Expand Down Expand Up @@ -693,21 +741,21 @@ def run(self):
#widgets = [prompt, Percentage(), ' ', Bar(), ' ', ETA()]
#pbar = ProgressBar(widgets=widgets, maxval=len(transcriptions)).start()

e=0
subtitle_translator = SubtitleTranslator(src=src, dest=dest)
translated_entries = []
for i, translated_entry in enumerate(pool.imap(subtitle_translator, entries)):
check_cancel_file()
translated_entries.append(translated_entry)
pBar(i, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)
pBar(total_entries, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)

with open(translated_srt_file, 'w', encoding='utf-8') as f:
for number_in_sequence, timecode, subtitles, count_failure, count_entries in translate(entries, src=src, dest=dest, patience="", verbose=""):
check_cancel_file()
for number_in_sequence, timecode, translated_subtitles in translated_entries:
f.write(number_in_sequence)
f.write(timecode)
for subtitle in subtitles:
f.write(subtitle)
for translated_subtitle in translated_subtitles:
f.write(translated_subtitle)
f.write('\n')
e += 1
pBar(e, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)
#pbar.update(e)
#pbar.finish()
pBar(total_entries, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)


print('Done.')
print("Original subtitles file created at : {}".format(srt_file))
Expand Down Expand Up @@ -985,6 +1033,7 @@ def run(self):
try:
if not os.path.isfile(cancel_file):
extracted_regions = []

#widgets = ["Converting speech regions to FLAC files : ", Percentage(), ' ', Bar(), ' ', ETA()]
#pbar = ProgressBar(widgets=widgets, maxval=len(regions)).start()

Expand Down Expand Up @@ -1151,10 +1200,12 @@ def run(self):
#widgets = [prompt, Percentage(), ' ', Bar(), ' ', ETA()]
#pbar = ProgressBar(widgets=widgets, maxval=total_entries).start()

'''
e=0
with open(translated_srt_file, 'w', encoding='utf-8') as f:
time.sleep(1)
for number_in_sequence, timecode, subtitles, count_failure, count_entries in translate(entries, src=src, dest=dest, patience="", verbose=""):
#for number_in_sequence, timecode, subtitles, count_failure, count_entries in translate(entries, src=src, dest=dest, patience="", verbose=""):
for number_in_sequence, timecode, subtitles, count_failure, count_entries in pool.imap(translate, entries, src=src, dest=dest, patience="", verbose=""):
check_cancel_file()
f.write(number_in_sequence)
f.write(timecode)
Expand All @@ -1167,6 +1218,23 @@ def run(self):
#pbar.finish()
pBar(total_entries, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)
time.sleep(1)
'''

subtitle_translator = SubtitleTranslator(src=src, dest=dest)
translated_entries = []
for i, translated_entry in enumerate(pool.imap(subtitle_translator, entries)):
check_cancel_file()
translated_entries.append(translated_entry)
pBar(i, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)
pBar(total_entries, total_entries, "Translating from %s to %s: " %(src, dest), activity, textView_debug)

with open(translated_srt_file, 'w', encoding='utf-8') as f:
for number_in_sequence, timecode, translated_subtitles in translated_entries:
f.write(number_in_sequence)
f.write(timecode)
for translated_subtitle in translated_subtitles:
f.write(translated_subtitle)
f.write('\n')

print('Done.')
print("Original subtitles file created at : {}".format(srt_file))
Expand Down Expand Up @@ -1252,7 +1320,8 @@ def printEnvironmentDir():
#for md in media_dirs:
#print("media_dirs = {}".format(md))
# /storage/emulated/0/Android/media
return

return "OK"

def pBar(count_value, total, prefix, activity, textView_debug):
bar_length = 10
Expand Down Expand Up @@ -1313,6 +1382,8 @@ def run(self):
time.sleep(1)
activity.runOnUiThread(R())



if __name__ == '__main__':
multiprocessing.freeze_support()
sys.exit(main())

0 comments on commit c91433e

Please sign in to comment.