|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.speech; |
| 18 | + |
| 19 | +// [START speech_export_to_gcs] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata; |
| 23 | +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest; |
| 24 | +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse; |
| 25 | +import com.google.cloud.speech.v1p1beta1.RecognitionAudio; |
| 26 | +import com.google.cloud.speech.v1p1beta1.RecognitionConfig; |
| 27 | +import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding; |
| 28 | +import com.google.cloud.speech.v1p1beta1.SpeechClient; |
| 29 | +import com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +public class ExportToStorageBeta { |
| 35 | + |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav"; |
| 38 | + String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/"; |
| 39 | + String encoding = "LINEAR16"; // encoding of the audio |
| 40 | + int sampleRateHertz = 8000; |
| 41 | + String languageCode = "en-US"; // language code BCP-47_LANGUAGE_CODE_OF_AUDIO |
| 42 | + exportToStorage(inputUri, outputStorageUri, encoding, sampleRateHertz, languageCode); |
| 43 | + } |
| 44 | + |
| 45 | + // Exports the recognized output to specified GCS destination. |
| 46 | + public static void exportToStorage( |
| 47 | + String inputUri, String outputStorageUri, String encoding, int sampleRateHertz, |
| 48 | + String languageCode) |
| 49 | + throws IOException, ExecutionException, InterruptedException { |
| 50 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 51 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 52 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 53 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 54 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(inputUri).build(); |
| 55 | + |
| 56 | + AudioEncoding audioEncoding = AudioEncoding.valueOf(encoding); |
| 57 | + |
| 58 | + // Pass in the URI of the Cloud Storage bucket to hold the transcription |
| 59 | + TranscriptOutputConfig outputConfig = TranscriptOutputConfig.newBuilder() |
| 60 | + .setGcsUri(outputStorageUri).build(); |
| 61 | + |
| 62 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 63 | + .setEncoding(audioEncoding) |
| 64 | + .setSampleRateHertz(sampleRateHertz) |
| 65 | + .setLanguageCode(languageCode) |
| 66 | + .build(); |
| 67 | + |
| 68 | + LongRunningRecognizeRequest request = |
| 69 | + LongRunningRecognizeRequest.newBuilder() |
| 70 | + .setConfig(config) |
| 71 | + .setAudio(audio) |
| 72 | + .setOutputConfig(outputConfig) |
| 73 | + .build(); |
| 74 | + |
| 75 | + OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future = |
| 76 | + speechClient.longRunningRecognizeAsync(request); |
| 77 | + |
| 78 | + System.out.println("Waiting for operation to complete..."); |
| 79 | + LongRunningRecognizeResponse response = future.get(); |
| 80 | + |
| 81 | + System.out.println("Results saved to specified output Cloud Storage bucket."); |
| 82 | + |
| 83 | + String output = response.getResultsList().stream().map( |
| 84 | + result -> String.valueOf(result.getAlternatives(0).getTranscript())) |
| 85 | + .collect(Collectors.joining("\n")); |
| 86 | + System.out.printf("Transcription: %s", output); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +// [END speech_export_to_gcs] |
0 commit comments