Skip to main content

Advanced subtitle translator with LLM support for converting SRT to bilingual ASS files using OpenAI, Gemini, or DeepSeek APIs

Project description

AI Subtitle Translator

Advanced subtitle translator with AI support and web API. Converts SRT subtitle files to styled ASS format with bilingual or monolingual translations using OpenAI, Google Gemini, or DeepSeek APIs.

Features

  • Web API: FastAPI-based REST API for subtitle translation
  • CLI Tool: Command-line interface for batch processing
  • Multiple AI Providers: OpenAI, Google Gemini, DeepSeek support
  • Bilingual Mode: Displays original text on top and translated text below
  • Monolingual Mode: Replaces original text with translation
  • Smart Translation: Full text or selective difficulty translation modes
  • Resumable: Automatically saves progress and can resume if interrupted
  • Batch Processing: Processes subtitles in batches for efficient API usage
  • Containerized: Docker support with multi-platform builds

Quick Start

Using Docker (Recommended)

  1. Pull the image:

    docker pull ghcr.io/yanp/ai-subtitle-translator:latest
    
  2. Run the container:

    docker run -d \
      -p 8080:8080 \
      -e DEEPSEEK_API_KEY=your_api_key_here \
      ghcr.io/yanp/ai-subtitle-translator:latest
    
  3. Access the API:

Using Docker Compose

  1. Clone the repository:

    git clone https://github.com/yanp/ai-subtitle-translator.git
    cd ai-subtitle-translator
    
  2. Set up environment variables:

    cp .env.example .env
    # Edit .env with your API keys
    
  3. Start the service:

    docker-compose up -d
    

Local Development

  1. Install uv:

    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  2. Install dependencies:

    uv venv
    uv pip install -e .
    
  3. Set environment variables:

    export DEEPSEEK_API_KEY=your_api_key_here
    
  4. Run the API:

    uv run fastapi dev src/ai_subtitle_translator/app.py
    
  5. Or use the CLI:

    uv run translate-subtitles input.srt --translation-mode bilingual
    

API Usage

Translate Subtitle File

POST /translate

Supports two modes: file upload or file path processing.

Mode 1: File Upload (Web Interface)

Upload an SRT file and get back a translated ASS file.

Parameters:

  • file: SRT subtitle file to upload
  • provider: AI provider (openai, gemini, deepseek) - default: deepseek
  • model: Specific model name (optional)
  • translation_mode: bilingual or monolingual - default: bilingual
  • prompt_template: full_text or selective_difficulty - default: selective_difficulty
  • batch_size: Number of lines per API call - default: 50

Example:

curl -X POST "http://localhost:8080/translate" \
  -F "file=@subtitle.srt" \
  -F "provider=deepseek" \
  -F "translation_mode=bilingual" \
  -F "prompt_template=selective_difficulty" \
  -o translated_subtitle.ass

Mode 2: File Path (Bazarr Hook)

Process files on the server filesystem (ideal for Bazarr hooks).

Parameters:

  • input_path: Path to SRT subtitle file on server
  • output_path: Output path for translated file (optional, defaults to same directory with .en-zh.ass extension)
  • provider: AI provider (openai, gemini, deepseek) - default: deepseek
  • model: Specific model name (optional)
  • translation_mode: bilingual or monolingual - default: bilingual
  • prompt_template: full_text or selective_difficulty - default: selective_difficulty
  • batch_size: Number of lines per API call - default: 50

Example:

curl -X POST "http://localhost:8080/translate" \
  -F "input_path=/media/subtitles/movie.srt" \
  -F "output_path=/media/subtitles/movie.en-zh.ass" \
  -F "provider=deepseek" \
  -F "translation_mode=bilingual"

Get Available Providers

GET /providers

Returns available AI providers and their configuration status.

CLI Usage

uv run translate-subtitles input.srt [options]

Options:

  • -o, --output: Output file path
  • --translation-mode: bilingual or monolingual
  • --prompt-template: full_text or selective_difficulty
  • -p, --provider: AI provider (openai, gemini, deepseek)
  • -m, --model: Specific model name
  • --batch-size: Batch size for API calls

Example:

uv run translate-subtitles movie.srt \
  --translation-mode bilingual \
  --prompt-template selective_difficulty \
  --provider deepseek \
  --batch-size 50

Configuration

Environment Variables

Variable Description Required
OPENAI_API_KEY OpenAI API key For OpenAI provider
GEMINI_API_KEY Google Gemini API key For Gemini provider
DEEPSEEK_API_KEY DeepSeek API key For DeepSeek provider

Translation Modes

  • Bilingual: Shows original text on top, translation below
  • Monolingual: Replaces original text with translation

Prompt Templates

  • Full Text: Translates every subtitle line
  • Selective Difficulty: Only translates complex phrases, slang, or cultural references

Development

Setup

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install dependencies
uv venv
uv pip install -e ".[dev]"

# Activate virtual environment
source .venv/bin/activate

Code Quality

# Format code
black .

# Lint code
ruff check .

# Type checking
mypy .

Testing

# Run tests
pytest

# Run with coverage
pytest --cov=.

Docker

Build locally

docker build -t ai-subtitle-translator .

Multi-platform build

docker buildx build --platform linux/amd64,linux/arm64 -t ai-subtitle-translator .

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Support

  • Open an issue on GitHub
  • Check the API documentation at /docs when running the server
  • Review the example files in the repository

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_subtitle_translator-0.3.0.tar.gz (71.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_subtitle_translator-0.3.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file ai_subtitle_translator-0.3.0.tar.gz.

File metadata

  • Download URL: ai_subtitle_translator-0.3.0.tar.gz
  • Upload date:
  • Size: 71.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ai_subtitle_translator-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cbb9d7bb38af2d9fb43155a0f4c51b5a63df9188e50df262f0f72878e661fb19
MD5 e1674ace2d89edfc290baeb2a1fc55f9
BLAKE2b-256 0ee4741637c5e962a5f9c76dc54e569e895ea554f7bdde5c4f246af52466201e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_subtitle_translator-0.3.0.tar.gz:

Publisher: build-and-publish.yaml on yanp/ai-subtitle-translator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ai_subtitle_translator-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_subtitle_translator-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f59c7be5f7bfe218aea1673b8dbc86ab687b8ac050807439d1e5c3a8812ecb1a
MD5 bcd661530d8d2f87f3b9e86d0f26c549
BLAKE2b-256 a27a376fb965fced3476d3b1e2d27595b7bb558cf71b0d0458a3ecc26654f7cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_subtitle_translator-0.3.0-py3-none-any.whl:

Publisher: build-and-publish.yaml on yanp/ai-subtitle-translator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page