-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe_whisper.py
More file actions
55 lines (45 loc) · 1.86 KB
/
Copy pathtranscribe_whisper.py
File metadata and controls
55 lines (45 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUT_DIR = ROOT / "data" / "output"
OUT_TXT = OUT_DIR / "transcript.txt"
def main(audio_path: str):
OUT_DIR.mkdir(parents=True, exist_ok=True)
audio = Path(audio_path).expanduser().resolve()
if not audio.is_file():
raise FileNotFoundError(f"Audio file not found: {audio}")
# whisper-cli writes transcript to "<-of>.txt"
out_prefix = OUT_DIR / "transcript" # will create transcript.txt
cmd = [
"whisper-cli",
"-m", str(ROOT / "whisper.cpp" / "models" / "ggml-base.en.bin"),
"-f", str(audio),
"-of", str(out_prefix),
"-otxt", # ensure txt output enabled
"-nt", # optional: no timestamps in terminal (file still ok)
]
print("⏳ Loading ASR model (10%)")
subprocess.run(cmd, check=True)
# whisper-cli writes to transcript.txt (based on -of)
generated = OUT_DIR / "transcript.txt"
if not generated.exists():
raise RuntimeError("Whisper did not generate transcript.txt (output file missing).")
text = generated.read_text(encoding="utf-8", errors="ignore").strip()
if not text:
raise RuntimeError(
"Whisper produced empty transcript.\n"
"Common causes:\n"
"1) The audio is silence / wrong file\n"
"2) The input path is wrong\n"
"3) whisper-cli failed but still printed timings\n"
)
# normalize final location/name (keep same file name)
OUT_TXT.write_text(text + "\n", encoding="utf-8")
print("✅ Transcription complete (40%)")
print(f"✅ Saved transcript to {OUT_TXT}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python src/transcribe_whisper.py <audio.wav>")
sys.exit(1)
main(sys.argv[1])