Skip to content
Navigation Menu
{{ message }}
forked from GoogleCloudPlatform/php-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeech.php
More file actions
253 lines (218 loc) · 9.81 KB
/
Copy pathspeech.php
File metadata and controls
253 lines (218 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Samples\Speech;
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
$inputDefinition = new InputDefinition([
new InputArgument('audio-file', InputArgument::REQUIRED, 'The audio file to transcribe'),
new InputOption('model', null, InputOption::VALUE_REQUIRED, 'The model to use'),
new InputOption('encoding', null, InputOption::VALUE_REQUIRED,
'The encoding of the audio file. This is required if the encoding is ' .
'unable to be determined. '
),
new InputOption('language-code', null, InputOption::VALUE_REQUIRED,
'The language code for the language used in the source file. ',
'en-US'
),
new InputOption('sample-rate', null, InputOption::VALUE_REQUIRED,
'The sample rate of the audio file in hertz. This is required ' .
'if the sample rate is unable to be determined. '
),
new InputOption('sample-rate', null, InputOption::VALUE_REQUIRED,
'The sample rate of the audio file in hertz. This is required ' .
'if the sample rate is unable to be determined. '
),
]);
$application = new Application('Cloud Speech');
$application->add(new Command('transcribe'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
transcribe_sync($audioFile, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-gcs'))
->setDefinition($inputDefinition)
->setDescription('Transcribe audio from a Storage Object using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a Cloud Storage
Object using the Google Cloud Speech API.
<info>php %command.full_name% gs://my-bucket/audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) {
throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]');
}
list($bucketName, $objectName) = array_slice($matches, 1);
transcribe_sync_gcs($bucketName, $objectName, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-words'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file and print word time offsets using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API and prints word time offsets.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
transcribe_sync_words($audioFile, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-model'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file, with selected model, using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with the
selected model, using the Google Cloud Speech API.
<info>php %command.full_name% audio_file.wav model_name</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$modelName = $input->getOption('model');
transcribe_model_selection($audioFile, $modelName);
});
$application->add(new Command('transcribe-enhanced'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file, with an enhanced model, using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with an enhanced
model, using the Google Cloud Speech API.
<info>php %command.full_name% audio_file.wav model_name</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$path = $input->getArgument('audio-file');
transcribe_enhanced_model($path);
});
$application->add(new Command('transcribe-punctuation'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file, with proper punctuation, using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with
proper punctuation, using the Google Cloud Speech API.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$path = $input->getArgument('audio-file');
transcribe_auto_punctuation($path);
});
$application->add(new Command('transcribe-async'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file asynchronously using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API asynchronously.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
transcribe_async($audioFile, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-async-gcs'))
->setDefinition($inputDefinition)
->setDescription('Transcribe audio asynchronously from a Storage Object using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a Cloud Storage
object asynchronously using the Google Cloud Speech API.
<info>php %command.full_name% gs://my-bucket/audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) {
throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]');
}
list($bucketName, $objectName) = array_slice($matches, 1);
transcribe_async_gcs($bucketName, $objectName, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-async-words'))
->setDefinition($inputDefinition)
->setDescription('Transcribe an audio file asynchronously and print word time offsets using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API asynchronously and prints word time offsets.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
$audioFile = $input->getArgument('audio-file');
$languageCode = $input->getOption('language-code');
transcribe_async_words($audioFile, $languageCode, [
'encoding' => $input->getOption('encoding'),
'sampleRateHertz' => $input->getOption('sample-rate'),
]);
});
$application->add(new Command('transcribe-stream'))
->setDefinition($inputDefinition)
->setDescription('Transcribe a stream of audio using Google Cloud Speech API')
->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a stream using
the Google Cloud Speech API.
<info>php %command.full_name% audio_file.wav</info>
EOF
)
->setCode(function (InputInterface $input, OutputInterface $output) {
streaming_recognize(
$input->getArgument('audio-file'),
$input->getOption('language-code'),
$input->getOption('encoding'),
$input->getOption('sample-rate')
);
});
// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
}
$application->run();
You can’t perform that action at this time.
