Skip to content

Instantly share code, notes, and snippets.

@sahidursuman
Forked from zthomae/soundstretch.sh
Last active August 29, 2015 14:07
Show Gist options
  • Save sahidursuman/8ee6fbe56bb6e92237fc to your computer and use it in GitHub Desktop.
Save sahidursuman/8ee6fbe56bb6e92237fc to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Brain hacking in action: A simple bash script for making "fast" versions of mp3 files.
#
# Usage: The script takes 2 arguments, a "getdir" and a "putdir".
#
# The script will recursively search for mp3 files in getdir and make "fast" versions of them
# (+40% speed). The full directory tree (from /) is recreated inside putdir, and the
# fast mp3s are placed in their corresponding locations in the new tree.
#
# Dependencies:
# * lame
# * soundstretch
# * id3lib
FORCE=0
VERBOSE=0
while getopts ":fv" opt; do
case $opt in
f)
FORCE=1
;;
v)
VERBOSE=1
;;
\?)
echo "Invalid option: $OPTARG"
echo "Usage: `basename $0` [-fv] getdir putdir"
exit 1
;;
esac
done
echo "---------------"
shift $((OPTIND-1))
if [[ ! -n "$1" || ! -n "$2" ]] ; then
echo "Usage: `basename $0` [-fv] getdir putdir"
exit 1
fi
DEPENDENCIES="lame soundstretch id3cp"
for i in $DEPENDENCIES ; do
type $i >/dev/null 2>&1 || { echo >&2 "Error: Required dependency $i not installed."; exit 1; }
done
GETDIR=$1
PUTDIR=$2
convertFile () {
force=$3
verbose=$4
file="$1"
if [ $verbose -eq 0 ]; then
exec &>/dev/null
fi
echo "Now processing: $file"
if [[ "${1:0:2}" == './' ]] ; then
file="${file:2}"
fi
base=${file%.*}
if [[ "${base:0:1}" != '/' ]] ; then
base="$(pwd)/$base"
fi
basedir=${base%/*}
ext=${file#*.}
outdir="$2"
if [[ -e "$outdir/$base.fast.mp3" ]] ; then
if [[ $force -eq 0 ]] ; then
echo "File exists. Will not overwrite."
exit 1
else
echo "File exists. Overwriting."
fi
fi
mkdir -p "$outdir/${basedir}"
lame --decode "$file" "$base.wav"
soundstretch "$base.wav" "$base.fast.wav" -tempo=+40
lame --preset fast medium "$base.fast.wav" "$outdir/$base.fast.mp3"
id3cp "$file" "$outdir/$base.fast.mp3"
rm "$base.wav" "$base.fast.wav"
echo "---------------"
}
export PUTDIR="$PUTDIR"
export FORCE=$FORCE
export VERBOSE=$VERBOSE
export -f convertFile
find "$GETDIR" -type f \( -iname "*.mp3" -a ! -iname "*.fast.mp3" \) -exec bash -c 'convertFile "{}" "$PUTDIR" $FORCE $VERBOSE' ';'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment