Using Faster Whisper on Windows
from faster_whisper import WhisperModel
# Path to the audio file
audio_path = r"C:\Users\User\Music\Audio.ogg"
# Model size (you can change to "base", "medium", "large-v2", etc.)
model = WhisperModel("small")
# Transcribe the audio
segments, info = model.transcribe(audio_path)
# Display the detected language and the text
print("Detected language:", info.language)
for segment in segments:
print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")import os
import subprocess
from faster_whisper import WhisperModel
from pathlib import Path
# === Configuration ===
# Path to the video
video_path = r"C:\Users\User\Videos\video.mp4"
# Temporary folder for the extracted audio
temp_audio_path = "temp_audio.wav"
# Current user's Desktop folder
desktop_folder = str(Path.home() / "Desktop")
output_text_file = os.path.join(desktop_folder, "transcription.txt")
# === Step 1: Extract audio from video using ffmpeg ===
print(" Extracting audio from video...")
ffmpeg_command = [
"ffmpeg", "-y", # -y to overwrite without asking
"-i", video_path,
"-vn", # No video
"-acodec", "pcm_s16le", # Uncompressed audio (WAV)
"-ar", "16000", # Sample rate
"-ac", "1", # Mono
temp_audio_path
]
try:
subprocess.run(ffmpeg_command, check=True)
except subprocess.CalledProcessError:
print(" Error extracting audio with ffmpeg.")
exit(1)
# === Step 2: Transcribe with Faster Whisper (you can change to "base", "medium", "large-v2", etc.) ===
print(" Transcribing audio...")
model = WhisperModel("large-v2")
segments, info = model.transcribe(temp_audio_path)
# === Step 3: Save transcription ===
print("Saving transcription...")
with open(output_text_file, "w", encoding="utf-8") as f:
f.write(f"Detected language: {info.language}\n\n")
for segment in segments:
f.write(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}\n")
print(f"Transcription saved to: {output_text_file}")
# === Step 4: Remove temporary audio file ===
os.remove(temp_audio_path)
print(" Temporary audio file deleted.")Last updated