Skip to content

Instantly share code, notes, and snippets.

@tkoop
Last active July 11, 2025 15:10
Show Gist options
  • Save tkoop/aacdf2845448a8cfde5e0d900e254001 to your computer and use it in GitHub Desktop.
Save tkoop/aacdf2845448a8cfde5e0d900e254001 to your computer and use it in GitHub Desktop.
Bash script to convert all audio files to mp3
#!/bin/bash
# Define supported audio extensions
EXTENSIONS="wav flac ogg m4a aac wma opus"
# Find and process all audio files in the current directory and subdirectories
find . -type f \( -iname "*.wav" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.m4a" -o -iname "*.aac" -o -iname "*.wma" -o -iname "*.opus" \) | while read -r file; do
# Get the directory and filename without extension
dir=$(dirname "$file")
filename=$(basename "$file" | cut -d. -f1)
output="$dir/$filename.mp3"
# Convert to MP3
echo "Converting: $file to $output"
if ffmpeg -i "$file" -codec:a mp3 -b:a 192k "$output" 2>/dev/null; then
# Verify the output file exists and is not empty
if [[ -s "$output" ]]; then
echo "Success: $file converted to $output"
# Delete the original file
rm "$file"
echo "Deleted original: $file"
else
echo "Error: Output file $output is empty or missing, keeping original"
fi
else
echo "Error: Conversion failed for $file, keeping original"
fi
done
echo "Conversion process complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment