Speech-to-Text API로 오디오 데이터를 전송할 때 요청의 content 필드에서 데이터를 직접 전송하거나 API가 Cloud Storage 버킷에 저장된 데이터에서 원격으로 인식을 수행하도록 할 수 있습니다.
오디오 데이터가 최대 60초 10MB인 경우에만동기식 인식을 위해 content 필드에 데이터를 직접 보낼 수 있습니다. content 필드의 모든 오디오 데이터는 base64 형식이어야 합니다. 이 페이지에서는 바이너리 파일의 오디오를 base64 인코딩 데이터로 변환하는 방법을 설명합니다.
오디오 데이터가 60초 또는 10MB를 초과하는 경우 인식을 위해 전송하려면 데이터를 Cloud Storage 버킷에 저장해야 합니다. base64 형식으로 변환하지 않고 비동기식으로 분석할 수 있습니다. 자세한 내용은 비동기 인식 문서를 참조하세요.
명령줄 사용
gRPC 요청 내에 바이너리 데이터를 직접 쓸 수 있습니다. 그러나 REST 요청을 할 때는 JSON이 사용됩니다. JSON은 바이너리 데이터를 직접 지원하지 않는 텍스트 형식이므로, Base64 인코딩을 사용하여 바이너리 데이터를 텍스트로 변환해야 합니다.
대부분의 개발 환경에는 바이너리를 ASCII 텍스트 데이터로 인코딩하는 기본 base64 유틸리티가 포함되어 있습니다. 파일을 인코딩하려면 다음 안내를 따르세요.
Linux
base64 명령줄 도구를 사용하여 파일을 인코딩합니다. -w 0 플래그를 사용하여 줄바꿈을 방지해야 합니다.
텍스트 편집기를 통해 바이너리 데이터를 요청에 삽입하는 것은 바람직하지 않으며 실용적이지도 않습니다. 실제로는 클라이언트 코드에 base64 인코딩 파일을 삽입합니다. 지원되는 모든 프로그래밍 언어에는 base64 인코딩 콘텐츠를 위한 기본 메커니즘이 있습니다.
Python
Python에서 base64는 다음과 같이 오디오 파일을 인코딩합니다.
# 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
Node.js에서 base64는 다음과 같이 오디오 파일을 인코딩합니다. 여기서 audioFile은 오디오 인코딩 파일의 경로입니다.
자바에서는 다음과 같이 org.apache.commons.codec.binary.Base64에 있는 encodeBase64 정적 메서드를 사용하여 바이너리 파일을 base64로 인코딩합니다.
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the speech.byte[]encodedAudio=Base64.encodeBase64(audio.getBytes());
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(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"]]