Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 123 lines (97 sloc) 2.75 KB
#!/usr/bin/env bash
# flacart.sh
# Author: William Woodruff
# ------------------------
# Adds album artwork to FLAC files in bulk.
# Requires metaflac and GNU bash 4.0.
# ------------------------
# This code is licensed by William Woodruff under the MIT License.
# http://opensource.org/licenses/MIT
function usage {
echo "Usage: $(basename "${0}") [-svh] [-f artfile] [-j jobs] [directory]"
echo -e "\t-f <file> - Use <file> as artwork instead of looking for common artwork files"
echo -e "\t-s - convert files sequentially instead of spawning processes"
echo -e "\t-v - be verbose"
echo -e "\t-h - print this usage information"
echo -e "\t-j X - Number of jobs to use"
exit 1
}
function verbose {
[[ "${verbose}" ]] && echo "${*}"
}
function error {
>&2 echo "Fatal: ${*}. Exiting."
exit 2
}
function installed {
cmd=$(command -v "${1}")
[[ -n "${cmd}" ]] && [[ -f "${cmd}" ]]
return ${?}
}
function find_artfile {
common_files=(cover.{jp{e,}g,png} folder.{jp{e,}g,png})
for file in "${common_files[@]}"; do
[[ -f "${dir}/${file}" ]] && { printf "${dir}/${file}" ; return ; }
done
}
function flacart() {
local flac_file="${1}"
verbose "Beginning '${flac_file}."
metaflac --import-picture-from="${artfile}" "${flac_file}"
verbose "Completed '${flac_file}'."
}
[[ "${BASH_VERSINFO[0]}" -lt 4 ]] && error "GNU bash 4.0 or later is required"
shopt -s nullglob
shopt -s globstar
installed metaflac || error "Could not find metaflac to encode with"
while getopts ":f:svhj:" opt; do
case "${opt}" in
f ) artfile=${OPTARG} ;;
s ) sequential=1 ;;
v ) verbose=1 ;;
j ) njobs=${OPTARG} ;;
* ) usage ;;
esac
done
shift $((OPTIND - 1))
if [[ -n "${1}" ]]; then
dir="${1}"
[[ ! -d "${dir}" ]] && error "Not a directory: '${dir}'"
else
dir="."
fi
if [[ -z "${artfile}" ]]; then
common_files=(cover.{jp{e,}g,png} folder.{jp{e,}g,png})
for file in "${common_files[@]}"; do
[[ -f "${dir}/${file}" ]] && { artfile="${dir}/${file}" ; break ; }
done
if [[ -z "${artfile}" ]]; then
error "Could not find an artwork file. Use the -f flag to specify one"
fi
fi
flacs=("${dir}"/**/*.flac)
if [[ "${sequential}" ]]; then
verbose "Encoding sequentially."
for file in "${flacs[@]}"; do
flacart "${file}"
done
else
verbose "Encoding in parallel."
if installed "parallel"; then
export -f flacart verbose
export conv
export artfile
if [[ -z "${njobs}" ]]; then
printf '%s\n' "${flacs[@]}" | parallel -j+0 -q flacart
else
printf '%s\n' "${flacs[@]}" | parallel -j"${njobs}" -q flacart
fi
else
verbose "'parallel' is not installed, falling back to forking. Job control will NOT work in this mode."
for file in "${flacs[@]}"; do
flacart "${file}" &
done
wait
fi
fi
verbose "All done. ${#flacs[@]} files encoded with artwork."