Shell: audio tc batch+cover support finished.

master
Pierre Neidhardt 2013-01-27 20:59:29 +01:00
parent c3da2d8b3e
commit 66c427e7c9
1 changed files with 56 additions and 24 deletions

View File

@ -78,7 +78,8 @@ tc_text_2utf8 ()
## For the sake of simplicity we convert everything to OGG.
## You can easily set output folder to current folder with TC_AUDIO_DEST=".".
TC_AUDIO_DEST="/media/data1/Musics/"
# TC_AUDIO_DEST="/media/data1/Musics/"
TC_AUDIO_DEST="$HOME/audio/"
## OGG quality ranges from -1 to 10.
## -q-1 45 kbit/s
@ -108,15 +109,43 @@ tc_audio_scan()
echo "void"
}
## TODO: cover resolution check. Should be > 100x100. Above <1000x1000 should give a warning.
## TODO: extract cover from tags if checksum does not match any from current folder.
tc_audio_batch()
{
OLDIFS=$IFS
IFS="
"
for i in $(find "." \( \
-iname '*.mp3' -o \
-iname '*.flac' -o \
-iname '*.wv' -o \
-iname '*.aac' -o \
-iname '*.wav' -o \
-iname '*.ape' -o \
-iname '*.mpc' \) ); do
tc_audio_transcode "$@" $i
done
# for i in *.mp3 ; do tc_audio_transcode "$@" $i ; done
# tc_audio_transcode "$@" "01 James Russo - Winged.mp3"
IFS=$OLDIFS
unset OLDIFS
}
## This function is supposed to work on a per-album basis. Folders will not be
## accepted as argument. This function will work best if all tracks of a folder
## belong to the same album. Theres is no way to handle covers raliably, so you
## should leave the only covers you want to keep in the working folder.
## TODO: cover resolution check. Should be > 100x100. Above <1000x1000 should give a warning.
## TODO: extract cover from tags if checksum does not match any from current folder.
tc_audio_transcode()
{
IFS=${OLDIFS:-$IFS}
_printhelp ()
{
cat <<EOF
@ -199,7 +228,7 @@ EOF
local OUTPUT
local OUTPUT_FILE
OUTPUT='$TC_AUDIO_DEST/$OUTPUT_ARTIST/${OUTPUT_ALBUM:+${OUTPUT_YEAR:+$OUTPUT_YEAR - }$OUTPUT_ALBUM/}'
OUTPUT_FILE='$OUTPUT$OUTPUT_ARTIST - ${OUTPUT_PADDEDTRACK:+$OUTPUT_PADDEDTRACK - }$OUTPUT_TITLE.$OUTPUT_FORMAT'
OUTPUT_FILE='$OUTPUT$OUTPUT_ARTIST - ${OUTPUT_PADDEDTRACK:+$OUTPUT_PADDEDTRACK - }$OUTPUT_TITLE'
while getopts ":a:b:d:g:l:n:t:hps" opt; do
case $opt in
@ -250,6 +279,12 @@ EOF
METADATA=$(echo "$STREAM" | sed -n '/Metadata/ ! d; /Metada/{b cont}; :cont ; {n;p;b cont}')
STREAM=$(echo "$STREAM" | grep "Stream")
if [ -z "$STREAM" ]; then
echo "Non-audio file."
return
fi
## We get format from extension, because codec is not reliable either.
local FORMAT
FORMAT="${1##*.}"
@ -292,7 +327,7 @@ EOF
TYER=$(_metadata_filter "TYER")
##================================================================================
## OUTPUT_* variables.
## OUTPUT variables.
_string_cleanser()
{
@ -333,12 +368,11 @@ EOF
## We remove the track count if any, we suppress leading zeros.
OUTPUT_TRACK=$(eval _string_cleanser $OUTPUT_TRACK | sed -e 's/^0*//' -e 's|/.*||')
## FIXME: ...
## If DATE is not a year, we use TYER if it is a year.
OUTPUT_DATE=$(eval _string_cleanser $OUTPUT_DATE)
local TYER_REG=$(echo "$TYER" | sed -n 's/.*\([[:digit:]]\{4\}\).*/\1/p')
if [ ${#DATE} -ne 4 ] && [ ${#TYER} -eq 4 ]; then
OUTPUT_DATE="$TYER"
if [ ${#DATE} -ne 4 ] && [ ${#TYER_REG} -eq 4 ]; then
OUTPUT_DATE="$TYER_REG"
fi
## Only reencode if not in OGG and if SKIP not set.
@ -346,7 +380,7 @@ EOF
[ "$FORMAT" = "ogg" ] && OGG_PARAM=""
## QUALITY
## TODO: test if table is worth it.
## TODO: test if table is useful at all.
[ $OUTPUT_BITRATE -eq 0 ] && OUTPUT_BITRATE=${BITRATE}
# if [ $OUTPUT_BITRATE -eq 0 ]; then
# [ $BITRATE -le 45 ] && OUTPUT_BITRATE=45k
@ -404,7 +438,8 @@ EOF
echo "TRACK [$OUTPUT_TRACK]"
echo
echo "==> OUTPUT"
echo "[$OUTPUT_FILE]"
echo "[$OUTPUT_FILE.$OUTPUT_FORMAT]"
echo
[ $PREVIEW -ne 0 ] && return
@ -418,31 +453,27 @@ EOF
return
fi
## COVER
## We copy cover only if it does not already exist.
local OUTPUT_COVER
local COVER_COUNTER
OUTPUT_COVER="$OUTPUT/"
[ -n "$ALBUM" ] && OUTPUT_COVER="$OUTPUT_COVER$ALBUM - "
OUTPUT_COVER="$OUTPUT/${OUTPUT_ALBUM:+$OUTPUT_ALBUM - }Cover"
for i in $(find "." \( \
-iname '*.png' -o \
-iname '*.jpg' \) ); do
OUTPUT_COVER="${OUTPUT_COVER}Cover.${i##*.}"
OUTPUT_COVER="${OUTPUT_COVER}.${i##*.}"
COVER_COUNTER=1
if [ ! "$(sha1sum $OUTPUT_COVER | cut -f1 -d' ')" = "$(sha1sum $i | cut -f1 -d' ')" ]; then
while [ -e "$OUTPUT_COVER" ] && \
[ ! "$(sha1sum $OUTPUT_COVER | cut -f1 -d' ')" = "$(sha1sum $i | cut -f1 -d' ')" ]; do
OUTPUT_COVER="${OUTPUT_COVER%.*} $COVER_COUNTER.${i##*.}"
COVER_COUNTER=$(($COVER_COUNTER+1))
done
while [ -e "$OUTPUT_COVER" ]; do
OUTPUT_COVER="${OUTPUT_COVER}Cover $COVER_COUNTER.${i##*.}"
COVER_COUNTER=$(($COVER_COUNTER+1))
done
## DEBUG only.
# echo "cp -nv $i [$OUTPUT_COVER]"
cp -nv "$i" "$OUTPUT_COVER"
fi
## DEBUG only.
# echo "cp -nv $i [$OUTPUT_COVER]"
cp -nv "$i" "$OUTPUT_COVER"
done
## Zsh compatibility. We need it otherwise word splitting of parameter like
@ -452,6 +483,7 @@ EOF
[ "$STATUS" = "off" ] && set -o shwordsplit
## TAG/RECODE
# ffmpeg -i "$1" -c:a libvorbis -b:a ${OUTPUT_BITRATE}k \
ffmpeg -i "$1" $OGG_PARAM \
-metadata title="$OUTPUT_TITLE" \
-metadata artist="$OUTPUT_ARTIST" \