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

I know the -y flag of ffmpeg, but it did not work on .mp3 files.

@elie20092
Copy link

Modifie la ligne 21 le dernier mot "$file" modifie le en "${file%.mp3}-432Hz.mp3"

@ManuelTS
Copy link
Author

ManuelTS commented Feb 12, 2023

I do not know French, but Google translate does fortunately. I added a "suffix" variable so the users can simply switch between the two file endings.

@Art10001
Copy link

Art10001 commented Nov 9, 2024

This is weird. The md says it's 441 -> 432 which is 9 Hz, which may mean you actually did calculate the percent change for 441 -> 432 instead of 440 -> 432 Hz which is 8 Hz.
That's a minor nitpick for anyone who doesn't use percent ratios or cents for retuning, though.

You are breaking the 8000, 16000, 22050, 44100, 48000 Hz which are the most common samplerates...
There are a few players out there that may dislike nonstandard rates.
(Edit: Lowering the sample rate may clamp high frequencies [tuned high frequencies/amplified {freq*2*2*2... or any multiplier that is divisible by 3, 6, 9, but not 13} are great] and make the music sound darker or slower which I dislike.)
I've no idea if 43200 will magically shift to 432 Hz, but here is a different command that does, and I've confirmed it works for the Solfeggios with another person who is more energy sensitive than me.

"asetrate=SAMPLE_RATE*NEW_FREQ/ORIG_FREQ,aresample=SAMPLE_RATE,atempo=ORIG_FREQ/NEW_FREQ"

For example:

ffmpeg -i file.ext -af "asetrate=48000*432/440,aresample=48000,atempo=440/432" out.ext

Found here https://www.mail-archive.com/[email protected]/msg30115.html

@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