Skip to content

Instantly share code, notes, and snippets.

@maxpeterson
Last active January 4, 2018 10:53
Show Gist options
  • Save maxpeterson/ebab9079b852224c43d1562c5f7bdf83 to your computer and use it in GitHub Desktop.
Save maxpeterson/ebab9079b852224c43d1562c5f7bdf83 to your computer and use it in GitHub Desktop.
Reencode music files from alac to aac
#!/bin/bash
folder=$1; shift
if test "$folder" == "" ; then
echo "Please provide a folder containing music to re-encode."
exit 1
fi
old_codec=${1:-alac}
new_codec=${2:-aac}
destdir=$(dirname "$folder")/Encoded
echo "Writing re-encoded files to $destdir"
echo
find "$folder" -type f -not -name .\* | while read file; do
#echo "$file"
outfile=${file/#$folder/$destdir}
if [ ! -f "$outfile" ]; then
streams=$(ffprobe -loglevel quiet -show_streams "$file")
if test "$?" == "1"; then
echo "ffprobe error $file: $streams"
else
echo "$streams" | grep -q -e "codec_name=$old_codec";
if test "$?" == "0"; then
dir=$(dirname "$outfile")
[[ -d "$dir" ]] || mkdir -p "$dir";
echo "encoding $file"
ffmpeg -nostdin -loglevel error -y -i "$file" -strict experimental -vn -sn -c:a "$new_codec" "$outfile";
fi
fi
fi
done
@maxpeterson
Copy link
Author

maxpeterson commented Jan 4, 2018

./reencode.sh ~/Music/iTunes/iTunes\ Media/Music

Will create files in ~/Music/iTunes/iTunes\ Media/Encoded/.. folder based on files in~/Music/iTunes/iTunes\ Media/Music/....

Copy other files with

rsync -auv --exclude='.*' M/Music/ M/Encoded/ 

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