Create Translated Subtitles(Video to srt File) with Google Speech-to-text, Cloud Translation, from Japanese to English

Requirements

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="./autotrans.json"

Transcription

# parameters about the audio file
sample_rate_hertz = 44100
language_code = "en-US"
audio_channel_count = 2
encoding = 'LINEAR16'
out_file = "subtitle"
max_chars = 40
storage_uri = 'gs://' + 'autotrans' + '/' +'ok.wav'
# Encoding of audio data sent.client = speech.SpeechClient()operation = client.long_running_recognize(
config=
{
"enable_word_time_offsets": True,
"enable_automatic_punctuation": True,
"sample_rate_hertz": sample_rate_hertz,
"language_code": language_code,
"audio_channel_count": audio_channel_count,
"encoding": encoding,
},
audio={"uri": storage_uri},
)
response = operation.result()

Translate text file

operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": source_lang,
"target_language_codes": target_language_codes,
"input_configs": input_configs,
"output_config": output_config,
})

Align text file with original srt file to output srt file in another language

def process_translations(subs, indexfile):
# read index.csv and foreach translated file,
print("Updating subtitles for each translated language")
with open(indexfile) as f:
lines = f.readlines()
# copy orig subs list and replace content for each line
for line in lines:
index_list = line.split(",")
lang = index_list[1]
langfile = index_list[2].split("/")[-1]
# langfile = '/'+langfile
lang_subs = update_srt(lang, langfile, subs)
write_srt(lang, lang_subs)
return
def update_srt(lang, langfile, subs):
# change subtitles' content to translated lines
with open(langfile) as f:
lines = f.readlines()
i = 0
for line in lines:
subs[i].content = line
i += 1
return subs

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Renee LIN

Renee LIN

Passionate about web dev and data analysis. Huge FFXIV fan.