2018-09-13 15:46:32 -07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Download audio using youtube-dl, passing
|
|
|
|
# a specific set of options specified by the user
|
|
|
|
|
|
|
|
printHelp() {
|
|
|
|
cat << EOF
|
|
|
|
Usage: dlaudio [options] [URLs]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-b, --batch-dl provide the links from a text file
|
2018-09-14 10:20:21 -07:00
|
|
|
-f, --format the format to use (default: flac)
|
2018-09-13 15:46:32 -07:00
|
|
|
-h, --help print this help page
|
|
|
|
-n, --filename the name of the downloaded file (without the extension)
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
declare -a opts
|
2018-09-14 10:20:21 -07:00
|
|
|
batchfile=
|
|
|
|
filename=
|
2018-09-15 12:34:58 -07:00
|
|
|
declare -r default_filename="--output=${HOME}/Music/%(title)s.%(ext)s"
|
2018-09-14 10:20:21 -07:00
|
|
|
format=
|
2018-09-13 15:46:32 -07:00
|
|
|
|
2018-09-14 10:20:21 -07:00
|
|
|
# error messages
|
2018-09-15 12:34:58 -07:00
|
|
|
declare -r nobatchfile_error="Error: no batch file entered"
|
|
|
|
declare -r nofilename_error="Error: no filename entered"
|
|
|
|
declare -r noformat_error="Error: no format entered"
|
2018-09-13 15:46:32 -07:00
|
|
|
|
|
|
|
while true; do
|
|
|
|
case "${1}" in
|
|
|
|
"-b"|"--batch-dl")
|
2018-09-14 10:20:21 -07:00
|
|
|
batchfile="${2}"
|
|
|
|
case "${batchfile}" in
|
2018-09-13 15:46:32 -07:00
|
|
|
"")
|
2018-09-14 10:20:21 -07:00
|
|
|
echo "${nobatchfile_error}" >&2
|
|
|
|
exit 1
|
2018-09-13 15:46:32 -07:00
|
|
|
;;
|
2018-09-14 10:20:21 -07:00
|
|
|
-*)
|
|
|
|
echo "Not a file: ${batchfile}"
|
2018-09-13 15:46:32 -07:00
|
|
|
exit 1
|
2018-09-14 10:20:21 -07:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[[ ! -f "${batchfile}" ]] && echo "Not a file: ${batchfile}" >&2 && exit 1
|
|
|
|
;;
|
2018-09-13 15:46:32 -07:00
|
|
|
esac
|
|
|
|
shift 2
|
|
|
|
continue
|
2018-09-14 10:20:21 -07:00
|
|
|
;;
|
2018-09-15 12:34:58 -07:00
|
|
|
--batch-dl=*)
|
|
|
|
batchfile="${1#*=}"
|
2018-09-14 10:20:21 -07:00
|
|
|
case "${batchfile}" in
|
|
|
|
"")
|
|
|
|
echo "${nobatchfile_error}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Not a file: ${batchfile}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[[ ! -f "${batchfile}" ]] && echo "Not a file: ${batchfile}" && exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
continue
|
2018-09-13 15:46:32 -07:00
|
|
|
;;
|
|
|
|
"-f"|"--format")
|
2018-09-14 10:20:21 -07:00
|
|
|
format="${2}"
|
|
|
|
case "${format}" in
|
2018-09-13 15:46:32 -07:00
|
|
|
"")
|
|
|
|
echo "No format entered" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
2018-09-14 10:20:21 -07:00
|
|
|
-*)
|
|
|
|
echo "Not a format: ${format}"
|
|
|
|
exit 1
|
2018-09-13 15:46:32 -07:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift 2
|
|
|
|
continue
|
|
|
|
;;
|
2018-09-14 10:20:21 -07:00
|
|
|
--format=*)
|
|
|
|
format="${1#*=}"
|
|
|
|
case "${format}" in
|
|
|
|
"")
|
|
|
|
echo "${noformat_error}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Not a format: ${format}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
;;
|
2018-09-13 15:46:32 -07:00
|
|
|
"-h"|"--help")
|
|
|
|
# print help page and exit
|
|
|
|
printHelp
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
"-n"|"--filename")
|
2018-09-14 10:20:21 -07:00
|
|
|
filename="${2}"
|
|
|
|
case "${filename}" in
|
2018-09-13 15:46:32 -07:00
|
|
|
"")
|
2018-09-14 10:20:21 -07:00
|
|
|
echo "${nofilename_error}" >&2
|
2018-09-13 15:46:32 -07:00
|
|
|
exit 1
|
|
|
|
;;
|
2018-09-14 10:20:21 -07:00
|
|
|
-*)
|
|
|
|
echo "Not a filename: ${filename}" >&2
|
|
|
|
exit 1
|
2018-09-13 15:46:32 -07:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift 2
|
|
|
|
continue
|
2018-09-14 10:20:21 -07:00
|
|
|
;;
|
|
|
|
--filename=*)
|
2018-09-15 12:34:58 -07:00
|
|
|
filename="${1#*=}"
|
2018-09-14 10:20:21 -07:00
|
|
|
case "${filename}" in
|
|
|
|
"")
|
|
|
|
echo "${nofilename_error}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Not a filename: ${filename}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
continue
|
2018-09-13 15:46:32 -07:00
|
|
|
;;
|
|
|
|
"--")
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
2018-09-15 02:40:20 -07:00
|
|
|
-*)
|
2018-09-14 10:20:21 -07:00
|
|
|
echo "Error, not an option: ${1}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
2018-09-13 15:46:32 -07:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# default options
|
|
|
|
opts+=("--no-part")
|
|
|
|
opts+=("--no-continue")
|
|
|
|
opts+=("--extract-audio")
|
|
|
|
opts+=("--audio-format=${format:-flac}")
|
|
|
|
|
2018-09-15 12:34:58 -07:00
|
|
|
# filename cannot be used at the same time as batch-dl
|
|
|
|
if [[ "${filename}" && -z "${batchfile}" ]]; then
|
2018-09-13 15:46:32 -07:00
|
|
|
opts+=("--output=${HOME}/Music/${filename}.%(ext)s")
|
2018-09-15 12:34:58 -07:00
|
|
|
elif [[ "${batchfile}" && -z "${filename}" ]]; then
|
|
|
|
opts+=("${default_filename}")
|
|
|
|
elif [[ "${batchfile}" && "${filename}" ]]; then
|
|
|
|
printf '%s\n' "Cannot pass '--batch-dl' and '--filename' together, ignoring"
|
|
|
|
opts+=("${default_filename}")
|
2018-09-13 15:46:32 -07:00
|
|
|
fi
|
|
|
|
|
2018-09-14 10:20:21 -07:00
|
|
|
if [[ "${batchfile}" ]]; then
|
2018-09-13 15:46:32 -07:00
|
|
|
youtube-dl "${opts[@]}" --batch-file="${batchfile}"
|
2018-09-15 12:34:58 -07:00
|
|
|
elif [[ "${*}" ]]; then
|
2018-09-13 15:46:32 -07:00
|
|
|
youtube-dl "${opts[@]}" "${@}"
|
2018-09-15 12:34:58 -07:00
|
|
|
else
|
|
|
|
printf '%s\n' "No arguments entered, cancelling"
|
2018-09-13 15:46:32 -07:00
|
|
|
fi
|