Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions src/transcriber.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
import json
import subprocess
from datetime import timedelta
from faster_whisper import WhisperModel

class Transcriber:
def __init__(self, audio_path: str, model_name: str = "openai/whisper-large-v3-turbo"):
def __init__(self, audio_path: str, model_name: str = "base"):
"""
Initialize the transcriber.

Expand Down Expand Up @@ -55,34 +55,31 @@ def transcribe(self, transcript_path: str, subtitle_path: str):
os.makedirs(os.path.dirname(transcript_path), exist_ok=True)
os.makedirs(os.path.dirname(subtitle_path), exist_ok=True)

print("Using insanely-fast-whisper...")
print(f"Model: {self.model_name}")

try:
# Run transcription to get JSON output
result = subprocess.run([
"insanely-fast-whisper",
"--file-name", self.audio_path,
"--transcript-path", transcript_path,
"--language", "en",
"--device-id", "mps",
"--model", self.model_name,
"--task", "transcribe"
], check=True, capture_output=True, text=True)
print(f"Using faster-whisper with model: {self.model_name}")
# Initialize model with CPU compute type
model = WhisperModel(self.model_name, device="cpu", compute_type="int8")

# Transcribe audio
print("Transcribing audio...")
segments, _ = model.transcribe(self.audio_path, language="en")

# Convert segments to JSON format
chunks = []
for segment in segments:
chunks.append({
"timestamp": [segment.start, segment.end],
"text": segment.text
})

print(result.stdout)
# Save JSON transcript
with open(transcript_path, 'w', encoding='utf-8') as f:
json.dump({"chunks": chunks}, f, indent=2)

# Convert JSON to SRT format
# Convert to SRT format
print("Converting transcript to SRT format...")
self._json_to_srt(transcript_path, subtitle_path)

except subprocess.CalledProcessError as e:
print(f"Error during transcription: {e.stderr}")
raise
except json.JSONDecodeError as e:
print(f"Error: Failed to parse JSON transcript at {transcript_path}")
print(f"JSON Error: {str(e)}")
raise
except Exception as e:
print(f"Unexpected error during transcription: {str(e)}")
print(f"Error during transcription: {str(e)}")
raise