Audio to Base64 Converter

Free online tool to convert audio files (MP3, WAV, OGG, FLAC, AAC) to Base64 format. Perfect for embedding audio in JSON, HTML, or CSS. Fast, secure, and works entirely in your browser.

Why Use Audio to Base64 Converter?

Multiple Format Support

Supports MP3, WAV, OGG, FLAC, AAC, M4A, and WMA audio formats

Instant Preview

Play and preview your audio before and after conversion

Secure & Private

All processing happens in your browser. No files uploaded to servers.

Large File Support

Convert audio files up to 30MB in size

Implementation Examples

Learn how to convert audio files to Base64 in your favorite programming language. All examples include complete, working code that you can use in your projects.

How to Convert Audio to Base64 with JavaScript

// Convert audio file to Base64
function audioToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => {
      const base64 = reader.result; // Includes data URL prefix
      resolve(base64);
    };
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}

// Usage with file input
document.querySelector('input[type="file"]').addEventListener('change', async (e) => {
  const file = e.target.files[0];
  const base64 = await audioToBase64(file);
  console.log(base64); // data:audio/mpeg;base64,//uQx...

  // Get only Base64 string (without prefix)
  const base64Only = base64.split(',')[1];
});

// Get audio duration
function getAudioDuration(file) {
  return new Promise((resolve) => {
    const audio = new Audio();
    audio.src = URL.createObjectURL(file);
    audio.onloadedmetadata = () => {
      resolve(audio.duration);
      URL.revokeObjectURL(audio.src);
    };
  });
}

How to Convert Audio to Base64 with Python

import base64
from mutagen.mp3 import MP3
from mutagen.wave import WAVE

# Convert audio file to Base64
def audio_to_base64(audio_path):
    with open(audio_path, "rb") as audio_file:
        encoded = base64.b64encode(audio_file.read())
        return encoded.decode('utf-8')

# Usage
base64_string = audio_to_base64("song.mp3")
print(f"data:audio/mpeg;base64,{base64_string}")

# Get audio info with mutagen
def get_audio_info(audio_path):
    try:
        # Try MP3
        audio = MP3(audio_path)
        return {
            'duration': audio.info.length,
            'bitrate': audio.info.bitrate,
            'sample_rate': audio.info.sample_rate
        }
    except:
        # Try WAV
        audio = WAVE(audio_path)
        return {
            'duration': audio.info.length,
            'channels': audio.info.channels
        }

info = get_audio_info("song.mp3")
print(f"Duration: {info['duration']} seconds")

How to Convert Audio to Base64 with PHP

<?php
// Convert audio file to Base64
function audioToBase64($audioPath) {
    $audioData = file_get_contents($audioPath);
    $base64 = base64_encode($audioData);

    // Get MIME type
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mimeType = finfo_file($finfo, $audioPath);
    finfo_close($finfo);

    return "data:$mimeType;base64,$base64";
}

// Usage
$dataUrl = audioToBase64("song.mp3");
echo $dataUrl;

// Get file size
$fileSize = filesize("song.mp3");
echo "File size: " . ($fileSize / 1024) . " KB";

// For API response
header('Content-Type: application/json');
echo json_encode([
    'audio' => base64_encode(file_get_contents("song.mp3")),
    'mimeType' => mime_content_type("song.mp3"),
    'size' => filesize("song.mp3")
]);
?>

How to Convert Audio to Base64 with Node.js

const fs = require('fs').promises;
const mm = require('music-metadata');

// Convert audio to Base64
async function audioToBase64(filePath) {
  const buffer = await fs.readFile(filePath);
  return buffer.toString('base64');
}

// Get audio metadata
async function getAudioMetadata(filePath) {
  const metadata = await mm.parseFile(filePath);
  return {
    duration: metadata.format.duration,
    bitrate: metadata.format.bitrate,
    sampleRate: metadata.format.sampleRate,
    codec: metadata.format.codec,
    mimeType: metadata.format.mimeType
  };
}

// Usage
(async () => {
  const base64 = await audioToBase64('song.mp3');
  const metadata = await getAudioMetadata('song.mp3');

  console.log('Base64:', base64.substring(0, 50) + '...');
  console.log('Duration:', metadata.duration, 'seconds');
  console.log('Bitrate:', metadata.bitrate, 'bps');
})();

// Express.js API endpoint
app.post('/api/audio/encode', async (req, res) => {
  const base64 = await audioToBase64(req.file.path);
  res.json({
    base64,
    mimeType: req.file.mimetype,
    size: req.file.size
  });
});

Share this tool

Help others discover this free Base64 tool

or