|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate word-level transcript from audio/video using OpenAI Whisper. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python scripts/transcribe.py <input-file> <output-json> [model-size] |
| 7 | +
|
| 8 | +Example: |
| 9 | + python scripts/transcribe.py \ |
| 10 | + public/assets/talks/codegen-in-rust/audio.m4a \ |
| 11 | + public/transcripts/t2468.json \ |
| 12 | + medium |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | +import json |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +def transcribe_file(input_path: str, output_path: str, model_size: str = "base"): |
| 20 | + """ |
| 21 | + Transcribe audio/video file with word-level timestamps. |
| 22 | +
|
| 23 | + Args: |
| 24 | + input_path: Path to audio/video file |
| 25 | + output_path: Path to output JSON file |
| 26 | + model_size: Whisper model size (tiny, base, small, medium, large) |
| 27 | + """ |
| 28 | + try: |
| 29 | + import whisper |
| 30 | + except ImportError: |
| 31 | + print("Error: openai-whisper is not installed.") |
| 32 | + print("Install it with: pip install openai-whisper") |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + print(f"Loading Whisper model: {model_size}") |
| 36 | + model = whisper.load_model(model_size) |
| 37 | + |
| 38 | + print(f"Transcribing: {input_path}") |
| 39 | + print("This may take a while depending on file length and model size...") |
| 40 | + |
| 41 | + result = model.transcribe( |
| 42 | + input_path, |
| 43 | + word_timestamps=True, # Critical for word-level highlighting |
| 44 | + language="en" # Can be removed for auto-detection |
| 45 | + ) |
| 46 | + |
| 47 | + # Build transcript structure |
| 48 | + transcript = { |
| 49 | + "language": result.get("language", "en"), |
| 50 | + "duration": 0, |
| 51 | + "segments": [] |
| 52 | + } |
| 53 | + |
| 54 | + total_words = 0 |
| 55 | + |
| 56 | + for segment in result.get("segments", []): |
| 57 | + seg_data = { |
| 58 | + "start": round(segment["start"], 3), |
| 59 | + "end": round(segment["end"], 3), |
| 60 | + "text": segment["text"].strip(), |
| 61 | + "words": [] |
| 62 | + } |
| 63 | + |
| 64 | + # Extract word-level timestamps if available |
| 65 | + if "words" in segment: |
| 66 | + for word in segment["words"]: |
| 67 | + seg_data["words"].append({ |
| 68 | + "word": word["word"].strip(), |
| 69 | + "start": round(word["start"], 3), |
| 70 | + "end": round(word["end"], 3) |
| 71 | + }) |
| 72 | + total_words += 1 |
| 73 | + |
| 74 | + transcript["segments"].append(seg_data) |
| 75 | + transcript["duration"] = max(transcript["duration"], seg_data["end"]) |
| 76 | + |
| 77 | + # Ensure output directory exists |
| 78 | + output_file = Path(output_path) |
| 79 | + output_file.parent.mkdir(parents=True, exist_ok=True) |
| 80 | + |
| 81 | + # Write JSON |
| 82 | + with open(output_file, 'w', encoding='utf-8') as f: |
| 83 | + json.dump(transcript, f, indent=2, ensure_ascii=False) |
| 84 | + |
| 85 | + print(f"\n✓ Transcript saved to: {output_path}") |
| 86 | + print(f" Language: {transcript['language']}") |
| 87 | + print(f" Duration: {transcript['duration']:.1f}s ({transcript['duration'] / 60:.1f}min)") |
| 88 | + print(f" Segments: {len(transcript['segments'])}") |
| 89 | + print(f" Words: {total_words}") |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + if len(sys.argv) < 3: |
| 93 | + print(__doc__) |
| 94 | + sys.exit(1) |
| 95 | + |
| 96 | + input_file = sys.argv[1] |
| 97 | + output_file = sys.argv[2] |
| 98 | + model = sys.argv[3] if len(sys.argv) > 3 else "base" |
| 99 | + |
| 100 | + if not Path(input_file).exists(): |
| 101 | + print(f"Error: Input file not found: {input_file}") |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + transcribe_file(input_file, output_file, model) |
0 commit comments