Last active
March 30, 2025 12:34
-
-
Save sebington/7b20c57bf80cf3b91aea673089aab07e to your computer and use it in GitHub Desktop.
Transcribe an audio/video file with Groq Whisper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = ">=3.13" | |
# dependencies = [ | |
# "groq", | |
# ] | |
# /// | |
from groq import Groq | |
import math | |
client = Groq() | |
filename = "input.mp3" # change file name here (40 Mb max) | |
with open(filename, "rb") as file: | |
transcription = client.audio.transcriptions.create( | |
file=(filename, file.read()), | |
model="whisper-large-v3-turbo", # or "distil-whisper-large-v3-en" or "whisper-large-v3" | |
response_format="verbose_json", | |
) | |
segments = (transcription.segments) | |
def convert_seconds_to_hms(seconds): | |
hours, remainder = divmod(seconds, 3600) | |
minutes, seconds = divmod(remainder, 60) | |
milliseconds = math.floor((seconds % 1) * 1000) | |
output = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02},{milliseconds:03}" | |
return output | |
count = 0 | |
with open("subs.srt", 'w') as f: | |
for segment in segments: | |
count +=1 | |
duration = f"{convert_seconds_to_hms(segment['start'])} --> {convert_seconds_to_hms(segment['end'])}\n" | |
text = f"{segment['text'].lstrip()}\n\n" | |
f.write(f"{count}\n{duration}{text}") | |
print("SRT file saved successfully!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment