29
29
# [END import_libraries]
30
30
31
31
32
- def transcribe_file (speech_file ):
32
+ def transcribe_file (speech_file , encoding , sample_rate_hertz , language ):
33
33
"""Transcribe the given audio file."""
34
34
from google .cloud import speech
35
35
speech_client = speech .Client ()
@@ -38,29 +38,28 @@ def transcribe_file(speech_file):
38
38
content = audio_file .read ()
39
39
audio_sample = speech_client .sample (
40
40
content = content ,
41
- source_uri = None ,
42
- encoding = 'LINEAR16' ,
43
- sample_rate_hertz = 16000 )
41
+ encoding = encoding ,
42
+ sample_rate_hertz = sample_rate_hertz )
44
43
45
44
start = time .time ()
46
- alternatives = audio_sample .recognize ('ja-JP' )
45
+ alternatives = audio_sample .recognize (language )
47
46
print ('Runtime: %s' % (time .time () - start ))
48
47
for alternative in alternatives :
49
48
print (u'Transcript: {}' .format (alternative .transcript ))
50
49
51
50
52
- def transcribe_gcs (gcs_uri ):
51
+ def transcribe_gcs (gcs_uri , encoding , sample_rate_hertz , language ):
53
52
"""Transcribes the audio file specified by the gcs_uri."""
54
53
from google .cloud import speech
55
54
speech_client = speech .Client ()
56
55
57
56
audio_sample = speech_client .sample (
58
57
content = None ,
59
58
source_uri = gcs_uri ,
60
- encoding = 'FLAC' ,
61
- sample_rate_hertz = 16000 )
59
+ encoding = encoding ,
60
+ sample_rate_hertz = sample_rate_hertz )
62
61
63
- alternatives = audio_sample .recognize ('en-US' )
62
+ alternatives = audio_sample .recognize (language )
64
63
for alternative in alternatives :
65
64
print ('Transcript: {}' .format (alternative .transcript ))
66
65
@@ -71,8 +70,11 @@ def transcribe_gcs(gcs_uri):
71
70
formatter_class = argparse .RawDescriptionHelpFormatter )
72
71
parser .add_argument (
73
72
'path' , help = 'File or GCS path for audio file to be recognized' )
73
+ parser .add_argument ('--encoding' , default = 'LINEAR16' )
74
+ parser .add_argument ('--sample_rate' , default = 16000 , type = int )
75
+ parser .add_argument ('--language' , default = 'en-US' )
74
76
args = parser .parse_args ()
75
77
if args .path .startswith ('gs://' ):
76
- transcribe_gcs (args .path )
78
+ transcribe_gcs (args .path , args . encoding , args . sample_rate , args . language )
77
79
else :
78
- transcribe_file (args .path )
80
+ transcribe_file (args .path , args . encoding , args . sample_rate , args . language )
0 commit comments