Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Über die Befehlszeile Audioinhalte aus dem Text erstellen
In diesem Dokument wird beschrieben, wie Sie eine Text-to-Speech-Anfrage über die Befehlszeile stellen. Weitere Informationen zu den grundlegenden Konzepten von Text-to-Speech finden Sie unter Grundlagen von Text-to-Speech.
Hinweis
Bevor Sie eine Anfrage an die Text-to-Speech API senden können, müssen Sie die folgenden Aktionen ausgeführt haben. Weitere Informationen finden Sie auf der Seite Vorbereitung.
Text-to-Speech in einem GCP-Projekt aktivieren.
Die Abrechnung muss für Text-to-Speech aktiviert sein.
Sie können Text in Audioinhalte umwandeln, wenn Sie eine HTTP-POST-Anfrage an den Endpunkt https://texttospeech.googleapis.com/v1/text:synthesize senden. Geben Sie dazu im Hauptteil des POST-Befehls im Konfigurationsabschnitt voice die Art der zu synthetisierenden Stimme an, im Abschnitt input im Feld text den zu synthetisierenden Text und im Abschnitt audioConfig die Art der zu erstellenden Audioinhalte.
Führen Sie unten in der Befehlszeile die REST-Anfrage aus, um mithilfe von Text-to-Speech Audioinhalte aus Text zu synthetisieren. Der Befehl ruft mit dem Befehl gcloud auth
application-default print-access-token ein Autorisierungstoken für die Anfrage ab.
Ersetzen Sie diese Werte in den folgenden Anfragedaten:
PROJECT_ID: Die alphanumerische ID Ihres Google Cloud -Projekts.
HTTP-Methode und URL:
POST https://texttospeech.googleapis.com/v1/text:synthesize
JSON-Text anfordern:
{
"input": {
"text": "Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."
},
"voice": {
"languageCode": "en-gb",
"name": "en-GB-Standard-A",
"ssmlGender": "FEMALE"
},
"audioConfig": {
"audioEncoding": "MP3"
}
}
Wenn Sie die Anfrage senden möchten, maximieren Sie eine der folgenden Optionen:
curl (Linux, macOS oder Cloud Shell)
Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:
Die JSON-Ausgabe für den REST-Befehl enthält die synthetisierten Audioinhalte im Base64-codierten Format. Kopieren Sie den Inhalt des Felds audioContent in eine neue Datei mit dem Namen synthesize-output-base64.txt. Die neue Datei sieht etwa so aus:
Decodieren Sie den Inhalt der Datei synthesize-output-base64.txt in eine neue Datei mit dem Namen synthesized-audio.mp3. Informationen zum Decodieren von Base64-codierten Inhalten finden Sie unter Base64-codierte Audioinhalte decodieren.
Linux
Kopieren Sie nur die Base64-codierten Inhalte in eine Textdatei.
Decodieren Sie die Datei, die den Quelltext enthält, mit dem Base64-Befehlszeilentool mit dem Flag -d:
Geben Sie den Inhalt von synthesized-audio.mp3 in einer Audioanwendung oder auf einem Audiogerät wieder. Sie können die Datei synthesized-audio.mp3 auch im Chrome-Browser öffnen, um die Audioinhalte abzuspielen. Gehen Sie hierfür zum Ordner mit der Datei, z. B. file://my_file_path/synthesized-audio.mp3.
Bereinigen
Löschen Sie das Projekt mit derGoogle Cloud console , wenn Sie es nicht benötigen. Damit vermeiden Sie unnötige Kosten für die Google Cloud Platform.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-11 (UTC)."],[],[],null,["# Quickstart: Create audio from text by using the command line\n\nCreate audio from text by using the command line\n================================================\n\nThis document walks you through the process of making a request to\nText-to-Speech using the command line. To learn more about the fundamental\nconcepts in Text-to-Speech, read\n[Text-to-Speech Basics](/text-to-speech/docs/basics).\n\nBefore you begin\n----------------\n\nBefore you can send a request to the Text-to-Speech API, you must have completed\nthe following actions. See the\n[before you begin](/text-to-speech/docs/before-you-begin) page for details.\n\n- Enable Text-to-Speech on a GCP project.\n- Make sure billing is enabled for Text-to-Speech.\n-\n [Install](/sdk/docs/install) the Google Cloud CLI, and then\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n After signing in,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\nSynthesize audio from text\n--------------------------\n\nYou can convert text to audio by making an HTTP POST request to the\n`https://texttospeech.googleapis.com/v1/text:synthesize` endpoint. In\nthe body of your POST command, specify the type of voice to synthesize in\nthe `voice` configuration section, specify the text to synthesize in the\n`text` field of the `input` section, and specify the type of audio to create\nin the `audioConfig` section.\n\n1. Execute the REST request below at the command line to synthesize audio from\n text using Text-to-Speech. The command uses the `gcloud auth\n application-default print-access-token` command to retrieve an authorization\n token for the request.\n\n\n Before using any of the request data,\n make the following replacements:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the alphanumeric ID of your Google Cloud project.\n\n\n HTTP method and URL:\n\n ```\n POST https://texttospeech.googleapis.com/v1/text:synthesize\n ```\n\n\n Request JSON body:\n\n ```\n {\n \"input\": {\n \"text\": \"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\"\n },\n \"voice\": {\n \"languageCode\": \"en-gb\",\n \"name\": \"en-GB-Standard-A\",\n \"ssmlGender\": \"FEMALE\"\n },\n \"audioConfig\": {\n \"audioEncoding\": \"MP3\"\n }\n }\n ```\n\n To send your request, expand one of these options:\n\n #### curl (Linux, macOS, or Cloud Shell)\n\n | **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) , or by using [Cloud Shell](/shell/docs), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\n Save the request body in a file named `request.json`,\n and execute the following command:\n\n ```\n curl -X POST \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n -H \"x-goog-user-project: PROJECT_ID\" \\\n -H \"Content-Type: application/json; charset=utf-8\" \\\n -d @request.json \\\n \"https://texttospeech.googleapis.com/v1/text:synthesize\"\n ```\n\n #### PowerShell (Windows)\n\n | **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\n Save the request body in a file named `request.json`,\n and execute the following command:\n\n ```\n $cred = gcloud auth print-access-token\n $headers = @{ \"Authorization\" = \"Bearer $cred\"; \"x-goog-user-project\" = \"PROJECT_ID\" }\n\n Invoke-WebRequest `\n -Method POST `\n -Headers $headers `\n -ContentType: \"application/json; charset=utf-8\" `\n -InFile request.json `\n -Uri \"https://texttospeech.googleapis.com/v1/text:synthesize\" | Select-Object -Expand Content\n ```\n\n You should receive a JSON response similar to the following:\n\n ```\n {\n \"audioContent\": \"//NExAASCCIIAAhEAGAAEMW4kAYPnwwIKw/BBTpwTvB+IAxIfghUfW..\"\n }\n ```\n\n \u003cbr /\u003e\n\n2. The JSON output for the REST command contains the synthesized audio in\n base64-encoded format. Copy the contents of the `audioContent` field into\n a new file named `synthesize-output-base64.txt`. Your new file will look something\n like the following:\n\n ```\n //NExAARqoIIAAhEuWAAAGNmBGMY4EBcxvABAXBPmPIAF//yAuh9Tn5CEap3/o\n ...\n VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n ```\n3. Decode the contents of the `synthesize-output-base64.txt` file into a new file\n named `synthesized-audio.mp3`. For information\n on decoding base64, see [Decoding Base64-Encoded Audio Content](/text-to-speech/docs/base64-decoding).\n\n ### Linux\n\n 1. Copy only the base-64 encoded content into a text file.\n\n 2. Decode the source text file using the base64 command line tool\n by using the `-d` flag:\n\n ```bash\n $ base64 SOURCE_BASE64_TEXT_FILE -d \u003e DESTINATION_AUDIO_FILE\n ```\n\n ### Mac OSX\n\n 1. Copy only the base-64 encoded content into a text file.\n\n 2. Decode the source text file using the base64 command line tool:\n\n ```bash\n $ base64 --decode SOURCE_BASE64_TEXT_FILE \u003e DESTINATION_AUDIO_FILE\n ```\n\n ### Windows\n\n 1. Copy only the base-64 encoded content into a text file.\n\n 2. Decode the source text file using the\n [`certutil`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/certutil) command.\n\n ```bash\n certutil -decode SOURCE_BASE64_TEXT_FILE DESTINATION_AUDIO_FILE\n ```\n4. Play the contents of `synthesized-audio.mp3` in an audio application or on\n an audio device. You can also open the `synthesized-audio.mp3` in the Chrome\n browser to play the audio by navigating to the folder that contains\n the file, for example `file://my_file_path/synthesized-audio.mp3`\n\nClean up\n--------\n\nTo avoid unnecessary Google Cloud Platform charges, use the\n[Google Cloud console](https://console.cloud.google.com/) to delete your project if you do not need it.\n\nWhat's next\n-----------\n\n\n- Learn more about Cloud Text-to-Speech by reading the [basics](/text-to-speech/docs/basics).\n- Review the list of [available voices](/text-to-speech/docs/voices) you can use for synthetic speech.\n\n\u003cbr /\u003e"]]