2727import com .google .cloud .speech .v1p1beta1 .RecognitionConfig .AudioEncoding ;
2828import com .google .cloud .speech .v1p1beta1 .SpeechClient ;
2929import com .google .cloud .speech .v1p1beta1 .TranscriptOutputConfig ;
30+ import com .google .cloud .storage .Blob ;
31+ import com .google .cloud .storage .BlobId ;
32+ import com .google .cloud .storage .Storage ;
33+ import com .google .cloud .storage .StorageOptions ;
34+ import com .google .protobuf .util .JsonFormat ;
3035import java .io .IOException ;
3136import java .util .concurrent .ExecutionException ;
3237import java .util .stream .Collectors ;
38+ import org .json .JSONObject ;
3339
3440public class ExportToStorageBeta {
3541
3642 public static void main (String [] args ) throws Exception {
3743 String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav" ;
3844 String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/" ;
45+ String objectName = "YOUR_OBJECT_NAME" ;
46+ String bucketName = "YOUR_BUCKET_ID" ;
3947 String encoding = "LINEAR16" ; // encoding of the audio
4048 int sampleRateHertz = 8000 ;
4149 String languageCode = "en-US" ; // language code BCP-47_LANGUAGE_CODE_OF_AUDIO
42- exportToStorage (inputUri , outputStorageUri , encoding , sampleRateHertz , languageCode );
50+ exportToStorage (
51+ inputUri ,
52+ outputStorageUri ,
53+ encoding ,
54+ sampleRateHertz ,
55+ languageCode ,
56+ bucketName ,
57+ objectName );
4358 }
4459
4560 // Exports the recognized output to specified GCS destination.
@@ -48,7 +63,9 @@ public static void exportToStorage(
4863 String outputStorageUri ,
4964 String encoding ,
5065 int sampleRateHertz ,
51- String languageCode )
66+ String languageCode ,
67+ String bucketName ,
68+ String objectName )
5269 throws IOException , ExecutionException , InterruptedException {
5370 // Initialize client that will be used to send requests. This client only needs to be created
5471 // once, and can be reused for multiple requests. After completing all of your requests, call
@@ -58,6 +75,9 @@ public static void exportToStorage(
5875
5976 AudioEncoding audioEncoding = AudioEncoding .valueOf (encoding );
6077
78+ // Instantiates a client
79+ Storage storage = StorageOptions .getDefaultInstance ().getService ();
80+
6181 // Pass in the URI of the Cloud Storage bucket to hold the transcription
6282 TranscriptOutputConfig outputConfig =
6383 TranscriptOutputConfig .newBuilder ().setGcsUri (outputStorageUri ).build ();
@@ -80,12 +100,39 @@ public static void exportToStorage(
80100 speechClient .longRunningRecognizeAsync (request );
81101
82102 System .out .println ("Waiting for operation to complete..." );
83- LongRunningRecognizeResponse response = future .get ();
103+ future .get ();
104+
105+ // Get blob given bucket and object name
106+ Blob blob = storage .get (BlobId .of (bucketName , objectName ));
107+
108+ // Extract byte contents from blob
109+ byte [] bytes = blob .getContent ();
110+
111+ // Get decoded representation
112+ String decoded = new String (bytes , "UTF-8" );
113+
114+ // Create json object
115+ JSONObject jsonObject = new JSONObject (decoded );
116+
117+ // Get json string
118+ String json = jsonObject .toString ();
119+
120+ // Specefy the proto type message
121+ LongRunningRecognizeResponse .Builder builder = LongRunningRecognizeResponse .newBuilder ();
122+
123+ // Construct a parser
124+ JsonFormat .Parser parser = JsonFormat .parser ().ignoringUnknownFields ();
125+
126+ // Parses from JSON into a protobuf message.
127+ parser .merge (json , builder );
128+
129+ // Get the converted values
130+ LongRunningRecognizeResponse storageResponse = builder .build ();
84131
85132 System .out .println ("Results saved to specified output Cloud Storage bucket." );
86133
87134 String output =
88- response .getResultsList ().stream ()
135+ storageResponse .getResultsList ().stream ()
89136 .map (result -> String .valueOf (result .getAlternatives (0 ).getTranscript ()))
90137 .collect (Collectors .joining ("\n " ));
91138 System .out .printf ("Transcription: %s" , output );
0 commit comments