Skip to content

A Python-based SRT subtitle translator powered by LLMs (like GPT or Claude)

License

Notifications You must be signed in to change notification settings

alejandrosnz/srt-llm-translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SRT LLM Translator

Overview

The SRT LLM Translator is a Python-based tool that translates subtitles from one language to another using large language models. It preserves the original timestamps of the subtitles, making it easy to integrate translated subtitles back into video files.

Features

  • Translates SRT subtitle files to a specified target language.
  • Maintains original timestamps for seamless integration.
  • Utilizes OpenAI's API for translation.

Requirements

  • Python 3.x
  • OpenAI API key
  • Required Python packages:
    • openai
    • srt

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
  2. Install the required packages:

    pip install -r requirements.txt
  3. Set up your OpenAI API key and optionally change the default model:

    export OPENAI_API_KEY='your_OpenAI_api_key'
    export OPENAI_MODEL='gpt-4o-mini'

Usage

To translate an SRT file, run the following command:

python srt_llm_translator.py --target-lang <target_language> --file <source_file.srt>

To translate multiple SRT files, run the following command:

python srt_llm_translator.py --target-lang <target_language> --folder <path/to/dir>

Parameters

  • --target-lang: The language code for the target language (e.g., en for English, es for Spanish).
  • --file: The path to the source SRT file.
  • --folder: The path to a directory where your source SRT files are.

Example

python srt_llm_translator.py --target-lang es --file sample/sample.srt

Other models

You can use other models by overwritting the following environment variables:

OpenRouter

export OPENAI_API_KEY='your_OpenRouter_api_key'
export OPENAI_MODEL=anthropic/claude-3.5-sonnet
export OPENAI_API_URL=https://openrouter.ai/api/v1

xAI Grok

export OPENAI_API_KEY='your_xAI_api_key'
export OPENAI_MODEL=grok-beta
export OPENAI_API_URL=https://api.x.ai/v1

Cost and Performance

The cost and performance of the translator were tested with xAI's grok-beta model (priced at $5/M token input and $15/M output). With this model, a 1-hour SRT file (approximately 500 phrases) cost around $0.25 and took 6 minutes to process. In contrast, using OpenAI's GPT 4o-mini model (priced at $0.15/M token input and $0.60/M output), the same 1-hour SRT file cost less than $0.01, but took nearly 8 minutes to process.

Due to the poor time performance observed with the initial implementation, I decided to introduce parallelism in the translation process. The results were significant, as shown in the table below:

Thread count xAI Grok-beta OpenAI GPT 4o-mini
1 6 min 8 min
10 50 sec 45 sec
20 (default) 30 sec 25 sec
50 75 sec 12 sec

The translation time has been significantly reduced, but Grok was unable to handle the 50 parallel requests. The thread count can be defined using the MAX_CONCURRENT_CALLS environment variable, which I have set to 20 as the default, just to be safe. However, GPT 4o-mini can handle 50 threads without any issues.