How long will the string be?
Base64 writes 4 characters for every 3 bytes. An MP3 spends a fixed number of bytes per second, set by its bitrate. Multiply the two and you know the string length before you encode anything.
| Sound | Bitrate | MP3 bytes | Base64 characters |
|---|---|---|---|
| Button click, 0.3 s | 96 kbps | 3,600 | 4,800 |
| Notification chime, 2 s | 128 kbps | 32,000 | 42,668 |
| Radio jingle, 30 s | 128 kbps | 480,000 | 640,000 |
| Song, 3 min | 192 kbps | 4,320,000 | 5,760,000 |
| Podcast episode, 1 h | 64 kbps | 28,800,000 | 38,400,000 |
Rule of thumb: at 128 kbps, each second of audio adds about 21,300 characters. The table counts audio frames only. A song bought from a music store often carries a 50 to 500 KB cover image in its ID3 tag, which turns into another 67 to 667 KB of text. With Strip ID3 tags on, none of those bytes reach the output.
What Strip ID3 removes, and what stays
Removed
- The ID3v2 block at the start of the file: title, artist, album, lyrics, ReplayGain values, and embedded cover art (APIC frames). Some files carry two of these blocks back to back. Both go.
- The ID3v1 trailer, the last 128 bytes, which start with the letters TAG.
Kept
- The Xing, Info, or VBRI header inside the first audio frame. Browsers read the frame count from this header to show the right duration for VBR files and to seek. Without the header, the progress bar breaks.
- APEv2 and Lyrics3 tags. These sit between the audio and the ID3v1 trailer, and the page does not cut them. Expect a few hundred extra bytes from files tagged this way.
Players skip tags, so stripping changes nothing you hear. The readout on the left shows the tag size and the cover art size before you decide.
Pick the output for where the string lands
- Data URI
-
The full
data:audio/mpeg;base64,form. Paste the string into asrcattribute, a CSS custom property, or a fetch call.The MIME type is
audio/mpeg, the registered type for MP3 under RFC 3003. Plenty of tutorials writeaudio/mp3. Chrome and Firefox accept the label, but the type is not registered and HTML validators flag the tag. - Raw
The string with no prefix. Speech services such as Google Cloud Speech-to-Text want this form in the
contentfield of a JSON request. Turn on the URL-safe alphabet for query strings or file names. Turn on 76-character wrapping for a MIME email body (RFC 2045). JSON and data URIs must stay on one line.- <audio>
-
<audio controls src="data:audio/mpeg;base64,SUQzBAAAAAAA..."></audio>A drop-in tag for a single-file HTML demo or an offline help page.
- JS
-
const clip = new Audio("data:audio/mpeg;base64,//uQxAAAAAAA..."); document.querySelector("#send").addEventListener("click", () => clip.play());Browsers block
play()until the visitor clicks or taps on the page, so trigger playback from an event handler, as above. A call on page load throws a NotAllowedError. - JSON
An object with the file name,
mimeType, byte count, anddata. Rename the keys to match your API before sending.
When to skip Base64 and serve the file
Inlining audio trades caching for one fewer request. For most sounds bigger than a click, the trade loses.
- Anything past about 100 KB inside HTML or CSS. Every page view downloads the full string again, and the browser holds the text plus the decoded bytes in memory. A separate MP3 file gets cached once.
- Songs and podcasts. A data URI has no range requests. The player decodes the whole string before playback, where a hosted file starts streaming after the first few kilobytes.
- Email. Gmail and Outlook drop the audio element. Attach the MP3 instead.
- UI sounds under 20 KB, such as clicks, chimes, and error beeps. One fewer request, no flash of silence on first use.
- Single-file deliverables: a self-contained HTML report, a CodePen, a browser extension popup.
- JSON APIs with no multipart upload, such as most speech-to-text and audio classification endpoints.
Server compression with gzip or Brotli wins back most of the 33% on the wire, since Base64 text compresses well. Memory use in the browser stays at the full size.
Encode or decode without a browser
The page accepts files up to 50 MB. For anything bigger, or for a build script, use the command line.
# Linux (GNU coreutils), one line, no breaks
base64 -w 0 chime.mp3 > chime.b64
# macOS
base64 -i chime.mp3 -o chime.b64
# Back to MP3
base64 -d chime.b64 > chime.mp3 # Linux
base64 -D -i chime.b64 -o chime.mp3 # macOS
Decoding a string in code:
// Browser, from a data URI
const blob = await (await fetch(dataUri)).blob();
// Node.js, from a raw string
fs.writeFileSync("chime.mp3", Buffer.from(b64, "base64"));
# Python
open("chime.mp3", "wb").write(base64.b64decode(b64))
A quick sanity check on any string: Base64 output starting with SUQz is the text ID3, so the file opens with a tag. Output starting with // opens straight on an MPEG frame header, so the tag is gone.