Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Saat mengirim data audio ke Speech-to-Text API, Anda dapat mengirim data secara langsung (dalam kolom content permintaan) atau buat API melakukan pengenalan dari jarak jauh pada data yang disimpan di bucket Cloud Storage.
Anda dapat mengirim data secara langsung di kolom content untuk
pengenalan sinkronhanya jika data audio Anda maksimum 60 detik dan 10 MB. Setiap data
audio di kolom content harus dalam format base64. Halaman ini menjelaskan cara
mengonversi audio dari file biner ke data berenkode base64.
Jika data audio Anda melebihi 60 detik atau 10 MB, data tersebut harus disimpan di bucket Cloud Storage agar dapat dikirim untuk pengenalan. Anda dapat menganalisisnya
secara asinkron tanpa mengonversinya ke format base64. Lihat
dokumentasi pengenalan asinkron
untuk mengetahui detailnya.
Menggunakan command line
Dalam permintaan gRPC, Anda cukup menulis data biner secara langsung;
tetapi JSON digunakan saat membuat permintaan REST. JSON
adalah format teks yang tidak mendukung data biner secara langsung, sehingga Anda harus
mengonversi data biner tersebut menjadi teks menggunakan
encoding Base64.
Sebagian besar lingkungan pengembangan berisi utilitas base64 native untuk
mengenkode biner ke dalam data teks ASCII. Untuk mengenkode file:
Linux
Enkode file menggunakan alat command line base64, yang memastikan untuk
mencegah penggabungan baris menggunakan flag -w 0:
base64 INPUT_FILE -w 0 > OUTPUT_FILE
macOS
Enkode file menggunakan alat command line base64:
base64 -i INPUT_FILE -o OUTPUT_FILE
Windows
Enkode file menggunakan alat Base64.exe:
Base64.exe -e INPUT_FILE > OUTPUT_FILE
PowerShell
Enkode file menggunakan metode Convert.ToBase64String:
Menyematkan data biner ke dalam permintaan melalui editor teks bukanlah hal
yang diinginkan atau praktis. Dalam praktiknya, Anda akan menyematkan file berenkode base64
dalam kode klien. Semua bahasa pemrograman yang didukung memiliki mekanisme bawaan
untuk konten encoding base64.
Python
Di Python, base64 mengenkode file audio sebagai berikut:
# Import the base64 encoding library.importbase64# Pass the audio data to an encoding function.defencode_audio(audio):audio_content=audio.read()returnbase64.b64encode(audio_content)
Node.js
Di Node.js, base64 mengenkode file audio sebagai berikut, dengan audioFile
adalah jalur ke file audio terenkode.
Di Java, gunakan metode statis encodeBase64 dalam
org.apache.commons.codec.binary.Base64 untuk mengenkode file biner ke base64:
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the speech.byte[]encodedAudio=Base64.encodeBase64(audio.getBytes());
[[["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-18 UTC."],[],[],null,["# Base64 encoding audio content\n\nWhen you send audio data to the Speech-to-Text API you can either send the\ndata directly (within the request's\n[`content`](/speech-to-text/docs/reference/rest/v1/RecognitionAudio) field) or\nhave the API perform recognition remotely on data stored in a Cloud Storage bucket.\nYou can send data directly in the `content` field for\n[synchronous recognition](/speech-to-text/docs/sync-recognize#performing_synchronous_speech_recognition_on_a_local_file)\n**only** if your audio data is a maximum of 60 seconds and 10 MB. Any audio\ndata in the `content` field must be in base64 format. This page describes how to\nconvert audio from a binary file to base64-encoded data.\n\nIf your audio data exceeds 60 seconds or 10 MB, it must be stored in a\nCloud Storage bucket in order to be sent for recognition. You can analyze\nit asynchronously without converting it to base64 format. See the\n[asynchronous recognition documentation](/speech-to-text/docs/async-recognize)\nfor details.\n\nUsing the command line\n----------------------\n\nWithin a gRPC request, you can simply write binary data out directly;\nhowever, JSON is used when making a REST request. JSON\nis a text format that does not directly support binary data, so you will need to\nconvert such binary data into text using\n[Base64](https://en.wikipedia.org/wiki/Base64) encoding.\n\nMost development environments contain a native `base64` utility to\nencode a binary into ASCII text data. To encode a file: \n\n### Linux\n\nEncode the file using the `base64` command line tool, making sure to\nprevent line-wrapping by using the `-w 0` flag: \n\n```\nbase64 INPUT_FILE -w 0 \u003e OUTPUT_FILE\n```\n\n### macOS\n\nEncode the file using the `base64` command line tool: \n\n```\nbase64 -i INPUT_FILE -o OUTPUT_FILE\n```\n\n### Windows\n\nEncode the file using the `Base64.exe` tool: \n\n```\nBase64.exe -e INPUT_FILE \u003e OUTPUT_FILE\n```\n\n### PowerShell\n\nEncode the file using the `Convert.ToBase64String` method: \n\n```\n[Convert]::ToBase64String([IO.File]::ReadAllBytes(\"./INPUT_FILE\")) \u003e OUTPUT_FILE\n```\n\nCreate a JSON request file, inlining the base64-encoded data: \n\n### JSON\n\n\n```json\n{\n \"config\": {\n \"encoding\": \"FLAC\",\n \"sampleRateHertz\": 16000,\n \"languageCode\": \"en-US\"\n },\n \"audio\": {\n \"content\": \"ZkxhQwAAACIQABAAAAUJABtAA+gA8AB+W8FZndQvQAyjv...\"\n }\n}\n```\n\n\u003cbr /\u003e\n\nUsing client libraries\n----------------------\n\nEmbedding binary data into requests through text editors is neither\ndesirable or practical. In practice, you will be embedding base64 encoded files\nwithin client code. All supported programming languages have built-in mechanisms\nfor base64 encoding content.\n\n\n### Python\n\nIn Python, base64 encode audio files as follows: \n\n # Import the base64 encoding library.\n import base64\n\n # Pass the audio data to an encoding function.\n def encode_audio(audio):\n audio_content = audio.read()\n return base64.b64encode(audio_content)\n\n### Node.js\n\nIn Node.js, base64 encode audio files as follows, where `audioFile`\nis the path to the audio-encoded file. \n\n const fs = require('fs');\n const content = fs.readFileSync(audioFile).toString('base64');\n\n### Java\n\nIn Java, use the `encodeBase64` static method within\n`org.apache.commons.codec.binary.Base64` to base64 encode binary files: \n\n // Import the Base64 encoding library.\n import org.apache.commons.codec.binary.Base64;\n\n // Encode the speech.\n byte[] encodedAudio = Base64.encodeBase64(audio.getBytes());\n\n\u003cbr /\u003e"]]