Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Buat audio dari teks menggunakan command line
Dokumen ini akan memandu Anda melalui proses permintaan ke Text-to-Speech menggunakan command line. Untuk mempelajari selengkapnya tentang konsep dasar dalam Text-to-Speech, baca Dasar-Dasar Text-to-Speech.
Sebelum memulai
Sebelum dapat mengirim permintaan ke Text-to-Speech API, Anda harus sudah menyelesaikan tindakan berikut. Lihat halaman sebelum memulai untuk mengetahui detailnya.
Aktifkan Text-to-Speech di project GCP.
Pastikan penagihan diaktifkan untuk Text-to-Speech.
Anda dapat mengonversi teks menjadi audio dengan membuat permintaan HTTP POST ke endpoint https://texttospeech.googleapis.com/v1/text:synthesize. Dalam
isi perintah POST, tentukan jenis suara yang akan disintesis
di bagian konfigurasi, voice tentukan teks yang akan disintesis di
kolom text dari input lalu tentukan jenis audio yang akan dibuat
di bagian audioConfig.
Jalankan permintaan REST di bawah ini pada command line untuk melakukan sintesis pada audio dari
teks menggunakan Text-to-Speech. Perintah tersebut menggunakan perintah gcloud auth
application-default print-access-token untuk mengambil token otorisasi untuk permintaan tersebut.
Sebelum menggunakan salah satu data permintaan,
lakukan penggantian berikut:
PROJECT_ID: ID alfanumerik Google Cloud project Anda.
Metode HTTP dan URL:
POST https://texttospeech.googleapis.com/v1/text:synthesize
Meminta isi JSON:
{
"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"
}
}
Untuk mengirim permintaan Anda, perluas salah satu opsi berikut:
curl (Linux, macOS, atau Cloud Shell)
Simpan isi permintaan dalam file bernama request.json,
dan jalankan perintah berikut:
Output JSON untuk perintah REST berisi audio yang disintesis dalam
format yang dienkode base64. Salin konten kolom audioContent ke
file baru bernama synthesize-output-base64.txt. File baru Anda akan terlihat seperti berikut:
Dekode konten file synthesize-output-base64.txt menjadi file baru
bernama synthesized-audio.mp3. Untuk mengetahui informasi
tentang mendekode base64, lihat Mendekode Konten Audio yang Dienkode Base64.
Linux
Hanya salin konten berenkode base-64 ke dalam file teks.
Dekode file teks sumber menggunakan alat command line base64
menggunakan flag -d:
Memutar konten synthesized-audio.mp3 dalam aplikasi audio atau di
perangkat audio. Anda juga dapat membuka synthesized-audio.mp3 di browser
Chrome untuk memutar audio dengan membuka folder yang berisi
file tersebut, misalnya file://my_file_path/synthesized-audio.mp3
Pembersihan
Untuk menghindari tagihan Google Cloud Platform yang tidak perlu, gunakan
Google Cloud console untuk menghapus project Anda jika tidak lagi diperlukan.
Langkah berikutnya
Pelajari Cloud Text-to-Speech lebih lanjut dengan membaca dasar-dasarnya.
Tinjau daftar suara yang tersedia yang dapat Anda gunakan untuk ucapan sintetis.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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"]]