#!/bin/bash # # speedcode - reencode audio to mp3, scaling the tempo # usage: speedcode [-f factor] [-b bitrate] outfile file1 [file2 ...] factor=1.75 bitrate='%%AUTO%%' mono='-af pan=1:0.5:0.5,format=s16le' while getopts b:f:s opt;do case $opt in b) bitrate=$OPTARG;; f) factor=$OPTARG;; s) mono=;; esac done shift $(expr $OPTIND - 1) outfile="$1" shift if [[ -e "$outfile" ]];then echo "Output file $outfile exists. Will not overwrite." >&2 exit 1 fi nearest_standard_mp3_rate () { # This doesn't take the sample rate into account =/ # standard_rates='8 16 24 32 40 48 56 64 80 96 112 128 144 160 192 224 256 320' standard_rates='8 16 24 32 40 48 56 64 80 96 112 128' # I don't like high rates tr ' ' \\n <<< "$standard_rates" | awk -v "rate=$1" '{ print $1 - rate, $1 }' | tr -d -- - | sort -n | head -n1 | cut -d' ' -f2 } if [[ "$bitrate" = '%%AUTO%%' ]];then # Use the maximum rate of the input files, multiplied by the speed factor, # rounded to the nearest standard mp3 bitrate bitrate="$(nearest_standard_mp3_rate $( dc <<< "$( # Use the first input file to determine the bitrate empirical-bitrate-calculator "$1" | sort -nr | cut -f1) 1024 8k/ $factor *p"))" echo "Autoselected bitrate: $bitrate" fi soundstretch <( exec mplayer -quiet -really-quiet $mono -ao pcm:waveheader:fast:file=/dev/stdout "$@" ) >( exec lame --cbr -b "$bitrate" - "$outfile" ) -tempo=$(echo "9k $factor 1 - 100 * p" | dc) # Copy the id3 tag of the first input file if [[ "$1" = *.mp3 ]];then id3cp "$1" "$outfile" fi