Created
June 10, 2015 21:54
-
-
Save bearcatsandor/dc4e4a151a81f2e17db4 to your computer and use it in GitHub Desktop.
/home/bearcat/bin/wvbatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh - | |
# | |
# WVBATCH - wavpack batch encode script | |
# | |
## CONSTANTS ## | |
readonly TRUE=0 | |
export TRUE | |
readonly FALSE=1 | |
export FALSE | |
## VARIABLES ## | |
export WAVPACK_OPTS="-vyhhx3m" | |
export DELETE_INPUT=$FALSE | |
MAX_PROCS="100%" | |
### FUNCTIONS ################################################################## | |
print_help() | |
{ | |
cat << EOF | |
WVBATCH - wavpack batch encode script | |
Usage: | |
wvbatch [options] <input.files> | |
Options: | |
-d) | |
delete all input files | |
(same as '-o "[other-options] -d"') | |
--help) | |
print this message | |
-j) | |
maximum number of processors to use || | |
percentage of the available processors to use | |
(default = '100%') | |
e.g.) | |
-j 6 | |
-j 40% | |
-o) | |
wavpack options | |
(default = '-hhx6'; '-qzymvr' is hardcoded) | |
e.g.) | |
-o -hx3b320c | |
-o "-hhx4 --raw-pcm=48000,16,6 --channel-order=FL,FR,FC,LFE,SL,SR" | |
Read the wavpack man page for all available options. | |
Dependencies: | |
1 WavPack | |
2 GNU Parallel | |
EOF | |
} | |
### TRAPS ###################################################################### | |
trap ' | |
printf "\naborting...\n" >&2 | |
exit 255 | |
' HUP INT QUIT ABRT KILL PIPE TERM | |
### GET OPTS ################################################################### | |
while [ $# -gt 0 ] ; do | |
case $1 in | |
-d) | |
DELETE_INPUT=$TRUE | |
shift | |
;; | |
--help) | |
print_help >&2 | |
exit 1 | |
;; | |
-j) | |
MAX_PROCS="$2" | |
shift 2 | |
;; | |
-o) | |
WAVPACK_OPTS="$2" | |
shift 2 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
if [ $# -eq 0 ] ; then | |
print_help >&2 | |
exit 1 | |
fi | |
if [ $DELETE_INPUT -eq $TRUE ] ; then | |
WAVPACK_OPTS="$WAVPACK_OPTS -d" | |
fi | |
### MAIN ####################################################################### | |
wavpack 2>&1 | sed 4q >&2 | |
parallel -j "$MAX_PROCS" ' | |
TIME_START=$(date +%s.%N) | |
wavpack -qzymvr $WAVPACK_OPTS {} | |
EXIT_STATUS=$? | |
TIME_STOP=$(date +%s.%N) | |
case $EXIT_STATUS in | |
0) | |
TIME=$(echo "$TIME_STOP - $TIME_START" | bc) | |
OUTFILE={.}.wv | |
WVINFO="$(wvunpack -qzs "$OUTFILE")" | |
MD5_SIG="$( | |
echo "$WVINFO" | | |
grep -F "original md5:" | | |
sed "s/original md5:[[:space:]]*//" | |
)" | |
MODALITY=$(echo "$WVINFO" | grep -F "modalities:") | |
if echo "$MODALITY" | grep -Fq "lossless" ; then | |
if echo "$MODALITY" | grep -Fq "hybrid" ; then | |
HYBRID="(+.wvc) " | |
fi | |
COMP_TYPE="lossless" | |
COMP_STAT="$( | |
echo "$WVINFO" | | |
grep -F "compression:" | | |
sed "s/compression:[[:space:]]*//" | |
)" | |
else | |
COMP_TYPE="lossy" | |
COMP_STAT="$( | |
echo "$WVINFO" | | |
grep -F "ave bitrate:" | | |
sed "s/ave bitrate:[[:space:]]*//" | |
)" | |
fi | |
printf "%s:\n" {} | |
if [ $DELETE_INPUT -eq $TRUE ] ; then | |
printf "deleted source file %s\n" {} | |
fi | |
printf "original md5 signature: %s\n" "$MD5_SIG" | |
printf "created (and verified) %s %sin %.2f secs (%s, %s)\n" \ | |
"$OUTFILE" "$HYBRID" $TIME "$COMP_TYPE" "$COMP_STAT" | |
printf "\n" | |
exit 0 | |
;; | |
*) | |
# wavpack will print the error here | |
printf " \033[41m>>>\033[0m %s\n\n" {} >&2 | |
exit $EXIT_STATUS | |
;; | |
esac | |
' ::: "$@" | |
EXIT_STATUS=$? | |
case $EXIT_STATUS in | |
0) | |
if [ $# -ne 1 ] ; then | |
printf " **** %d files successfully processed ****\n" $# >&2 | |
fi | |
exit 0 | |
;; | |
254) | |
printf " **** warning: errors occured in more than 253 of %d files! ****\n" $# >&2 | |
exit 254 | |
;; | |
255) # parallel error | |
exit 255 | |
;; | |
*) | |
printf " **** warning: errors occured in %d of %d files! ****\n" $EXIT_STATUS $# >&2 | |
exit $EXIT_STATUS | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment