Skip to content

Instantly share code, notes, and snippets.

@spech66
Created April 4, 2023 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spech66/5385a78ad0af4cb4f287e0543505d0a2 to your computer and use it in GitHub Desktop.
Save spech66/5385a78ad0af4cb4f287e0543505d0a2 to your computer and use it in GitHub Desktop.
OpenAI whisper speech to text with ffmpeg based ogg->mp3 converter for WhatsApp messages
import os
import sys
import openai
# Fail if no commandline argument is provided
if len(sys.argv) < 2:
print("Please provide an audio file")
exit(1)
audio_file_name = sys.argv[1]
# Check if file exists
if not os.path.isfile(audio_file_name):
print("File does not exist")
exit(1)
# If file is in ogg format convert it to mp3
if audio_file_name.endswith(".ogg"):
os.system(f"ffmpeg -i '{audio_file_name}' -ab 320k '{audio_file_name}.mp3'")
audio_file_name = f"{audio_file_name}.mp3"
audio_file= open(audio_file_name, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file, language="de")
print(transcript)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment