ambevar-dotfiles/.scripts/tc-audio-batch

128 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2013-10-08 22:17:30 +02:00
_printhelp ()
{
cat<<EOF
2013-10-08 22:21:02 +02:00
Usage: ${1##*/} [-p PROC] ROOT [OPTIONS]
2013-10-08 22:17:30 +02:00
This will batch-process all audio files found in ROOT folder and subfolders with
the tc-audio-transcode script.
All OPTIONS are passed to tc-audio-transcode. For more details see
tc-audio-transcode -h
This script has the ability to run multiple processes. The number or processes
defaults to the number of online cores if 'lscpu' is found, or to 1
otherwise. The number of parallel processes is bounded by the number of input
2013-10-08 22:21:02 +02:00
files. Outputting from several processes at the same time to the standard output
is not convenient, therefore when more than 1 process is started, the output is
disabled (except for errors).
2013-10-08 22:17:30 +02:00
If you still want the output, you need to restrict the number of processes to
1. For instance to preview changes in current folder you would write:
${1##*/} -p1 . -p
Options:
-p PROC: run PROC processes in parallel.
EOF
}
if [ -n "$(command -v lscpu)" ]; then
## lscpu is a Linux util also available on *BSD.
CPUNO=$(lscpu | awk '/^CPU\(s\)/ {print $2;exit}')
else
CPUNO=1
fi
while getopts ":p:h" opt; do
case $opt in
h)
_printhelp "$0"
2013-10-12 16:01:46 +02:00
exit 1 ;;
2013-10-08 22:17:30 +02:00
p)
2013-10-12 16:01:46 +02:00
CPUNO=$OPTARG ;;
2013-10-08 22:17:30 +02:00
?)
_printhelp "$0"
2013-10-12 16:01:46 +02:00
exit 1 ;;
2013-10-08 22:17:30 +02:00
:)
_printhelp "$0"
2013-10-12 16:01:46 +02:00
exit 1 ;;
2013-10-08 22:17:30 +02:00
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
exit 1
fi
ROOT="$1"
shift
if [ -z "$(command -v ffmpeg)" ]; then
echo "ffmpeg required."
exit
fi
if [ ! -f "${0%/*}/titlecase.awk" ]; then
echo "AWK titlecase script required."
exit
fi
if [ ! -f "${0%/*}/tc-audio-transcode" ]; then
echo "tc-audio-transcode script required."
exit
fi
2013-10-08 22:17:30 +02:00
INPUT="$(find "." \( \
-iname '*.aac' -o \
-iname '*.ape' -o \
-iname '*.flac' -o \
-iname '*.ogg' -o \
-iname '*.mp3' -o \
2013-10-12 16:01:46 +02:00
-iname '*.mp4' -o \
-iname '*.mpc' -o \
-iname '*.wav' -o \
2013-10-08 22:17:30 +02:00
-iname '*.wv' \) | sort -n)"
2013-10-08 22:17:30 +02:00
_worker()
{
CORE=$1
shift
## WARNING: ffmpeg should not be called from within a while<<EOF loop since
## it continues to read input while running.
OLDIFS=$IFS
IFS="
"
for file in $(echo "$INPUT" | sed -n "$CORE~${CPUNO}p"); do
if [ -n "$file" ]; then
echo "$(tput setf 2)$(tput bold)==>$(tput sgr0) $file"
[ $CPUNO -eq 1 ] && "${0%/*}"/tc-audio-transcode "$@" "$file" || \
tc-audio-transcode -q "$@" "$file" >/dev/null
fi
done
IFS=$OLDIFS
echo "$(tput setf 5)$(tput bold)::$(tput sgr0) Thread $CORE/$CPUNO terminated."
}
FILENO=$(echo "$INPUT" | wc -l)
if [ $CPUNO -gt $FILENO ]; then
CPUNO=$FILENO
fi
for i in $(seq 2 $CPUNO); do
_worker $i "$@" &
done
2013-10-08 22:17:30 +02:00
## We run one worker in the current process so that this script will hopefully
## terminate when the last job finishes.
_worker 1 "$@"