dotfiles/audio-dl

57 lines
1.4 KiB
Bash

#!/bin/bash
set -e
binary=yt-dlp
helptxt="Usage: audio-dl [OPTIONS] URL
-o, --output Specifies the output file, without the extension
-c, --convert Converts the file to the specified format
--yt-dl Use youtube-dl instead of yt-dlp
-h, --help Prints this text and exit"
function printerr() { echo "$*" >&2; }
if ! hash $binary 2>/dev/null ; then
printerr "You might wanna install $binary for this command buddy."
exit 1
elif [ $# -eq 0 ]; then
printf '%s\n' "$helptxt"
echo
printerr "You didn't enter any arguments, dumbass."
exit 1
elif [ $1 = "-h" ] || [ $1 = "--help" ]; then
printf '%s\n' "$helptxt"
exit 0
fi
arguments=("$@")
last_arg=""
filename=""
convert=""
video_url=""
for arg in "${arguments[@]}"; do
case $last_arg in
"-o" | "--output") filename="$arg" ;;
"-c" | "--convert") convert="$arg" ;;
"--yt-dl") binary="yt-dl" ;;
"" | *) video_url="$arg";;
esac
last_arg="$arg"
done
if [ "$filename" = "" ]; then
filename="%(title)s"
fi
$binary -o "${filename}.%(ext)s" -f 'bestaudio' "${video_url}"
file=$($binary -o "${filename}.%(ext)s" -f 'bestaudio' "${video_url}" --get-filename)
if [ "$convert" = "" ]; then
:
else
ffmpeg -i "${file}" "${file%.*}${convert}"
rm -f "${file}"
fi