|
21 | 21 | use Symfony\Component\Console\Application;
|
22 | 22 | use Symfony\Component\Console\Command\Command;
|
23 | 23 | use Symfony\Component\Console\Input\InputArgument;
|
| 24 | +use Symfony\Component\Console\Input\InputDefinition; |
24 | 25 | use Symfony\Component\Console\Input\InputInterface;
|
25 | 26 | use Symfony\Component\Console\Input\InputOption;
|
26 | 27 | use Symfony\Component\Console\Output\OutputInterface;
|
27 | 28 |
|
| 29 | +$inputDefinition = new InputDefinition([ |
| 30 | + new InputArgument('audio-file', InputArgument::REQUIRED, 'The audio file to transcribe'), |
| 31 | + new InputOption('encoding', null, InputOption::VALUE_REQUIRED, |
| 32 | + 'The encoding of the audio file. This is required if the encoding is ' . |
| 33 | + 'unable to be determined. ' |
| 34 | + ), |
| 35 | + new InputOption('language-code', null, InputOption::VALUE_REQUIRED, |
| 36 | + 'The language code for the language used in the source file. ', |
| 37 | + 'en-US' |
| 38 | + ), |
| 39 | + new InputOption('sample-rate', null, InputOption::VALUE_REQUIRED, |
| 40 | + 'The sample rate of the audio file in hertz. This is required ' . |
| 41 | + 'if the sample rate is unable to be determined. ' |
| 42 | + ), |
| 43 | + new InputOption('sample-rate', null, InputOption::VALUE_REQUIRED, |
| 44 | + 'The sample rate of the audio file in hertz. This is required ' . |
| 45 | + 'if the sample rate is unable to be determined. ' |
| 46 | + ), |
| 47 | +]); |
| 48 | + |
28 | 49 | $application = new Application('Cloud Speech');
|
29 | 50 | $application->add(new Command('transcribe'))
|
30 |
| - ->setDescription('Transcribe Audio using Google Cloud Speech API') |
| 51 | + ->setDefinition($inputDefinition) |
| 52 | + ->setDescription('Transcribe an audio file using Google Cloud Speech API') |
31 | 53 | ->setHelp(<<
|
32 |
| -The %command.name% command transcribes audio using the Google Cloud Speech API. |
| 54 | +The %command.name% command transcribes audio from a file using the |
| 55 | +Google Cloud Speech API. |
33 | 56 |
|
34 | 57 | php %command.full_name% audio_file.wav
|
35 | 58 |
|
36 | 59 | EOF
|
37 | 60 | )
|
38 |
| - ->addArgument( |
39 |
| - 'audio-file', |
40 |
| - InputArgument::REQUIRED, |
41 |
| - 'The audio file to transcribe' |
42 |
| - ) |
43 |
| - ->addOption( |
44 |
| - 'encoding', |
45 |
| - null, |
46 |
| - InputOption::VALUE_REQUIRED, |
47 |
| - 'The encoding of the audio file. This is required if the encoding is ' . |
48 |
| - 'unable to be determined. ' |
49 |
| - ) |
50 |
| - ->addOption( |
51 |
| - 'language-code', |
52 |
| - null, |
53 |
| - InputOption::VALUE_REQUIRED, |
54 |
| - 'The language code for the language used in the source file. ', |
55 |
| - 'en-US' |
56 |
| - ) |
57 |
| - ->addOption( |
58 |
| - 'sample-rate', |
59 |
| - null, |
60 |
| - InputOption::VALUE_REQUIRED, |
61 |
| - 'The sample rate of the audio file in hertz. This is required ' . |
62 |
| - 'if the sample rate is unable to be determined. ' |
| 61 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 62 | + $audioFile = $input->getArgument('audio-file'); |
| 63 | + $languageCode = $input->getOption('language-code'); |
| 64 | + transcribe_sync($audioFile, $languageCode, [ |
| 65 | + 'encoding' => $input->getOption('encoding'), |
| 66 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | +$application->add(new Command('transcribe-gcs')) |
| 71 | + ->setDefinition($inputDefinition) |
| 72 | + ->setDescription('Transcribe audio from a Storage Object using Google Cloud Speech API') |
| 73 | + ->setHelp(<< |
| 74 | +The %command.name% command transcribes audio from a Cloud Storage |
| 75 | +Object using the Google Cloud Speech API. |
| 76 | +
|
| 77 | +php %command.full_name% gs://my-bucket/audio_file.wav |
| 78 | +
|
| 79 | +EOF |
63 | 80 | )
|
64 |
| - ->addOption( |
65 |
| - 'async', |
66 |
| - null, |
67 |
| - InputOption::VALUE_NONE, |
68 |
| - 'Run the transcription asynchronously. ' |
| 81 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 82 | + $audioFile = $input->getArgument('audio-file'); |
| 83 | + $languageCode = $input->getOption('language-code'); |
| 84 | + if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) { |
| 85 | + throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]'); |
| 86 | + } |
| 87 | + list($bucketName, $objectName) = array_slice($matches, 1); |
| 88 | + transcribe_sync_gcs($bucketName, $objectName, $languageCode, [ |
| 89 | + 'encoding' => $input->getOption('encoding'), |
| 90 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | +$application->add(new Command('transcribe-words')) |
| 95 | + ->setDefinition($inputDefinition) |
| 96 | + ->setDescription('Transcribe an audio file and print word time offsets using Google Cloud Speech API') |
| 97 | + ->setHelp(<< |
| 98 | +The %command.name% command transcribes audio from a file using the |
| 99 | +Google Cloud Speech API and prints word time offsets. |
| 100 | +
|
| 101 | +php %command.full_name% audio_file.wav |
| 102 | +
|
| 103 | +EOF |
69 | 104 | )
|
70 |
| - ->addOption( |
71 |
| - 'stream', |
72 |
| - null, |
73 |
| - InputOption::VALUE_NONE, |
74 |
| - 'Stream the audio file.' |
| 105 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 106 | + $audioFile = $input->getArgument('audio-file'); |
| 107 | + $languageCode = $input->getOption('language-code'); |
| 108 | + transcribe_sync_words($audioFile, $languageCode, [ |
| 109 | + 'encoding' => $input->getOption('encoding'), |
| 110 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | +$application->add(new Command('transcribe-async')) |
| 115 | + ->setDefinition($inputDefinition) |
| 116 | + ->setDescription('Transcribe an audio file asynchronously using Google Cloud Speech API') |
| 117 | + ->setHelp(<< |
| 118 | +The %command.name% command transcribes audio from a file using the |
| 119 | +Google Cloud Speech API asynchronously. |
| 120 | +
|
| 121 | +php %command.full_name% audio_file.wav |
| 122 | +
|
| 123 | +EOF |
75 | 124 | )
|
76 | 125 | ->setCode(function (InputInterface $input, OutputInterface $output) {
|
77 |
| - $encoding = $input->getOption('encoding'); |
| 126 | + $audioFile = $input->getArgument('audio-file'); |
78 | 127 | $languageCode = $input->getOption('language-code');
|
79 |
| - $sampleRate = $input->getOption('sample-rate'); |
| 128 | + transcribe_async($audioFile, $languageCode, [ |
| 129 | + 'encoding' => $input->getOption('encoding'), |
| 130 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 131 | + ]); |
| 132 | + }); |
| 133 | + |
| 134 | +$application->add(new Command('transcribe-async-gcs')) |
| 135 | + ->setDefinition($inputDefinition) |
| 136 | + ->setDescription('Transcribe audio asynchronously from a Storage Object using Google Cloud Speech API') |
| 137 | + ->setHelp(<< |
| 138 | +The %command.name% command transcribes audio from a Cloud Storage |
| 139 | +object asynchronously using the Google Cloud Speech API. |
| 140 | +
|
| 141 | +php %command.full_name% gs://my-bucket/audio_file.wav |
| 142 | +
|
| 143 | +EOF |
| 144 | + ) |
| 145 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
80 | 146 | $audioFile = $input->getArgument('audio-file');
|
81 |
| - $options = [ |
82 |
| - 'encoding' => $encoding, |
83 |
| - 'languageCode' => $languageCode, |
84 |
| - 'sampleRateHertz' => $sampleRate, |
85 |
| - ]; |
86 |
| - if ($isGcs = preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) { |
87 |
| - list($bucketName, $objectName) = array_slice($matches, 1); |
88 |
| - } |
89 |
| - if ($isGcs) { |
90 |
| - if ($input->getOption('stream')) { |
91 |
| - throw new LogicException('Cannot stream from a bucket!'); |
92 |
| - } |
93 |
| - if ($input->getOption('async')) { |
94 |
| - transcribe_async_gcs($bucketName, $objectName, $languageCode, $options); |
95 |
| - } else { |
96 |
| - transcribe_sync_gcs($bucketName, $objectName, $languageCode, $options); |
97 |
| - } |
98 |
| - } else { |
99 |
| - if ($input->getOption('async')) { |
100 |
| - transcribe_async($audioFile, $languageCode, $options); |
101 |
| - } elseif ($input->getOption('stream')) { |
102 |
| - $encodingInt = constant("Google\Cloud\Speech\V1\RecognitionConfig_AudioEncoding::$encoding"); |
103 |
| - streaming_recognize($audioFile, $languageCode, $encodingInt, $sampleRate); |
104 |
| - } else { |
105 |
| - transcribe_sync($audioFile, $languageCode, $options); |
106 |
| - } |
| 147 | + $languageCode = $input->getOption('language-code'); |
| 148 | + if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) { |
| 149 | + throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]'); |
107 | 150 | }
|
| 151 | + list($bucketName, $objectName) = array_slice($matches, 1); |
| 152 | + transcribe_async_gcs($bucketName, $objectName, $languageCode, [ |
| 153 | + 'encoding' => $input->getOption('encoding'), |
| 154 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 155 | + ]); |
| 156 | + }); |
| 157 | + |
| 158 | +$application->add(new Command('transcribe-async-words')) |
| 159 | + ->setDefinition($inputDefinition) |
| 160 | + ->setDescription('Transcribe an audio file asynchronously and print word time offsets using Google Cloud Speech API') |
| 161 | + ->setHelp(<< |
| 162 | +The %command.name% command transcribes audio from a file using the |
| 163 | +Google Cloud Speech API asynchronously and prints word time offsets. |
| 164 | +
|
| 165 | +php %command.full_name% audio_file.wav |
| 166 | +
|
| 167 | +EOF |
| 168 | + ) |
| 169 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 170 | + $audioFile = $input->getArgument('audio-file'); |
| 171 | + $languageCode = $input->getOption('language-code'); |
| 172 | + transcribe_async_words($audioFile, $languageCode, [ |
| 173 | + 'encoding' => $input->getOption('encoding'), |
| 174 | + 'sampleRateHertz' => $input->getOption('sample-rate'), |
| 175 | + ]); |
| 176 | + }); |
| 177 | + |
| 178 | +$application->add(new Command('transcribe-stream')) |
| 179 | + ->setDefinition($inputDefinition) |
| 180 | + ->setDescription('Transcribe a stream of audio using Google Cloud Speech API') |
| 181 | + ->setHelp(<< |
| 182 | +The %command.name% command transcribes audio from a stream using |
| 183 | +the Google Cloud Speech API. |
| 184 | +
|
| 185 | +php %command.full_name% audio_file.wav |
| 186 | +
|
| 187 | +EOF |
| 188 | + ) |
| 189 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 190 | + streaming_recognize( |
| 191 | + $input->getArgument('audio-file'), |
| 192 | + $input->getOption('language-code'), |
| 193 | + $input->getOption('encoding'), |
| 194 | + $input->getOption('sample-rate') |
| 195 | + ); |
108 | 196 | });
|
109 | 197 |
|
110 | 198 | // for testing
|
|
0 commit comments