2016-07-13 11:25:43 +02:00
|
|
|
#!/usr/bin/env bash
|
2016-03-31 04:48:35 +02:00
|
|
|
AUTHCODE=$1
|
|
|
|
shift
|
2016-12-16 05:25:27 +01:00
|
|
|
|
|
|
|
if ! command -v ffmpeg 2> /dev/null ; then
|
|
|
|
date "+%F %Tz ABORT: ffmpeg is missing"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-03-31 04:48:35 +02:00
|
|
|
while [ $# -gt 0 ]; do
|
2016-12-16 04:54:19 +01:00
|
|
|
FILE="$1"
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Decoding ${FILE} with AUTHCODE ${AUTHCODE}..."
|
2016-03-31 04:48:35 +02:00
|
|
|
|
2016-12-16 04:54:19 +01:00
|
|
|
ffmpeg -i "${FILE}" 2> tmp.txt
|
2016-12-16 05:25:27 +01:00
|
|
|
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 )
|
2016-03-31 04:48:35 +02:00
|
|
|
BITRATE="${BITRATE}k"
|
2016-12-16 05:25:27 +01:00
|
|
|
OUTPUT=$(echo "${TITLE}" | sed -e 's/\:/-/g' | xargs echo )
|
2016-03-31 04:48:35 +02:00
|
|
|
OUTPUT_DIR="${GENRE}/${ARTIST}/${TITLE}"
|
|
|
|
|
2016-12-16 04:54:19 +01:00
|
|
|
ffmpeg -v error -stats -activation_bytes "${AUTHCODE}" -i "${FILE}" -vn -c:a libmp3lame -ab "${BITRATE}" "${OUTPUT}.mp3"
|
2016-03-31 04:48:35 +02:00
|
|
|
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Created ${OUTPUT}.mp3."
|
2016-03-31 04:48:35 +02:00
|
|
|
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Extracting chaptered mp3 files from ${OUTPUT}.mp3..."
|
2016-03-31 04:48:35 +02:00
|
|
|
mkdir -p "${OUTPUT_DIR}"
|
2016-12-16 05:25:27 +01:00
|
|
|
set -x
|
2016-03-31 04:48:35 +02:00
|
|
|
while read -r first _ _ start _ end; do
|
2016-12-16 04:54:19 +01:00
|
|
|
if [[ "${first}" = "Chapter" ]]; then
|
2016-03-31 04:48:35 +02:00
|
|
|
read
|
|
|
|
read _ _ chapter
|
2016-12-16 04:54:19 +01:00
|
|
|
ffmpeg -v error -stats -i "${OUTPUT}.mp3" -ss "${start%?}" -to "${end}" -acodec copy "${OUTPUT} - ${chapter}.mp3" < /dev/null
|
|
|
|
mv "${OUTPUT} - ${chapter}.mp3" "${OUTPUT_DIR}"
|
2016-12-16 05:25:27 +01:00
|
|
|
set +x
|
2016-03-31 04:48:35 +02:00
|
|
|
fi
|
|
|
|
done < tmp.txt
|
|
|
|
mv "${OUTPUT}.mp3" "${OUTPUT_DIR}"
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Done creating chapters. Single file and chaptered files contained in ${OUTPUT_DIR}."
|
2016-03-31 04:48:35 +02:00
|
|
|
|
|
|
|
rm tmp.txt
|
2016-07-13 11:58:43 +02:00
|
|
|
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Extracting cover into ${OUTPUT_DIR}/cover.jpg..."
|
2016-12-16 04:54:19 +01:00
|
|
|
ffmpeg -v error -activation_bytes "${AUTHCODE}" -i "${FILE}" -an -vcodec copy "${OUTPUT_DIR}/cover.jpg"
|
2016-12-16 05:25:27 +01:00
|
|
|
echo "$(date "+%F %T%z") Done."
|
2016-07-13 11:58:43 +02:00
|
|
|
|
2016-03-31 04:48:35 +02:00
|
|
|
shift
|
|
|
|
done
|