Skip to content

Instantly share code, notes, and snippets.

@ManuelTS
Last active October 23, 2023 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
filename=""
for file in "${found[@]}"; do # Iterate the found files
# Math:
# We want to convert from 441Hz to 432Hz, the difference is 9Hz which are 2,040816327 % of 441Hz.
# Parameters:
# asetrate contains the desired pitch (frequency) but changes the playback speed
# atempo ajusts the resulting playback speed, which should remain the same
# aresample keeps the pitch change by correct rate of (re)sampling
mv "$file" "$file.tmp"
if $suffix == "true"; then
filename="${file%.mp3}-432Hz.mp3"
else
filename="$file"
fi
ffmpeg -loglevel 8 -i "$file.tmp" -af asetrate=43200,atempo=1.00,aresample=43200 "$filename"
rm "$file.tmp"
echo "Pitched $filename"
done
@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.

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