Cloud Storage に保存されている音声ファイルを文字に変換します(複数のチャンネルを含む)。
コードサンプル
Java
/**
* Transcribe a remote audio file with multi-channel recognition
*
* @param gcsUri the path to the audio file
*/
public static void transcribeMultiChannelGcs(String gcsUri) throws Exception {
try (SpeechClient speechClient = SpeechClient.create()) {
// Configure request to enable multiple channels
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(44100)
.setAudioChannelCount(2)
.setEnableSeparateRecognitionPerChannel(true)
.build();
// Set the remote path for the audio file
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
// Use non-blocking call for getting file transcription
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
speechClient.longRunningRecognizeAsync(config, audio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
Thread.sleep(10000);
}
// Just print the first result here.
for (SpeechRecognitionResult result : response.get().getResultsList()) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
// Print out the result
System.out.printf("Transcript : %s\n", alternative.getTranscript());
System.out.printf("Channel Tag : %s\n\n", result.getChannelTag());
}
}
}
Node.js
const speech = require('@google-cloud/speech').v1p1beta1;
// Creates a client
const client = new speech.SpeechClient();
const config = {
encoding: 'LINEAR16',
languageCode: 'en-US',
audioChannelCount: 2,
enableSeparateRecognitionPerChannel: true,
};
const audio = {
uri: gcsUri,
};
const request = {
config: config,
audio: audio,
};
const [response] = await client.recognize(request);
const transcription = response.results
.map(
result =>
` Channel Tag: ${result.channelTag} ${result.alternatives[0].transcript}`
)
.join('\n');
console.log(`Transcription: \n${transcription}`);
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。