Media Translation では、音声ファイルや音声ストリームを別の言語のテキストに翻訳します。このページでは、Media Translation クライアント ライブラリを使用してストリーミング オーディオをテキストに翻訳する方法を示すコードサンプルを提供します。
プロジェクトを設定する
Media Translation を使用するには、Google Cloud プロジェクトを設定し、そのプロジェクトで Media Translation API を有効にする必要があります。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Media Translation API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Media Translation API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 希望する言語のクライアント ライブラリをインストールします。
音声を翻訳する
以下のサンプルコードは、最長 5 分間の音声を含むファイルまたはライブマイクから音声を翻訳する方法を示しています。認識の精度が最高となるように音声データを提供する方法についてのおすすめは、ベスト プラクティスをご覧ください。
主要な手順は、音声ソースに関係なく同じです。
Media Translation にリクエストを送信するために使用する
SpeechTranslationServiceClient
を初期化します。同じクライアントは複数のリクエストに再利用できます。
音声の処理方法を指定する
StreamingTranslateSpeechConfig
リクエスト オブジェクトを作成します。StreamingTranslateSpeechConfig
オブジェクトは、オーディオ ソース ファイルに関する情報を提供するTranslateSpeechConfig
オブジェクトと、話し手が会話を中断したときに Media Translation による翻訳を続行するかどうかを指定するsingle_utterance
プロパティで構成されます。TranslateSpeechConfig
オブジェクトは、音声ソースの技術仕様(エンコード、サンプルレートなど)を提供し、翻訳のソース言語とターゲット言語(BCP-47 言語コードを使用)を設定します。また、音声文字変換にどの翻訳モデルを Media Translation で使用するかを定義します。StreamingTranslateSpeechRequest
リクエスト オブジェクトのシーケンスを送信します。翻訳する音声ファイルごとに一連のリクエストを送信します。最初のリクエストはリクエストの
StreamingTranslateSpeechConfig
オブジェクトを提供し、後続のリクエストはストリーミングの音声コンテンツを提供します。StreamingTranslateSpeechResult
レスポンス オブジェクトを受け取ります。text_translation_result.is_final
の値がfalse
であるレスポンスを受信した場合は、最新の翻訳結果によって前の結果が上書きされます。Media Translation の最終結果があるときは、
text_translation_result.is_final
フィールドはtrue
に設定され、その後に受信した翻訳結果は以前の結果に追加されます。(この例では、前の結果は上書きされません)。完了した翻訳を出力し、音声文字変換と対応する音声の次の部分のために新しいセクションを開始できます。StreamingTranslateSpeechConfig
リクエスト オブジェクトでsingle_utterance
フィールドが true に設定されている場合、話し手が会話をやめたときに、Media Translation はレスポンス内のspeech_event_type
に対するEND_OF_SINGLE_UTTERANCE
イベントを返します。クライアントはリクエストの送信を停止しますが、翻訳が終了するまでレスポンスを受信し続けます。ストリーミングには 5 分間の制限があります。この制限を超えると、OUT_OF_RANGE エラーが返されます。
コードサンプル
音声ファイルからの音声の翻訳
Java
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Java API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Node.js
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Node.js API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Python
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Python API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
マイクからの音声の翻訳
Java
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Java API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Node.js
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Node.js API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Python
Media Translation 用のクライアント ライブラリをインストールして使用する方法については、Media Translation のクライアント ライブラリをご覧ください。詳細については、Media Translation Python API のリファレンス ドキュメントをご覧ください。
Media Translation で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。