From c1e1830eb94d5b6e54137dd1456d80e47f7bc969 Mon Sep 17 00:00:00 2001 From: Daniel Hoherd Date: Thu, 15 Dec 2016 19:54:19 -0800 Subject: [PATCH 1/4] Safety quotes, consistent variable braces. --- AAXtoMP3.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/AAXtoMP3.sh b/AAXtoMP3.sh index 5fc0331..be0be24 100755 --- a/AAXtoMP3.sh +++ b/AAXtoMP3.sh @@ -2,31 +2,31 @@ AUTHCODE=$1 shift while [ $# -gt 0 ]; do - FILE=$1 - echo "Decoding $FILE with AUTHCODE $AUTHCODE..." + FILE="$1" + echo "Decoding ${FILE} with AUTHCODE ${AUTHCODE}..." - ffmpeg -i "$FILE" 2> tmp.txt - TITLE=`grep -a -m1 -h -r "title" tmp.txt | head -1 | cut -d: -f2- | xargs -0` - TITLE=`echo $TITLE | sed -e 's/(Unabridged)//' | xargs -0` - ARTIST=`grep -a -m1 -h -r "artist" tmp.txt | head -1 | cut -d: -f2- | xargs` - GENRE=`grep -a -m1 -h -r "genre" tmp.txt | head -1 | cut -d: -f2- | xargs` - BITRATE=`grep -a -m1 -h -r "bitrate" tmp.txt | head -1 | rev | cut -d: -f 1 | rev | egrep -o [0-9]+ | xargs` + ffmpeg -i "${FILE}" 2> tmp.txt + TITLE=$(grep -a -m1 -h -r "title" tmp.txt | head -1 | cut -d: -f2- | xargs -0) + TITLE=$(echo "${TITLE}" | sed -e 's/(Unabridged)//' | xargs -0) + ARTIST=$(grep -a -m1 -h -r "artist" tmp.txt | head -1 | cut -d: -f2- | xargs) + GENRE=$(grep -a -m1 -h -r "genre" tmp.txt | head -1 | cut -d: -f2- | xargs) + BITRATE=$(grep -a -m1 -h -r "bitrate" tmp.txt | head -1 | rev | cut -d: -f 1 | rev | egrep -o [0-9]+ | xargs) BITRATE="${BITRATE}k" - OUTPUT=`echo $TITLE | sed -e 's/\:/-/g' | xargs -0` + OUTPUT=$(echo "${TITLE}" | sed -e 's/\:/-/g' | xargs -0) OUTPUT_DIR="${GENRE}/${ARTIST}/${TITLE}" - ffmpeg -v error -stats -activation_bytes $AUTHCODE -i "${FILE}" -vn -c:a libmp3lame -ab $BITRATE "${OUTPUT}.mp3" + ffmpeg -v error -stats -activation_bytes "${AUTHCODE}" -i "${FILE}" -vn -c:a libmp3lame -ab "${BITRATE}" "${OUTPUT}.mp3" echo "Created ${OUTPUT}.mp3." echo "Extracting chaptered mp3 files from ${OUTPUT}.mp3..." mkdir -p "${OUTPUT_DIR}" while read -r first _ _ start _ end; do - if [[ $first = Chapter ]]; then + if [[ "${first}" = "Chapter" ]]; then read read _ _ chapter - ffmpeg -v error -stats -i "${OUTPUT}.mp3" -ss "${start%?}" -to "$end" -acodec copy "${OUTPUT} - $chapter.mp3" < /dev/null - mv "${OUTPUT} - $chapter.mp3" "${OUTPUT_DIR}" + ffmpeg -v error -stats -i "${OUTPUT}.mp3" -ss "${start%?}" -to "${end}" -acodec copy "${OUTPUT} - ${chapter}.mp3" < /dev/null + mv "${OUTPUT} - ${chapter}.mp3" "${OUTPUT_DIR}" fi done < tmp.txt mv "${OUTPUT}.mp3" "${OUTPUT_DIR}" @@ -35,7 +35,7 @@ while [ $# -gt 0 ]; do rm tmp.txt echo "Extracting cover into ${OUTPUT_DIR}/cover.jpg..." - ffmpeg -v error -activation_bytes $AUTHCODE -i "$FILE" -an -vcodec copy "${OUTPUT_DIR}/cover.jpg" + ffmpeg -v error -activation_bytes "${AUTHCODE}" -i "${FILE}" -an -vcodec copy "${OUTPUT_DIR}/cover.jpg" echo "Done." shift From 69c1c2e77eaa653fe5809ebd878fe00e46db63af Mon Sep 17 00:00:00 2001 From: Daniel Hoherd Date: Thu, 15 Dec 2016 20:25:27 -0800 Subject: [PATCH 2/4] Check for ffmpeg. xargs echo. date on status updates. --- AAXtoMP3.sh | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/AAXtoMP3.sh b/AAXtoMP3.sh index be0be24..2e8ce44 100755 --- a/AAXtoMP3.sh +++ b/AAXtoMP3.sh @@ -1,42 +1,50 @@ #!/usr/bin/env bash AUTHCODE=$1 shift + +if ! command -v ffmpeg 2> /dev/null ; then + date "+%F %Tz ABORT: ffmpeg is missing" + exit 1 +fi + while [ $# -gt 0 ]; do FILE="$1" - echo "Decoding ${FILE} with AUTHCODE ${AUTHCODE}..." + echo "$(date "+%F %T%z") Decoding ${FILE} with AUTHCODE ${AUTHCODE}..." ffmpeg -i "${FILE}" 2> tmp.txt - TITLE=$(grep -a -m1 -h -r "title" tmp.txt | head -1 | cut -d: -f2- | xargs -0) - TITLE=$(echo "${TITLE}" | sed -e 's/(Unabridged)//' | xargs -0) - ARTIST=$(grep -a -m1 -h -r "artist" tmp.txt | head -1 | cut -d: -f2- | xargs) - GENRE=$(grep -a -m1 -h -r "genre" tmp.txt | head -1 | cut -d: -f2- | xargs) - BITRATE=$(grep -a -m1 -h -r "bitrate" tmp.txt | head -1 | rev | cut -d: -f 1 | rev | egrep -o [0-9]+ | xargs) + TITLE=$(grep -a -m1 -h -r "title" tmp.txt | head -1 | cut -d: -f2- | xargs echo ) + TITLE=$(echo "${TITLE}" | sed -e 's/ (Unabridged)//' | xargs echo ) + ARTIST=$(grep -a -m1 -h -r "artist" tmp.txt | head -1 | cut -d: -f2- | xargs echo ) + GENRE=$(grep -a -m1 -h -r "genre" tmp.txt | head -1 | cut -d: -f2- | xargs echo ) + BITRATE=$(grep -a -m1 -h -r "bitrate" tmp.txt | head -1 | rev | cut -d: -f 1 | rev | egrep -o [0-9]+ | xargs echo ) BITRATE="${BITRATE}k" - OUTPUT=$(echo "${TITLE}" | sed -e 's/\:/-/g' | xargs -0) + OUTPUT=$(echo "${TITLE}" | sed -e 's/\:/-/g' | xargs echo ) OUTPUT_DIR="${GENRE}/${ARTIST}/${TITLE}" ffmpeg -v error -stats -activation_bytes "${AUTHCODE}" -i "${FILE}" -vn -c:a libmp3lame -ab "${BITRATE}" "${OUTPUT}.mp3" - echo "Created ${OUTPUT}.mp3." + echo "$(date "+%F %T%z") Created ${OUTPUT}.mp3." - echo "Extracting chaptered mp3 files from ${OUTPUT}.mp3..." + echo "$(date "+%F %T%z") Extracting chaptered mp3 files from ${OUTPUT}.mp3..." mkdir -p "${OUTPUT_DIR}" + set -x while read -r first _ _ start _ end; do if [[ "${first}" = "Chapter" ]]; then read read _ _ chapter ffmpeg -v error -stats -i "${OUTPUT}.mp3" -ss "${start%?}" -to "${end}" -acodec copy "${OUTPUT} - ${chapter}.mp3" < /dev/null mv "${OUTPUT} - ${chapter}.mp3" "${OUTPUT_DIR}" + set +x fi done < tmp.txt mv "${OUTPUT}.mp3" "${OUTPUT_DIR}" - echo "Done creating chapters. Single file and chaptered files contained in ${OUTPUT_DIR}." + echo "$(date "+%F %T%z") Done creating chapters. Single file and chaptered files contained in ${OUTPUT_DIR}." rm tmp.txt - echo "Extracting cover into ${OUTPUT_DIR}/cover.jpg..." + echo "$(date "+%F %T%z") Extracting cover into ${OUTPUT_DIR}/cover.jpg..." ffmpeg -v error -activation_bytes "${AUTHCODE}" -i "${FILE}" -an -vcodec copy "${OUTPUT_DIR}/cover.jpg" - echo "Done." + echo "$(date "+%F %T%z") Done." shift done From f5c09d727099fa24377988b64d873d54bb2af189 Mon Sep 17 00:00:00 2001 From: Daniel Hoherd Date: Thu, 15 Dec 2016 20:54:53 -0800 Subject: [PATCH 3/4] read -r, trap and cleanup --- AAXtoMP3.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/AAXtoMP3.sh b/AAXtoMP3.sh index 2e8ce44..b726a46 100755 --- a/AAXtoMP3.sh +++ b/AAXtoMP3.sh @@ -7,6 +7,8 @@ if ! command -v ffmpeg 2> /dev/null ; then exit 1 fi +trap 'rm -f "tmp.txt"' EXIT TERM + while [ $# -gt 0 ]; do FILE="$1" echo "$(date "+%F %T%z") Decoding ${FILE} with AUTHCODE ${AUTHCODE}..." @@ -30,8 +32,8 @@ while [ $# -gt 0 ]; do set -x while read -r first _ _ start _ end; do if [[ "${first}" = "Chapter" ]]; then - read - read _ _ chapter + read -r + read -r _ _ chapter ffmpeg -v error -stats -i "${OUTPUT}.mp3" -ss "${start%?}" -to "${end}" -acodec copy "${OUTPUT} - ${chapter}.mp3" < /dev/null mv "${OUTPUT} - ${chapter}.mp3" "${OUTPUT_DIR}" set +x From 30b71ba73cc227c1946331b33add4a8beb5a744b Mon Sep 17 00:00:00 2001 From: Daniel Hoherd Date: Thu, 15 Dec 2016 21:03:51 -0800 Subject: [PATCH 4/4] Trap ctrl-c and exit 0 --- AAXtoMP3.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AAXtoMP3.sh b/AAXtoMP3.sh index b726a46..52621b4 100755 --- a/AAXtoMP3.sh +++ b/AAXtoMP3.sh @@ -7,7 +7,7 @@ if ! command -v ffmpeg 2> /dev/null ; then exit 1 fi -trap 'rm -f "tmp.txt"' EXIT TERM +trap 'rm -f "tmp.txt" ; exit 0 ;' EXIT TERM INT while [ $# -gt 0 ]; do FILE="$1"