Translate an AssemblyAI Subtitle Transcript

In this guide, we’ll show you how to translate an AssemblyAI generated subtitle transcript. We will also be using the Googletrans Python library to implement the Google Translate API.

Get Started

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard.

Step-by-Step Instructions

Install the relevant packages.

  1. AssemblyAI SDK
  2. Googletrans
$pip install -U assemblyai googletrans

Import the assemblyai package and set the API key. Import Translator class from googletrans package.

1import assemblyai as aai
2from googletrans import Translator
3
4aai.settings.api_key = "YOUR_API_KEY"

Create a Transcriber object.

1transcriber = aai.Transcriber()

Create a Translator object.

1translator = Translator()

Use the Transcriber object’s transcribe method and pass in the audio file’s path as a parameter. The transcribe method saves the results of the transcription to the Transcriber object’s transcript attribute.

1transcript = transcriber.transcribe("./my-audio.mp3")

Alternatively, you can pass in a path to an audio file saved on the internet.

1transcript = transcriber.transcribe("https://example.org/audio.mp3")

Export SRT subtitles with the export_subtitles_srt method. Create a subtitle_transcript variable to translate in the next step.

1subtitle_transcript = transcript.export_subtitles_srt()

Translate subtitle transcript to target language

1translation = translator.translate(subtitle_transcript, dest='es')

Print results

1print(translation.text)

Conclusion

Using the subtitles and transcripts generated by AssemblyAI, we can also generate translated alternatives using other translation APIs. The implementation logic will be similar with other solutions like DeepL, Yandex Translate and many more.

To translate to other languages, find the full list of Supported Languages in the Further Documentations section.

Further Documentation

Googletrans Library

Translation Supported Languages