1+ import argparse
2+ import json
3+ import os
4+ import whisper
5+
6+ def transcribe_file (input_file , output_file ):
7+ # Load the Whisper model
8+ model = whisper .load_model ("base" )
9+
10+ # Transcribe the audio file
11+ result = model .transcribe (input_file , verbose = False )
12+
13+ # Prepare the output JSON
14+ transcription_data = {
15+ "transcription" : result ["text" ],
16+ "language" : result ["language" ],
17+ "segments" : [
18+ {
19+ "text" : segment ["text" ],
20+ "start" : segment ["start" ],
21+ "end" : segment ["end" ],
22+ }
23+ for segment in result ["segments" ]
24+ ],
25+ }
26+
27+ # Write the JSON to the output file
28+ with open (output_file , "w" ) as f :
29+ json .dump (transcription_data , f , indent = 4 )
30+
31+ print (f"Transcription saved to { output_file } " )
32+
33+ if __name__ == "__main__" :
34+ parser = argparse .ArgumentParser (description = "Transcribe an audio file and save the result as JSON." )
35+ parser .add_argument ("input_file" , help = "Path to the input audio file." )
36+ parser .add_argument ("output_file" , help = "Path to the output JSON file." )
37+
38+ args = parser .parse_args ()
39+
40+ if not os .path .exists (args .input_file ):
41+ print (f"Error: Input file { args .input_file } does not exist." )
42+ exit (1 )
43+
44+ transcribe_file (args .input_file , args .output_file )
0 commit comments