|
- #!/usr/bin/env bash
-
- DEFAULT_BASEDIR=$(pwd)
- DEFAULT_CONVERTER="ffmpeg"
- FLACDIR="flac"
- MP3DIR="mp3"
-
- while [[ $# -gt 0 ]]; do
- opt="$1"
- shift;
- case "$opt" in
- "-d"|"--dir")
- BASEDIR="$1"
- shift;
- ;;
- "-c"|"--converter")
- CONVERTER="$1"
- shift;
- ;;
- "-h"|"--help")
- usage
- ;;
- esac
- done
-
- if [ -z ${BASEDIR} ]; then
- echo "No Basedir parameter given."
- BASEDIR="${DEFAULT_BASEDIR}"
- fi
-
- if [ -z ${CONVERTER} ]; then
- CONVERTER="${DEFAULT_CONVERTER}"
- fi
-
- usage() {
- cat<<EOF
- Usage: $0 [-d BASEDIR] [-c CONVERTER]
-
- -d, --dir BASEDIR The root directory where to start to convert recursively.
- -c, --converter CONVERTER The converter to use. Choices are "ffmpeg" or "libav"
- -h, --help This usage dialog
- EOF
- exit
- }
-
- convert_to_mp3() {
- NUM_FILES=$(find . -type f -iname '*.flac' | wc -l)
- CURRENT=${NUM_FILES}
- IFS=$'\n'; for FLACFILE in $(find ${BASEDIR}/${FLACDIR}/ -type f -iname '*.flac'); do
- SRCPATH=${FLACFILE}
- DSTPATH=$(echo ${FLACFILE} | sed "s|^${BASEDIR}/${FLACDIR}/|${BASEDIR}/${MP3DIR}/|g;s|\.flac$|\.mp3|g")
- SRCDIR=$(dirname "${SRCPATH}")
- DSTDIR=$(dirname "${DSTPATH}")
-
- if [ ! -d "${DSTDIR}" ]; then
- echo "Creating ${DSTDIR}."
- mkdir -p "${DSTDIR}"
- fi
-
- if [ ! -e "${DSTDIR}/folder.jpg" ]; then
- echo "Copying cover art into ${DSTDIR}."
- cp "${SRCDIR}/folder.jpg" "${DSTDIR}/folder.jpg"
- fi
-
-
- if [ ! -e "${DSTPATH}" ]; then
- echo "Converting file ${CURRENT} of ${NUM_FILES}: $(basename $FLACFILE) -> $(basename $DSTPATH)"
- if [ ${CONVERTER} == "libav" ]; then
- # Convert using libav
- avconv -i "${SRCPATH}" -c:a libmp3lame -b:a 192k -map_metadata 0:g:0 "${DSTPATH}"
- elif [ ${CONVERTER} == "ffmpeg" ]; then
- # Convert using ffmpeg
- ffmpeg -i "${SRCPATH}" -ab 192k -vcodec copy -map_metadata 0:g:0 -hide_banner -v 0 "${DSTPATH}"
- fi
- fi
-
-
-
- let CURRENT-=1
- done
- }
-
- echo "Using ${BASEDIR} as basedir"
- echo " Therefore we are converting from ${BASEDIR}/${FLACDIR} to ${BASEDIR}/${MP3DIR}"
- read -p "Press any key to continue... (CTRL+C to abort)" -n1 -s
- cd ${BASEDIR}
- convert_to_mp3
-
- # vim: set ts=2 sw=2 tw=0 noet :
|