:: krowemoh

Tuesday | 11 NOV 2025
Posts Links Other About Now

previous
next

2025-11-04
Translating Subtitles

google, translate, python

I have some subtitles that I generated but they were in a language other than English and as I really only know English, I needed some translation work.

I used Opus-MT but that didn't seem to give me as great a translation as I was hoping so I decided to use the google translate API. This required me to use google cloud and it worked relatively well.

I previous used google cloud. The key step is to start the terminal from the top right and to read the translation tutorial.

Below is my script to translate subtitles. pysubs2 is handy to parse the srt and get the specific text that needs to be translated. It would have been trivial to write my own little line counting and parsing logic but this library made quick work of the entire thing.

import os
import sys

import pysubs2
from google.cloud import translate

PROJECT_ID = os.environ.get("PROJECT_ID", "")
assert PROJECT_ID
PARENT = f"projects/{PROJECT_ID}"

def translate_text(text: str):
    client = translate.TranslationServiceClient()
    
    response = client.translate_text(
        parent=PARENT,
        contents=[text],
        target_language_code='en',
    )
    
    return response.translations[0]
    
filename = sys.argv[1]
fname = os.path.basename(filename)

subs = pysubs2.load(filename, encoding="utf-8")

for line in subs:
    eng = translate_text(line.text)
    line.text = eng.translated_text
    print(line.text)
    subs.save(f'eng/{fname}')