Metadata-Version: 2.4
Name: audio-subtitler
Version: 0.1.16
Summary: Convert audio files to subtitles (VTT, SRT) using Faster-Whisper
Author: Gary Lab
License: MIT License
        
        Copyright (c) 2025 audio-subtitler contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        
Project-URL: Homepage, https://github.com/garylab/audio-subtitler
Project-URL: Author Blog, https://garymeng.com
Keywords: audio,transcription,vtt,srt,subtitles,whisper,speech-to-text,webvtt
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: faster-whisper>=1.2.1
Requires-Dist: stt2vtt>=0.0.5
Requires-Dist: webvtt-py>=0.5.1
Provides-Extra: runpod
Requires-Dist: runpod>=1.7.0; extra == "runpod"
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Dynamic: license-file

# Audio Subtitler

Convert audio files to subtitles (VTT, SRT) using Faster-Whisper.

[![PyPI](https://img.shields.io/pypi/v/audio-subtitler.svg)](https://pypi.org/project/audio-subtitler/)
[![Python Versions](https://img.shields.io/pypi/pyversions/audio-subtitler.svg)](https://pypi.org/project/audio-subtitler/)
[![Downloads](https://static.pepy.tech/badge/audio-subtitler)](https://pepy.tech/project/audio-subtitler)
[![Monthly Downloads](https://img.shields.io/pypi/dm/audio-subtitler.svg)](https://pypi.org/project/audio-subtitler/)
[![Run on RunPod](https://img.shields.io/badge/Run%20on-RunPod-6b3cff?logo=runpod&logoColor=white)](https://runpod.io?ref=hh0mhml0)

## Features

- 🚀 **Full Faster-Whisper support** - All features and parameters from faster-whisper
- 📝 **Multiple formats** - VTT (WebVTT) and SRT subtitle output
- 🎯 **Smart auto-detection** - Automatically detects format from file extension
- 🌍 **Multi-language** - Supports 100+ languages with auto-detection
- ⚡ **GPU acceleration** - CUDA support for faster transcription
- 🎙️ **Voice Activity Detection** - Automatically removes silence
- 💻 **Simple APIs** - Easy-to-use CLI and Python API
- 🐳 **Docker GPU support** - Ready for serverless deployment

## Installation

```bash
pip install audio-subtitler
```

Optional dependencies:
```bash
pip install audio-subtitler[runpod]  # For RunPod serverless
pip install audio-subtitler[dev]     # For development
```

## Quick Start

### CLI

```bash
# Auto-detect format from file extension (recommended)
audiosubtitler input.mp3 -o output.vtt
audiosubtitler input.mp3 -o output.srt

# Specify options
audiosubtitler input.mp3 -o output.vtt --model large-v3 --language en --device cuda

# Output to stdout
audiosubtitler input.mp3 --format srt > output.srt

# Use shorter command
audiosub input.mp3 -o output.vtt

# Hint punctuation style (helps large-v3 and others output more periods, commas, etc.)
audiosubtitler input.mp3 -o output.vtt --initial-prompt "Hello. How are you? Thanks."
```

### Python API

```python
from src import AudioSubtitler

# Initialize
converter = AudioSubtitler(
    model_size_or_path="base",
    device="cpu",
    compute_type="int8"
)

# Transcribe (returns subtitle string directly)
vtt = converter.transcribe("audio.mp3", format="vtt", language="en")
print(vtt)  # "WEBVTT\n\n00:00:00.000 --> ..."

srt = converter.transcribe("audio.mp3", format="srt")

# Better punctuation: pass a short punctuated phrase as a hint
vtt = converter.transcribe("audio.mp3", format="vtt", initial_prompt="Hello. How are you? Thanks.")
```

## Getting better punctuation (for subtitles)

Larger Whisper models (e.g. large-v3) are more accurate on words but often output **longer segments with less punctuation**. Parameters that affect punctuation and segment boundaries:

| Parameter | Default | Effect |
|-----------|---------|--------|
| `--initial-prompt` | `"Hello. How are you? Thanks, bye."` | Hints punctuation style; use `''` to disable. |
| `--vad-silence-duration-ms` | `500` | Lower = more segment breaks (e.g. `300` or `400`) = more punctuation. |
| `--patience` | `1.0` | Lower = more segment boundaries (e.g. `0` or `0.5`) = more punctuation. |

Example for more punctuation:
```bash
audiosubtitler input.mp3 -o output.vtt --vad-silence-duration-ms 400 --patience 0.5
```

2. **Post-process with a punctuation restoration model**  
   For English, you can run the transcript through a dedicated punctuation model (e.g. [rpunct](https://pypi.org/project/rpunct/) or [speechbox](https://pypi.org/project/speechbox/)) and then regenerate VTT/SRT from the punctuated text if your tool supports it.

## API Reference

### AudioSubtitler

**Constructor**: `AudioSubtitler(**kwargs)`

Accepts all [faster-whisper WhisperModel](https://github.com/SYSTRAN/faster-whisper) parameters:
- `model_size_or_path`: Model name (tiny, base, small, medium, large, large-v3) or path
- `device`: "cpu", "cuda", or "auto"
- `compute_type`: "int8", "int8_float16", "int16", "float16", "float32"
- `cpu_threads`, `num_workers`, `download_root`, `local_files_only`, etc.

**Method**: `transcribe(audio, format="vtt", **kwargs)`

Parameters:
- `audio`: File path (str), file object (BinaryIO), or numpy array
- `format`: "vtt" or "srt" (default: "vtt")
- `**kwargs`: All [faster-whisper transcribe](https://github.com/SYSTRAN/faster-whisper#transcribe) parameters
  - `language`, `beam_size`, `vad_parameters`, `word_timestamps`, etc.

Returns: **str** — The subtitle content (VTT, SRT, or JSON string depending on `format`).

## Docker (GPU only)

```bash
docker-compose -f docker-compose-gpu.yml up
```

**RunPod serverless**

Input (in the job `input`):
```json
{
  "audio": "<base64_encoded_audio>",
  "format": "vtt"
}
```
- `audio`: required, base64-encoded audio bytes
- `format`: optional, `"vtt"` (default), `"srt"`, or `"json"`

Output: the handler **returns the subtitle string directly** (no wrapper). RunPod puts it in the job result’s `output` field, so the response body looks like:
```json
{
  "delayTime": 1119,
  "executionTime": 499,
  "id": "...",
  "output": "WEBVTT\n\n00:00:00.000 --> 00:00:00.280\nHello\n\n...",
  "status": "COMPLETED",
  "workerId": "..."
}
```
Use `response["output"]` to get the VTT/SRT/JSON string.

Errors: the handler **raises exceptions** (e.g. no audio, invalid base64, transcription failure). RunPod surfaces these in its error response.

## Output Examples

**VTT:**
```
WEBVTT

00:00:00.000 --> 00:00:03.500
Hello, this is a test transcription.

00:00:03.500 --> 00:00:07.200
The audio is converted to text with timestamps.
```

**SRT:**
```
1
00:00:00,000 --> 00:00:03,500
Hello, this is a test transcription.

2
00:00:03,500 --> 00:00:07,200
The audio is converted to text with timestamps.
```

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `WHISPER_MODEL` | `base` | Model size |
| `WHISPER_DEVICE` | `cpu` | cpu, cuda, auto |
| `WHISPER_COMPUTE_TYPE` | `int8` | Compute type |
| `WHISPER_BEAM_SIZE` | `5` | Beam size |
| `WHISPER_VAD_SILENCE_MS` | `500` | Min silence (ms) for segment boundaries (RunPod). Lower = more punctuation. |
| `WHISPER_PATIENCE` | `1.0` | Beam search patience (RunPod). Lower = more segment boundaries. |


## License

MIT License - see [LICENSE](LICENSE) file for details.
