#! /bin/bash
#
# Make .ogg out of all .wav in cwd and below, remove .wav!
#
# When converting remove only if conversion is ok.
# ffprobe any present .ogg and check if playtime is same as wav, remove only if all is good.
#CONFIRM=-i # confirm deletion?
REQS="ffmpeg ffprobe"
which $REQS >/dev/null
if [ ! $? -eq 0 ]; then
echo We need $REQS to do stuff!
exit 1
fi
shopt -s globstar nullglob nocaseglob # enable 2-star magic
for f in **/*.wav; do
OGG=$(dirname "$f")/$(basename "$f" wav)ogg
echo -n "$f: "
if [ ! -f "$OGG" ]; then
echo -en "\e[33mno ogg\e[0m"
CONVERT=1
else
echo -en "\e[32malready there\e[0m"
if [ ! "$(ffprobe "$f" 2>&1|grep Duration|cut -d, -f1)" = "$(ffprobe "$OGG" 2>&1|grep Duration|cut -d, -f1)" ]; then
echo -en " - \e[31mogg corrupt\e[0m"
CONVERT=1
rm $CONFIRM "$OGG"
else
OGGOK=1
fi
fi
if [ $CONVERT ]; then
echo -en " - \e[33mconverting\e[0m"
OUT=$(ffmpeg -loglevel error -i "$f" "$OGG" 2>&1)
if [ ! $? -eq 0 ]; then
echo -e " \e[31mfatal error!\e[0m\nffmpeg output was:\n\n$OUT\n"
rm $CONFIRM "$OGG"
elif [ ! -z "$OUT" ]; then
echo -e " \e[31mnon-fatal error!\e[0m\nffmpeg output was:\n\n$OUT\n"
else
OGGOK=1
fi
fi
if [ $OGGOK ]; then
echo -e " - all good removing wav"
rm $CONFIRM "$f"
fi
done