27
27
import com .google .cloud .speech .v1p1beta1 .RecognitionConfig .AudioEncoding ;
28
28
import com .google .cloud .speech .v1p1beta1 .SpeechClient ;
29
29
import 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 ;
30
35
import java .io .IOException ;
31
36
import java .util .concurrent .ExecutionException ;
32
37
import java .util .stream .Collectors ;
38
+ import org .json .JSONObject ;
33
39
34
40
public class ExportToStorageBeta {
35
41
36
42
public static void main (String [] args ) throws Exception {
37
43
String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav" ;
38
44
String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/" ;
45
+ String objectName = "YOUR_OBJECT_NAME" ;
46
+ String bucketName = "YOUR_BUCKET_ID" ;
39
47
String encoding = "LINEAR16" ; // encoding of the audio
40
48
int sampleRateHertz = 8000 ;
41
49
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 );
43
58
}
44
59
45
60
// Exports the recognized output to specified GCS destination.
@@ -48,7 +63,9 @@ public static void exportToStorage(
48
63
String outputStorageUri ,
49
64
String encoding ,
50
65
int sampleRateHertz ,
51
- String languageCode )
66
+ String languageCode ,
67
+ String bucketName ,
68
+ String objectName )
52
69
throws IOException , ExecutionException , InterruptedException {
53
70
// Initialize client that will be used to send requests. This client only needs to be created
54
71
// once, and can be reused for multiple requests. After completing all of your requests, call
@@ -58,6 +75,9 @@ public static void exportToStorage(
58
75
59
76
AudioEncoding audioEncoding = AudioEncoding .valueOf (encoding );
60
77
78
+ // Instantiates a client
79
+ Storage storage = StorageOptions .getDefaultInstance ().getService ();
80
+
61
81
// Pass in the URI of the Cloud Storage bucket to hold the transcription
62
82
TranscriptOutputConfig outputConfig =
63
83
TranscriptOutputConfig .newBuilder ().setGcsUri (outputStorageUri ).build ();
@@ -80,12 +100,39 @@ public static void exportToStorage(
80
100
speechClient .longRunningRecognizeAsync (request );
81
101
82
102
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 ();
84
131
85
132
System .out .println ("Results saved to specified output Cloud Storage bucket." );
86
133
87
134
String output =
88
- response .getResultsList ().stream ()
135
+ storageResponse .getResultsList ().stream ()
89
136
.map (result -> String .valueOf (result .getAlternatives (0 ).getTranscript ()))
90
137
.collect (Collectors .joining ("\n " ));
91
138
System .out .printf ("Transcription: %s" , output );
0 commit comments