|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.11" |
| 3 | +# dependencies = [ |
| 4 | +# "pydub", |
| 5 | +# ] |
| 6 | +# /// |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from pydub import AudioSegment |
| 11 | + |
| 12 | +def format_timestamp(seconds: float) -> str: |
| 13 | + """Convert seconds to SRT timestamp format: HH:MM:SS,mmm""" |
| 14 | + millis = int(seconds * 1000) |
| 15 | + hours = millis // (3600 * 1000) |
| 16 | + minutes = (millis % (3600 * 1000)) // (60 * 1000) |
| 17 | + secs = (millis % (60 * 1000)) // 1000 |
| 18 | + ms = millis % 1000 |
| 19 | + return f"{hours:02}:{minutes:02}:{secs:02},{ms:03}" |
| 20 | + |
| 21 | +def generate_srt(transcript, audio_file, output_file): |
| 22 | + # Load audio for validation |
| 23 | + audio = AudioSegment.from_mp3(audio_file) |
| 24 | + duration_sec = len(audio) / 1000.0 |
| 25 | + |
| 26 | + lines = [] |
| 27 | + for i, entry in enumerate(transcript, start=1): |
| 28 | + start = entry["start"] |
| 29 | + end = entry["end"] |
| 30 | + text = entry["text"] |
| 31 | + |
| 32 | + # validate against audio length |
| 33 | + if end > duration_sec: |
| 34 | + print(f"Warning: Subtitle {i} ends after audio length, trimming to {duration_sec:.2f}s") |
| 35 | + end = duration_sec |
| 36 | + |
| 37 | + start_ts = format_timestamp(start) |
| 38 | + end_ts = format_timestamp(end) |
| 39 | + |
| 40 | + lines.append(f"{i}\n{start_ts} --> {end_ts}\n{text}\n") |
| 41 | + |
| 42 | + Path(output_file).write_text("\n".join(lines), encoding="utf-8") |
| 43 | + print(f"SRT file created: {output_file}") |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + if len(sys.argv) != 4: |
| 47 | + print("Usage: uv run generate_srt.py transcript.json input.mp3 output.srt") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + transcript_file = sys.argv[1] |
| 51 | + audio_file = sys.argv[2] |
| 52 | + output_file = sys.argv[3] |
| 53 | + |
| 54 | + with open(transcript_file, "r", encoding="utf-8") as f: |
| 55 | + data = json.load(f) |
| 56 | + transcript = data["transcription"] |
| 57 | + |
| 58 | + |
| 59 | + generate_srt(transcript, audio_file, output_file) |
0 commit comments