Skip to content

Instantly share code, notes, and snippets.

@tastytea
Last active May 21, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tastytea/b696076fed4003654e98e842c19ad689 to your computer and use it in GitHub Desktop.
Save tastytea/b696076fed4003654e98e842c19ad689 to your computer and use it in GitHub Desktop.
Adds an Youtube video to MPD
#!/bin/zsh
# Adds an Youtube video to MPD
if [ -z "${1}" ]; then
echo "usage: ${0} <URL>" >&2
exit 1
fi
# Split by newline
oldIFS=${IFS}
IFS=$'\n'
url="${1}"
# Set music_dir to the MPD music_directory
music_dir=~"/Musik"
tmpfile="addfromyoutube-$(date +'%s').m3u"
data=($(youtube-dl --get-title --get-url --audio-format best --extract-audio --get-duration ${url}))
title="${data[1]}"
newurl="${data[2]}"
duration=("${(@s/:/)data[3]}")
seconds=-1
if [ ${#duration} -eq 3 ]; then # HH:MM:SS
seconds=$((${duration[1]} * 60 * 60 + ${duration[2]} * 60 + ${duration[3]}))
elif [ ${#duration} -eq 2 ]; then # MM:SS
seconds=$((${duration[1]} * 60 + ${duration[2]}))
elif [ ${#duration} -eq 1 ]; then # SS
seconds=${duration[1]}
fi
# Write M3U
(
echo -e '#EXTM3U\n'
echo "#EXTINF:${seconds}, ${title}"
echo "${newurl}"
) > "${music_dir}/${tmpfile}"
mpc load "${tmpfile}"
rm "${music_dir}/${tmpfile}"
notify-send -u low -t 2000 "Added to MPD playlist" "${title}"
IFS=${oldIFS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment