How to convert base64 encoded text (audio / image) data into binary (wav / mp3) file?

Base64 encoding is done to convert binary data in ASCII data. This makes it easy to embed binary data inside text files like HTML files, javascript, css files, etc. So you can send binary information like image data, audio data, mucic files etc as part of your HTML web page and javascript files.

Base64 decoding is the reverse process of converting Base64 encoded ASCII text back to the original binary form. In this article we will see how to convert Base64 encoded audio data in formats like wav or mp3 back to the original binary wav/mp3 file.

In fact, you can use the below methods to convert any base64 encoded data back to its binary format.

Step 1: Save the base64 encoded data into a text file

Copy ONLY the base64 data into text file and save it. Let us call the text file mybase64.txt

Please note that you should not copy any other encoding information like the example below. But only the actual Base64 content that is present after the below encoding information.

data:audio/wav;base64,

Step 2: Convert Base 64 text to Binary File

Convert Base64 text to Binary File in Ubuntu or any Linux Platform

Use the below command base64 with the -d option to decode the base64 content back to binary content in Ubuntu or any other Linux distribution.

base64 mybase64.txt -d > myaudio.wav

Convert Base64 text to Binary File in Windows Platform

Use the windows command line tool Base64.exe with the -d option to decode the base64 content back to binary content in Windows Platform.

Base64.exe -d mybase64.txt > myaudio.wav

Convert Base64 text to Binary File in Mac OS X Platform

Use the command line tool base64 with the –decode option to decode the base64 content back to binary content in Apple Mac OS X.

base64 --decode mybase64.txt > myaudio.wav

Fix Base64 Decode Error

If you receive any error message while converting base64 to binary format, then first make sure you have copied ONLY the bas64 code and not any encoding information like data/audio etc.

Next, if you copied the base64 code from a HTML encoding, then most probably you have “=” of the base64 encoding replaced by its HTML equivalent “%3D”. You can see the %3D if present, at the end of the base64 text.

So replacing all ‘%3D’ with ‘=’ should fix your error. Do a simple string replace, in the language of your choice python or javascript as follows

content.replace("%3D","=");
Content Protection by DMCA.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here