Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created March 15, 2025 18:08
Show Gist options
  • Save sharpred/7f4e121ccf57c75eea1dd42ff94b5b8d to your computer and use it in GitHub Desktop.
Save sharpred/7f4e121ccf57c75eea1dd42ff94b5b8d to your computer and use it in GitHub Desktop.
Create high quality mp3 from bandcamp wav files
#!/bin/bash
# Set input folder (current directory)
INPUT_DIR="$(pwd)"
COVER_IMAGE="$INPUT_DIR/cover.jpg"
OUTPUT_DIR="$INPUT_DIR/mp3_output"
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Count total number of wav files
TOTAL_TRACKS=$(ls "$INPUT_DIR"/*.wav | wc -l)
# Loop through all .wav files in the folder
for wav_file in "$INPUT_DIR"/*.wav; do
# Get the base filename without extension
base_name="$(basename -- "$wav_file" .wav)"
# Extract metadata using awk
artist=$(echo "$base_name" | awk -F ' - ' '{print $1}')
album=$(echo "$base_name" | awk -F ' - ' '{print $2}')
track_number_title=$(echo "$base_name" | awk -F ' - ' '{print $3}')
# Extract track number (first word) and track title (remaining words)
track_number=$(echo "$track_number_title" | awk '{print $1}')
track_title=$(echo "$track_number_title" | cut -d ' ' -f2- | tr '[:upper:]' '[:lower:]')
track_total="$track_number of $TOTAL_TRACKS"
output_file="$OUTPUT_DIR/$track_title.mp3"
echo "Converting: $wav_file -> $output_file"
echo " Artist: $artist"
echo " Album: $album"
echo " Track #: $track_number ($track_total)"
echo " Title: $track_title"
# Run ffmpeg conversion with metadata and embedded cover art
ffmpeg -i "$wav_file" -i "$COVER_IMAGE" -map 0:a -map 1:v -c:v mjpeg -b:a 320k \
-id3v2_version 3 -metadata artist="$artist" -metadata album="$album" \
-metadata track="$track_number/$TOTAL_TRACKS" -metadata title="$track_title" "$output_file"
done
echo "Conversion complete. MP3 files saved in $OUTPUT_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment