Created
January 25, 2026 06:56
-
-
Save rezarajan/0a02b25ef1e6db8a55a53fbf5a4ab4e0 to your computer and use it in GitHub Desktop.
A simple script to convert popular audio file formats to Red Book standard audio files for burning to CD-R media.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Configuration | |
| SRC_DIR="${1:-.}" | |
| WAV_DIR="wav" | |
| TOC_FILE="$WAV_DIR/audio.toc" | |
| SAMPLE_RATE=44100 | |
| BIT_DEPTH=16 | |
| CHANNELS=2 | |
| CD_FRAME_SAMPLES=588 # 1 frame = 2352 bytes / (2 channels * 2 bytes per sample) | |
| mkdir -p "$WAV_DIR" | |
| echo "Converting audio files to WAV and aligning to CD frames..." | |
| # Collect supported audio files | |
| mapfile -t INPUTS < <( | |
| find "$SRC_DIR" -maxdepth 1 -type f \( \ | |
| -iname "*.flac" -o \ | |
| -iname "*.wav" -o \ | |
| -iname "*.aiff" -o \ | |
| -iname "*.aif" -o \ | |
| -iname "*.mp3" -o \ | |
| -iname "*.ogg" -o \ | |
| -iname "*.m4a" -o \ | |
| -iname "*.aac" \ | |
| \) | sort | |
| ) | |
| for input in "${INPUTS[@]}"; do | |
| base="$(basename "$input")" | |
| name="${base%.*}" | |
| wav="$WAV_DIR/$name.wav" | |
| echo " $input -> $wav" | |
| # Convert with sox to stereo 16-bit 44.1kHz WAV | |
| sox "$input" -r "$SAMPLE_RATE" -b "$BIT_DEPTH" -c "$CHANNELS" "$wav" | |
| # Align to CD frame boundary | |
| total_samples=$(soxi -s "$wav") | |
| remainder=$(( total_samples % CD_FRAME_SAMPLES )) | |
| if [ "$remainder" -ne 0 ]; then | |
| pad_samples=$(( CD_FRAME_SAMPLES - remainder )) | |
| echo " Padding $pad_samples samples to CD frame..." | |
| # pad 0 seconds at start, pad_samples/44100 seconds at end | |
| sox "$wav" "$wav.tmp.wav" pad 0 $(awk "BEGIN {printf \"%.6f\", $pad_samples/$SAMPLE_RATE}") | |
| mv "$wav.tmp.wav" "$wav" | |
| fi | |
| done | |
| echo "Generating TOC file in $WAV_DIR..." | |
| { | |
| echo "CD_DA" | |
| for wav in "$WAV_DIR"/*.wav; do | |
| cat <<EOF | |
| TRACK AUDIO | |
| FILE "$wav" 0 | |
| EOF | |
| done | |
| } > "$TOC_FILE" | |
| echo | |
| echo "Done." | |
| echo | |
| echo "Next steps:" | |
| echo " 1. Insert a blank CD-R" | |
| echo " 2. (Optional) Test with cdrdao:" | |
| echo " sudo cdrdao write --simulate --device /dev/cdrom $TOC_FILE" | |
| echo " 3. Burn:" | |
| echo " sudo cdrdao write --speed 8 --device /dev/cdrom $TOC_FILE" | |
| echo " (or wodim with padding):" | |
| echo " sudo wodim -v dev=/dev/sr0 -dao -audio -pad wav/*.wav" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment