11#!/usr/bin/env python
22from __future__ import unicode_literals
33
4+ from google .protobuf .json_format import MessageToJson
45from google .cloud import speech
56from google .cloud .speech import enums
67from google .cloud .speech import types
910import logging
1011import subprocess
1112import sys
13+ import IPython
1214
1315
1416logging .basicConfig (level = logging .INFO , format = '%(message)s' )
1820
1921parser = argparse .ArgumentParser (description = 'Convert speech audio to text using Google Speech API' )
2022parser .add_argument ('in_filename' , help = 'Input filename (`-` for stdin)' )
23+ parser .add_argument ('--timing' , action = 'store_true' , help = 'Include timing info' )
2124
2225
2326def decode_audio (in_filename , ** input_kwargs ):
@@ -38,25 +41,24 @@ def decode_audio(in_filename, **input_kwargs):
3841 return out [0 ]
3942
4043
41- def get_transcripts (audio_data ):
44+ def get_transcripts (audio_data , include_timing_info = False ):
4245 client = speech .SpeechClient ()
4346 audio = types .RecognitionAudio (content = audio_data )
4447 config = types .RecognitionConfig (
4548 encoding = enums .RecognitionConfig .AudioEncoding .LINEAR16 ,
4649 sample_rate_hertz = 16000 ,
47- language_code = 'en-US'
50+ language_code = 'en-US' ,
51+ enable_word_time_offsets = include_timing_info ,
4852 )
49- response = client .recognize (config , audio )
50- return [result .alternatives [0 ].transcript for result in response .results ]
53+ return client .recognize (config , audio )
5154
5255
53- def transcribe (in_filename ):
56+ def transcribe (in_filename , include_timing_info = False ):
5457 audio_data = decode_audio (in_filename )
55- transcripts = get_transcripts (audio_data )
56- for transcript in transcripts :
57- print (repr (transcript .encode ('utf-8' )))
58+ response = get_transcripts (audio_data , include_timing_info )
59+ print (MessageToJson (response , sort_keys = True ))
5860
5961
6062if __name__ == '__main__' :
6163 args = parser .parse_args ()
62- transcribe (args .in_filename )
64+ transcribe (args .in_filename , args . timing )
0 commit comments