@@ -38,8 +38,18 @@ def convert_video_to_wav(input_filepath, offset=None):
3838 print ("Exception during conversion:" + str (e ))
3939 raise e
4040
41- def transcribe_audio (media_filepath ):
41+ def transcribe_audio (media_filepath , testing = False ):
42+ if testing :
43+ json_output_path = f"/PythonRpcServer/transcribe_hellohellohello.wav.json"
44+ with open (json_output_path , 'r' ) as json_file :
45+ transcription_result = json .load (json_file )
46+
47+ # Print the transcription result (testing purpose)
48+ print ("Transcription result:" )
49+ print (json .dumps (transcription_result , indent = 4 ))
4250
51+ return transcription_result
52+
4353 if media_filepath == 'TEST-transcribe_example_result' :
4454 result_json_file = 'transcribe_exampleffmp_result.json'
4555 with open (result_json_file , 'r' ) as json_file :
@@ -51,8 +61,10 @@ def transcribe_audio(media_filepath):
5161 raise FileNotFoundError (f"Media file not found: { media_filepath } " )
5262
5363 # convert video to wav if needed
64+ wav_created = False # Track if WAV was created
5465 if not media_filepath .endswith ('.wav' ):
5566 media_filepath , _ = convert_video_to_wav (media_filepath )
67+ wav_created = True # WAV file was created
5668
5769
5870 # Path to the output JSON file that Whisper will generate
@@ -87,26 +99,27 @@ def transcribe_audio(media_filepath):
8799 transcription_result = json .load (json_file )
88100
89101 # Print the transcription result (testing purpose)
90- # print("Transcription result:")
91- # print(json.dumps(transcription_result, indent=4))
102+ print ("Transcription result:" )
103+ print (json .dumps (transcription_result , indent = 4 ))
92104
93105 # Delete the JSON file after reading it
94106 os .remove (json_output_path )
95107 print (f"Deleted the JSON file: { json_output_path } " )
96108
109+ if wav_created :
110+ try :
111+ os .remove (media_filepath )
112+ print (f"Deleted the WAV file: { media_filepath } " )
113+ except Exception as e :
114+ print (f"Error deleting WAV file: { str (e )} " )
115+
97116 return transcription_result
98117
99118# Example usage
100119if __name__ == '__main__' :
101120 # Example media file path inside the container (the actual path will depend on where the file is located)
102- import sys
103- if len (sys .argv ) > 1 :
104- audio_filepath = sys .argv [1 ]
105- else :
106- audio_filepath = 'sharedVolume/recording0.wav' # Update this path as needed
107-
108- try :
109- transcription_result = transcribe_audio (audio_filepath )
110- print ("Transcription Result:" , json .dumps (transcription_result , indent = 4 ))
111- except Exception as e :
112- print (f"Error: { str (e )} " )
121+ json_output_path = f"/PythonRpcServer/transcribe_hellohellohello.wav.json"
122+ with open (json_output_path , 'r' ) as json_file :
123+ transcription_result = json .load (json_file )
124+
125+ print ("Transcription Result:" , json .dumps (transcription_result , indent = 4 ))
0 commit comments