Skip to content

Instantly share code, notes, and snippets.

@scottcagno
Created December 5, 2015 08:55
Show Gist options
  • Select an option

  • Save scottcagno/46cf84f3f7fcc65a4303 to your computer and use it in GitHub Desktop.

Select an option

Save scottcagno/46cf84f3f7fcc65a4303 to your computer and use it in GitHub Desktop.
walk directory tree and convert any audio files (mp3, aif, aiff, flac, caf, m4a, etc.) to wav format @ 44.1Khz 24bit audio
#!/bin/bash
# EXAMPLE OPTIONS
#ffmpeg -i $file -ar 44.1k -ab 320k "${file%.mp3}.wav" && rm "$file"
#sox $file -b 24 -r 44100 -c 2 "${file%.mp3}.wav" norm && rm "$file"
function rmspaces {
local dir=$1
for file in "$dir"/*; do
[ -f "$file" ] && ( mv "$file" "$(echo $file | sed -e 's/ /_/g')" )
done
}
function iscaf {
local file="$1"
if [ -f $file ]; then
if [ "${file##*.}" = "caf" ]; then
echo "[$file] FOUND AN CAF";
sox $file -b 24 -r 44100 -c 2 "${file%.caf}.wav" norm && rm "$file"
fi
fi
}
function ismp3 {
local file="$1"
if [ -f $file ]; then
if [ "${file##*.}" = "mp3" ]; then
echo "[$file] FOUND AN MP3";
sox $file -b 24 -r 44100 -c 2 "${file%.mp3}.wav" norm && rm "$file"
fi
fi
}
function isaif {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "aif" ]; then
echo "[$file] FOUND AN AIF";
sox $file -b 24 -r 44100 -c 2 "${file%.aif}.wav" norm && rm "$file"
fi
fi
}
function isaiff {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "aiff" ]; then
echo "[$file] FOUND AN AIFF";
sox $file -b 24 -r 44100 -c 2 "${file%.aiff}.wav" norm && rm "$file"
fi
fi
}
function ismp4 {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "mp4" ]; then
echo "[$file] FOUND AN MP4";
sox $file -b 24 -r 44100 -c 2 "${file%.mp4}.wav" norm && rm "$file"
fi
fi
}
function ism4a {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "m4a" ]; then
echo "[$file] FOUND AN M4A";
sox $file -b 24 -r 44100 -c 2 "${file%.m4a}.wav" norm && rm "$file"
fi
fi
}
function ismov {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "mov" ]; then
echo "[$file] FOUND AN MOV";
sox $file -b 24 -r 44100 -c 2 "${file%.mov}.wav" norm && rm "$file"
fi
fi
}
function isflac {
local file=$1
if [ -f $file ]; then
if [ "${file##*.}" = "flac" ]; then
echo "[$file] FOUND AN FLAC";
sox $file -b 24 -r 44100 -c 2 "${file%.flac}.wav" norm && rm "$file"
fi
fi
}
function walk_tree {
local dir="$1"
local i
for i in "$dir"/*; do
if [ "$i" = . -o "$i" = .. ]; then
continue
elif [ -d "$i" ]; then # FOUND DIR, PROCESS DIR AND/OR WALK-DOWN INTO DIR...
echo "[cdir]:: $i"
walk_tree "$i" # RECURSIVLY CALL ONESELF AND DECEND INTO NEXT LEVEL
elif [ -f "$i" ]; then
rmspaces $dir
iscaf $i
ismp3 $i
isaif $i
isaiff $i
ismp4 $i
isflac $i
ism4a $i
ismov $i
echo "[file]:: $i" # HANDLE PARTICULAR FILE
else
echo "[desc]:: $i" # DESCEND INTO NEXT DIRECTORY; ELSE SOMETHING IS WRONG
continue
fi
done
}
walk_tree $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment