dotfiles/scripts/download-audio.sh

36 lines
844 B
Bash

#!/bin/sh
origin="$(pwd)"
cd $HOME/Downloads/youtube-dl
if [ "$1" = "--format" ]; then
format="$2"
[ -z "$3" ] && link="$(xclip -o)" || link="$3"
elif [ -z "$1" ]; then
link="$(xclip -o)"
else
link="$1"
if [ "$2" = "--format" ]; then
format="$3"
fi
fi
[ -z "$format" ] && format="opus"
if [ "$format" = "opus" ]; then
filename="$(youtube-dl -x --audio-quality 0 --audio-format opus --get-filename "$link")"
filename="$(echo "$filename" | sed -r "s|\..*$||").opus"
youtube-dl -x --audio-quality 0 --audio-format opus "$link"
size="$(stat --printf="%s" "$filename")"
if [ "$size" < 500000 ]; then
youtube-dl -x --audio-quality 0 --audio-format vorbis "$link"
rm "$filename"
fi
else
youtube-dl -x --audio-quality 0 --audio-format "$format" "$link"
fi
cd "$origin"