Base64 Audio — Encoding, Embedding, and Decoding Audio Data
Table of Contents
Base64 audio encoding works identically to Base64 image encoding — audio binary data is encoded to a text string that can be embedded in HTML, sent in JSON API payloads, or stored in text-based configuration. The same 33% size overhead applies, and the same tradeoffs between inline embedding and external URLs exist.
This guide covers practical Base64 audio patterns: embedding audio in HTML pages, sending audio to speech-to-text APIs, and decoding audio strings back to playable files.
Embedding Audio in HTML with Base64 Data URIs
The HTML audio element accepts Base64 data URIs in its src attribute, just like images:
<!-- MP3 audio embedded as Base64 --> <audio controls> <source src="data:audio/mpeg;base64,//uQxAA..." type="audio/mpeg"> <source src="data:audio/ogg;base64,T2dnUwAC..." type="audio/ogg"> Your browser does not support audio. </audio>
Audio MIME types for data URIs:
audio/mpeg— MP3 filesaudio/ogg— OGG Vorbis filesaudio/wav— WAV files (lossless, very large Base64)audio/webm— WebM audioaudio/aac— AAC files
Use Base64 audio embedding sparingly. Even a short 5-second MP3 at 128kbps is ~80KB, which becomes ~107KB of Base64 text. Audio files are much larger than images and are better served as separate cached files for anything beyond very short clips.
When Base64 audio makes sense: notification sounds (short bleeps under 2 seconds), CAPTCHA audio where external hosting creates privacy concerns, offline-capable PWA assets that must load without a network connection.
Encoding Audio Files to Base64
Command line (Linux / Mac):
# Encode audio file to Base64 base64 -i sound.mp3 | tr -d ' ' > sound.txt # Create a full data URI string echo -n "data:audio/mpeg;base64," > sound-uri.txt base64 -i sound.mp3 | tr -d ' ' >> sound-uri.txt
Python:
import base64
with open('sound.mp3', 'rb') as f:
audio_data = f.read()
encoded = base64.b64encode(audio_data).decode('utf-8')
data_uri = f"data:audio/mpeg;base64,{encoded}"
print(data_uri[:80] + "...") # Preview first 80 chars
Node.js:
const fs = require('fs');
const audioBuffer = fs.readFileSync('sound.mp3');
const encoded = audioBuffer.toString('base64');
const dataUri = 'data:audio/mpeg;base64,' + encoded;
Browser (FileReader):
const input = document.getElementById('audio-input');
input.addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (ev) => {
const dataUri = ev.target.result; // full data URI
const audio = new Audio(dataUri);
audio.play();
};
reader.readAsDataURL(file);
});
Sell Custom Apparel — We Handle Printing & Free Shipping
Base64 Audio in AI and Speech APIs
Many speech recognition and text-to-speech APIs accept audio as Base64 strings in their JSON request bodies:
// Google Cloud Speech-to-Text style request
const audioBuffer = fs.readFileSync('recording.wav');
const base64Audio = audioBuffer.toString('base64');
const response = await fetch('https://speech.googleapis.com/v1/speech:recognize', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
},
body: JSON.stringify({
config: {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
},
audio: {
content: base64Audio // Base64 audio here
}
})
});
The Anthropic Claude API similarly accepts audio encoded as Base64 in the messages API when using audio-capable models. ElevenLabs and other TTS APIs often return audio as Base64 in their JSON responses, which you then decode to play or save.
For APIs that return Base64 audio (text-to-speech responses), decode with Buffer.from(base64String, 'base64') in Node.js and write directly to a file with the appropriate extension.
Decoding Base64 Audio to a Playable File
Node.js — save to file:
const base64String = "//uQxAA..."; // your Base64 audio
const buffer = Buffer.from(base64String, 'base64');
fs.writeFileSync('output.mp3', buffer);
Browser — play decoded audio:
function playBase64Audio(base64String, mimeType = 'audio/mpeg') {
const binaryString = atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const blob = new Blob([bytes], { type: mimeType });
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.play();
audio.onended = () => URL.revokeObjectURL(url); // cleanup
}
// Play it
playBase64Audio("//uQxAA...", "audio/mpeg");
Browser — create a download link:
function downloadBase64Audio(base64String, filename = 'audio.mp3') {
const dataUri = 'data:audio/mpeg;base64,' + base64String;
const link = document.createElement('a');
link.href = dataUri;
link.download = filename;
link.click();
}
Size and Performance — Is Base64 Audio Worth It?
Audio files are much larger than images, so the 33% Base64 overhead matters more:
| Audio File | Original Size | Base64 Size |
|---|---|---|
| 1-second MP3 notification | ~16KB | ~21KB |
| 5-second sound effect | ~80KB | ~107KB |
| 30-second intro music | ~480KB | ~640KB |
| 3-minute song | ~2.8MB | ~3.7MB |
Base64 audio embedded in HTML or JavaScript cannot be cached by the browser independently. A 640KB Base64 string in your JavaScript bundle is re-downloaded every time the bundle changes. The same audio as a separate file gets cached and only downloaded once.
Recommendation: use Base64 audio only for sounds under ~20KB (short notification beeps, UI feedback sounds). For anything longer, host the audio as a separate file and reference it by URL.
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free Base64 Encoder/DecoderFrequently Asked Questions
Can I use our free Base64 decoder to inspect a Base64 audio string?
You can paste it in and decode it, but the result will be binary audio data displayed as text — garbled characters. This confirms the string is valid Base64 and shows the approximate decoded length, but you cannot hear the audio this way. Use the browser playback code above to play decoded audio.
What is the best audio format to use for Base64 embedding?
MP3 (audio/mpeg) gives the best size-to-quality ratio and the broadest browser support. OGG Vorbis is smaller and open-source but requires a fallback for Safari. WAV is lossless but far too large for Base64 embedding.
How do I get just the Base64 string from a data URI returned by FileReader?
FileReader.readAsDataURL() returns "data:audio/mpeg;base64,ACTUAL_BASE64". Split on the comma and take the second part: const base64Only = dataUri.split(",")[1]. This gives you just the encoded content without the prefix.

