Skip to content

Instantly share code, notes, and snippets.

@ManuelTS
Last active November 9, 2024 09:45
Show Gist options
  • Save ManuelTS/c232a70e081558dfcd6c797f991009df to your computer and use it in GitHub Desktop.
Save ManuelTS/c232a70e081558dfcd6c797f991009df to your computer and use it in GitHub Desktop.
Batch convert all .mp3 files in the current directory to 432Hz with ffmpeg
#!/bin/bash
# Batch convert all .mp3 files in the current directory to 432Hz with ffmpeg
# Options
suffix="false" # Append the -432Hz suffix or not
oldIFS=$IFS
IFS=$'\n'
set -f # Deal with blanks and special characters in file names of the file command and in for loop
found=($(find . -name "*.mp3")) # Find the files in the current directory
IFS=$oldIFS
set +f
cpuCount=$(nproc --all)
started=0
# Wait for all remaining sub shells to complete, first argument is the counter of max subshells
wait4Completion() {
if [ "$1" -gt 0 ]; then
((started++)) # Increase variable
fi
if [ $started -gt "$1" ]; then
wait;
started=0;
fi
}
for file in "${found[@]}"; do # Iterate the found files
(mv "$file" "$file.tmp"; ffmpeg -loglevel 8 -i "$file.tmp" -af "asetrate=48000*432/440,aresample=48000,atempo=440/432" "$file"; rm "$file.tmp"; echo "Pitched $file") &
wait4Completion $cpuCount
done
wait4Completion 0
@ManuelTS
Copy link
Author

ManuelTS commented Nov 9, 2024

You are right, I got the math not entirely right and the ffmpeg params wrong too. I moved away from converting my music but with your suggestion I updated the script and it also sounds correct! Since, before the music seamed to slow down a bit.

Thanks a ton for pointing that out with the solution!

Additionally, I added parallel conversion limited by the CPU count. So whole music libraries can be converted much faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment