Skip to main content

ElevenLabs Force Alignment SRT Generator

🎬 A powerful Python tool for generating synchronized SRT subtitles using ElevenLabs Force Alignment API with optional AI-powered semantic segmentation.

✨ Features

  • High-Precision Alignment: Uses ElevenLabs Force Alignment API for accurate word-level timing
  • AI Semantic Segmentation: Leverages Google Gemini for intelligent subtitle breaking
  • Bilingual Support: Automatically generates bilingual subtitles (original + translation)
  • Multi-Language: Supports 99+ languages including Chinese, English, Japanese, Korean, etc.
  • Smart Formatting: Removes punctuation and optimizes line breaks for readability
  • Flexible Output: Configurable character limits and segmentation strategies

🚀 Quick Start

Prerequisites

Installation

pip install elevenlabs-srt-generator

Option 2: Install from Source

git clone https://github.com/preangelleo/script-force-alignment.git
cd script-force-alignment
pip install -r requirements.txt
  1. Set up environment variables:
cp .env.example .env
# Edit .env and add your API keys
  1. Run setup validation:
python setup.py

📖 Usage

Command Line Interface

After installing from PyPI, you can use the CLI directly:

# Basic usage
elevenlabs-srt audio.mp3 "Your transcript text" -o output.srt

# With options
elevenlabs-srt audio.mp3 transcript.txt \
  --output subtitles.srt \
  --max-chars 30 \
  --language chinese \
  --no-semantic  # Disable AI segmentation

Python API

from main import elevenlabs_force_alignment_to_srt

# Generate subtitles
success, result = elevenlabs_force_alignment_to_srt(
    audio_file="path/to/audio.mp3",
    input_text="Your transcript text here",
    output_filepath="output/subtitles.srt",
    max_chars_per_line=20,
    language='chinese',
    use_semantic_segmentation=True,  # Enable AI segmentation
    model='gemini-2.0-flash'  # Optional: specify Gemini model
)

if success:
    print(f"Subtitles saved to: {result}")

Using the Example Script

Edit example_usage.py with your parameters:

# Configuration
AUDIO_FILE_PATH = "./samples/your_audio.mp3"
TEXT_CONTENT = "Your transcript here..."
OUTPUT_FILE_PATH = "./output/subtitles.srt"
LANGUAGE = 'chinese'
MAX_CHARS_PER_LINE = 20
USE_SEMANTIC_SEGMENTATION = True

Then run:

python example_usage.py

Running Tests

The test script allows you to compare semantic vs simple segmentation:

python test.py

🔧 API Configuration

Required Environment Variables

Create a .env file with:

ELEVENLABS_API_KEY=your_elevenlabs_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here

Getting API Keys

  1. ElevenLabs API Key:

    • Sign up at ElevenLabs
    • Go to your profile settings
    • Copy your API key
    • Important: Enable the Force Alignment feature in your API settings (it's disabled by default)
  2. Google Gemini API Key:

📝 API Reference

Main Function

elevenlabs_force_alignment_to_srt(
    audio_file: str,           # Path to audio file
    input_text: str,           # Transcript text
    output_filepath: str,      # Output SRT path
    api_key: str = None,       # Optional API key override
    max_chars_per_line: int = 20,  # Max characters per line
    language: str = 'chinese',     # Language code
    use_semantic_segmentation: bool = True,  # Enable AI segmentation
    model: str = None          # Gemini model (default: gemini-2.0-flash)
) -> Tuple[bool, str]

Parameters

  • audio_file: Path to audio file (MP3, WAV, M4A, OGG, FLAC, etc.)
  • input_text: Exact transcript of the audio content
  • output_filepath: Where to save the SRT file
  • api_key: Optional ElevenLabs API key (overrides .env)
  • max_chars_per_line: Maximum characters per subtitle line
  • language: Language of the content (e.g., 'chinese', 'english')
  • use_semantic_segmentation: Enable AI-powered semantic breaking
  • model: Gemini model to use (default: 'gemini-2.0-flash'). Options:
    • 'gemini-2.0-flash': Fast and efficient (default)
    • 'gemini-2.0-flash-exp': Experimental features
    • 'gemini-1.5-pro': Higher quality output
    • 'gemini-2.0-flash-thinking': Complex reasoning

Returns

  • Tuple[bool, str]: (Success status, Output path or error message)

🎯 Features Comparison

Feature Semantic Segmentation Simple Segmentation
Natural breaks ✅ Yes ❌ No
Bilingual support ✅ Yes ❌ No
AI-powered ✅ Yes ❌ No
Processing time ~3-5s ~1-2s
Quality High Basic

🌍 Supported Languages

The tool supports 99+ languages including:

  • Chinese (Simplified & Traditional)
  • English
  • Japanese
  • Korean
  • Spanish
  • French
  • German
  • Russian
  • Arabic
  • Hindi
  • And many more...

📊 Output Format

The tool generates standard SRT format:

1
00:00:00,123 --> 00:00:02,456
这是第一行字幕
This is the first subtitle

2
00:00:02,456 --> 00:00:05,789
这是第二行字幕
This is the second subtitle

🔍 Troubleshooting

Common Issues

  1. API Key Errors:

    • Ensure your API keys are valid
    • Check that .env file is in the correct location
    • Verify keys don't have extra spaces
  2. Audio File Issues:

    • Maximum file size: 1GB
    • Supported formats: MP3, WAV, M4A, OGG, FLAC, AAC, OPUS, MP4
    • Ensure file path is correct
  3. Text Alignment Issues:

    • Text must match audio content exactly
    • Remove extra spaces or formatting
    • Check language setting matches audio

Debug Mode

Enable detailed logging by setting environment variable:

export DEBUG=true
python example_usage.py

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

📧 Support

For issues, questions, or suggestions:

🚦 Project Status

Python License API AI


Made with ❤️ for the subtitle generation community

Metadata

Release files for elevenlabs-srt-generator 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elevenlabs-srt-generator 1.0.2
File Size Uploaded Provenance Actions
elevenlabs_srt_generator-1.0.2.tar.gz 19.4 kB Without provenance Details for elevenlabs_srt_generator-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for elevenlabs-srt-generator 1.0.2
File Interpreter ABI Platform Actions
elevenlabs_srt_generator-1.0.2-py3-none-any.whl Python 3 none any Details for elevenlabs_srt_generator-1.0.2-py3-none-any.whl

Total release size: 32.6 kB

Release files / elevenlabs_srt_generator-1.0.2.tar.gz

Download URL elevenlabs_srt_generator-1.0.2.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
76ad8ce54e2ff2ca906a72d4b16ffde0737844f2da48acb845720abe529705aa
BLAKE2b-256 checksum
How to use checksums
4e9370d50f8c19e22026c31f35a3e9037b99e2bdb118c7523bb94ae8156497c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / elevenlabs_srt_generator-1.0.2-py3-none-any.whl

Download URL elevenlabs_srt_generator-1.0.2-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a7181ba88f824b1db221c779043b97b8a8e0ac980d40d286124a87c9c0c5c4e
BLAKE2b-256 checksum
How to use checksums
175589a91127250ea3796f452309fad11c6e44809252e8779619d1ce501f0af8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Security for elevenlabs-srt-generator 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page