Luke Oliff.

TIL: Encoding Files as Base64 in the Terminal

·TIL·1 min read·Luke Oliff

Some APIs accept audio as base64 encoded JSON instead of requiring a file upload or streaming. base64 in the terminal handles the encoding.

base64 -i recording.wav -o recording.b64

Encodes the WAV file to base64 text. The output is a single long line of characters that can be embedded in a JSON payload.

base64 recording.wav | pbcopy

Encode and copy to the clipboard in one step. Paste the result directly into a JSON body.

AUDIO_B64=$(base64 -i recording.wav | tr -d '\n')
jq -n --arg audio "$AUDIO_B64" '{audio: {content: $audio}}'

Pipe the encoded audio into jq and construct a valid JSON payload. The tr -d removes any line wrapping.

Does base64 increase file size?

Yes, by about 33%. Binary data encoded as base64 takes more space. For large files use binary upload or streaming instead.

Is base64 available on macOS?

Yes. base64 is built into macOS and Linux.