- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTranscriptify.py
More file actions
Latest commit
89 lines (68 loc) · 2.78 KB
/
Copy pathTranscriptify.py
File metadata and controls
89 lines (68 loc) · 2.78 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
importos
importtime
importopenai
fromtqdmimporttqdm
importsubprocess
# Define API key file path
API_KEY_PATH=os.path.expanduser("~/.openai")
# Load API key from file if it exists
ifos.path.isfile(API_KEY_PATH):
withopen(API_KEY_PATH, "r") asf:
api_key=f.read().strip()
openai.api_key=api_key
# Ask for API key if it's not loaded
whilenotopenai.api_key:
api_key=input("Please enter your OpenAI API key: ").strip()
openai.api_key=api_key
# Save API key to file
withopen(API_KEY_PATH, "w") asf:
f.write(api_key)
# Get ffmpeg path
ifos.name=='nt': # Windows system
try:
ffmpeg_path=subprocess.check_output(['where', 'ffmpeg']).decode().strip()
except:
print('Please make sure ffmpeg is installed and added to the PATH environment variable')
exit()
else: # Linux and Mac systems
try:
ffmpeg_path=subprocess.check_output(['which', 'ffmpeg']).decode().strip()
except:
print('Please make sure ffmpeg is installed and added to the PATH environment variable')
exit()
defcompress_audio(input_file):
# Get input file size
file_size=os.path.getsize(input_file)
iffile_size<=25000000:
returninput_file
# Compress audio file using ffmpeg
output_file=f"{os.path.splitext(input_file)[0]}_compressed.mp3"
command=f"{ffmpeg_path} -i {input_file} -ac 1 -ar 16000 -ab 32k {output_file}"
os.system(command)
returnoutput_file
deftranscribe_audio(input_file, output_file, response_format, progress_bar):
withopen(input_file, "rb") asf:
transcript=openai.Audio.transcribe("whisper-1", f, response_format=response_format)
progress_bar.update(100)
withopen(output_file, "w", encoding="utf-8") asf:
f.write(transcript)
# Remove compressed file
compressed_file=f"{os.path.splitext(input_file)[0]}_compressed.mp3"
ifos.path.exists(compressed_file):
os.remove(compressed_file)
input_file=input("Please enter the audio file name (including extension): ").strip("'\"")
output_format=input("Please enter the output format (txt, vtt, srt, tsv, json, all): ").strip("'\"")
ifoutput_formatnotin ["txt", "vtt", "srt", "tsv", "json", "all"]:
print("Invalid output format.")
exit()
file_name, _=os.path.splitext(input_file)
output_file=f"{file_name}_transcript.{output_format}"
# Compress audio file if larger than 25MB
input_file=compress_audio(input_file)
progress_bar=tqdm(total=100, desc="Transcribing audio", ncols=80)
start_time=time.time()
transcribe_audio(input_file, output_file, output_format, progress_bar)
progress_bar.close()
elapsed_time=time.time() -start_time
print(f"Audio transcription completed in {elapsed_time:.2f} seconds")
print(f"Transcript saved to {output_file}")