-
Notifications
You must be signed in to change notification settings - Fork 0
/
diet-scribe.py
65 lines (51 loc) · 2.29 KB
/
diet-scribe.py
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
56
57
58
59
60
61
62
63
64
65
import sys
import os
from pydub import AudioSegment
import openai
from moviepy.editor import *
openai.api_key = "insert-your-api-key" #insert your api key here
def convert_audio_to_wav(input_file):
try:
output_file = os.path.splitext(input_file)[0] + ".wav"
file_extension = os.path.splitext(input_file)[1].lower()
if file_extension == ".mp3":
audio = AudioSegment.from_mp3(input_file)
elif file_extension == ".m4a":
audio = AudioSegment.from_file(input_file, "m4a")
elif file_extension == ".mp4":
video = VideoFileClip(input_file)
video.audio.write_audiofile(output_file, codec="pcm_s16le", verbose=False)
print(f"Conversion successful! WAV file saved as: {output_file}")
return output_file
else:
raise ValueError("Unsupported audio format")
audio.export(output_file, format="wav")
print(f"Conversion successful! WAV file saved as: {output_file}")
return output_file
except Exception as e:
print(f"An error occurred during conversion: {e}")
return None
def transcribe(saved_wav_file_path):
file = open(saved_wav_file_path, "rb")
transcription = openai.Audio.transcribe("whisper-1", file)
text_output = transcription["text"]
txt_file_path = os.path.splitext(saved_wav_file_path)[0] + ".txt"
with open(txt_file_path, "w") as txt_file:
txt_file.write(text_output)
print(f"Transcription saved to: {txt_file_path}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python audio_to_txt.py <input_file.mp3 or input_file.m4a or input_file.mp4>")
sys.exit(1)
input_file = sys.argv[1]
if not os.path.isfile(input_file):
print(f"Input file '{input_file}' not found.")
sys.exit(1)
if not (input_file.lower().endswith(".mp3") or input_file.lower().endswith(".m4a") or input_file.lower().endswith(".mp4")):
print("Input file must be an MP3, M4A, or MP4 file.")
sys.exit(1)
saved_wav_file_path = convert_audio_to_wav(input_file)
if saved_wav_file_path is not None:
transcribe(saved_wav_file_path)
else:
print("Conversion failed, no WAV file path available.")