AI-Powered Subtitle Translator
This script provides a simple tool for translating SRT subtitle files using the OpenAI GPT-3.5-turbo API. It supports merging short subtitle segments, ensuring translations are coherent, and processing subtitles efficiently with retry logic for API failures.
- Text Translation: Translates text from one language to another using OpenAI's GPT API.
- Subtitle Merging: Merges subtitles with short time gaps or excessive length to ensure cohesive translation.
- Error Handling: Implements retry logic for API errors, falling back to the original text after multiple failures.
- SRT Processing: Reads and parses SRT subtitle files, translates their content, and outputs the translated subtitles to a new file.
- Python 3.7+
- OpenAI API Key
- Installed Python packages:
openai
srt
asyncio
re
Install dependencies using pip:
pip install openai srt
-
Add your OpenAI API key in the script:
openai.api_key = "your_openai_api_key"
-
Update the paths to your input and output SRT files:
input_srt = "path/to/your/input.srt" output_srt = "path/to/your/output.srt"
Run the script in your terminal:
python script_name.py
The translated SRT file will be saved to the specified output path.
The translate_text_async
function uses OpenAI's GPT API to translate text asynchronously. The translate_text
function wraps it for synchronous use.
- Parameters:
text
: The text to translate.source_language
: The source language (default: Japanese"ja"
).target_language
: The target language (default: Chinese"zh"
).max_retries
: Number of retries for failed API calls (default: 3).
The merge_subtitles
function combines consecutive subtitles with short gaps (max_gap
) or excessive combined length (max_length
) to ensure better translation.
- Parameters:
max_gap
: Maximum gap in seconds to merge subtitles (default: 2).max_length
: Maximum character length for combined subtitles (default: 500).
The translate_srt
function reads the input SRT file, merges and translates subtitles, and writes the result to an output SRT file.
- Parameters:
input_file
: Path to the input SRT file.output_file
: Path to save the translated SRT file.
Input Subtitle (Japanese):
1
00:00:01,000 --> 00:00:04,000
こんにちは、世界!
2
00:00:05,000 --> 00:00:07,000
これはサンプル字幕です。
Output Subtitle (Chinese):
1
00:00:01,000 --> 00:00:07,000
你好,世界!这是一个示例字幕。
- API Failures: The script retries API calls up to 3 times. If all retries fail, the original text is returned.
- File I/O Errors: Handles errors in reading/writing files gracefully.
- The OpenAI GPT API may incur usage costs. Ensure your API key has an active quota.
- The script is optimized for translating SRT files with simple text. Complex formatting or edge cases may require adjustments.
Feel free to clone and modify this repository for your subtitle translation needs! 😊