1- import subprocess
21import os
2+ import subprocess
33import json
4- import re
54
6- def transcribe_audio_with_whisper (audio_file_path ):
7- if not os .path .exists (audio_file_path ):
8- raise FileNotFoundError (f"Audio file { audio_file_path } does not exist." )
5+ # Path to the Whisper executable inside the container
6+ WHISPER_EXECUTABLE = './main' # Executable 'main' is assumed to be in the same directory as this script
7+
8+ def transcribe_audio (media_filepath ):
9+ # Ensure the media file exists
10+ if not os .path .exists (media_filepath ):
11+ raise FileNotFoundError (f"Media file not found: { media_filepath } " )
12+
13+ # Path to the output JSON file that Whisper will generate
14+ json_output_path = f"{ media_filepath } .json"
915
10- command = [
11- "whisper" ,
12- audio_file_path ,
13- "--model" , "base.en" ,
14- "--output_format" , "json"
16+ # Command to run Whisper.cpp inside the container using the main executable
17+ whisper_command = [
18+ WHISPER_EXECUTABLE , # Path to Whisper executable
19+ '-ojf' , # Output as JSON file
20+ '-f' , media_filepath # Media file path
1521 ]
1622
17- try :
18- result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , check = True )
19-
20- print ("Whisper Output:" )
21- print (result .stdout )
22-
23- formatted_data = {"en" : []}
24-
25- segments = result .stdout .strip ().split ('\n \n ' )
26- for segment in segments :
27- match = re .search (r'\[(\d+:\d+\.\d+)\s+-->\s+(\d+:\d+\.\d+)\]\s+(.*)' , segment )
28- if match :
29- start_time = match .group (1 )
30- end_time = match .group (2 )
31- text = match .group (3 ).strip ()
32-
33- formatted_data ["en" ].append ({
34- "starttime" : start_time ,
35- "endtime" : end_time ,
36- "caption" : text
37- })
38-
39- return formatted_data
40-
41- except subprocess .CalledProcessError as e :
42- print (f"Error during transcription: { e .stderr } " )
43- return None
23+ print ("Running Whisper transcription inside the container..." )
4424
45- except Exception as e :
46- print (f"An unexpected error occurred: { e } " )
47- return None
25+ # Execute the Whisper command
26+ result = subprocess .run (whisper_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
4827
49- if __name__ == "__main__" :
50- audio_file = "randomvoice_16kHz.wav"
28+ # Handle command failure
29+ if result .returncode != 0 :
30+ raise Exception (f"Whisper failed with error:\n { result .stderr .decode ('utf-8' )} " )
5131
52- transcription = transcribe_audio_with_whisper (audio_file )
32+ # Check if the output JSON file was generated
33+ if not os .path .exists (json_output_path ):
34+ raise FileNotFoundError (f"Expected JSON output file not found: { json_output_path } " )
5335
54- if transcription :
55- print (json .dumps (transcription , indent = 4 ))
56- else :
57- print ("Transcription failed." )
36+ # Load the JSON transcription result
37+ with open (json_output_path , 'r' ) as json_file :
38+ transcription_result = json .load (json_file )
39+
40+ # Delete the JSON file after reading it
41+ os .remove (json_output_path )
42+ print (f"Deleted the JSON file: { json_output_path } " )
43+
44+ return transcription_result
45+
46+ # Example usage
47+ if __name__ == '__main__' :
48+ # Example media file path inside the container (the actual path will depend on where the file is located)
49+ audio_filepath = 'sharedVolume/recording0.wav' # Update this path as needed
50+
51+ try :
52+ transcription_result = transcribe_audio (audio_filepath )
53+ print ("Transcription Result:" , json .dumps (transcription_result , indent = 4 ))
54+ except Exception as e :
55+ print (f"Error: { str (e )} " )
0 commit comments