added yt2mpd.sh

This commit is contained in:
tastytea 2018-06-10 21:17:18 +02:00
commit dc0e3ebb12
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
1 changed files with 46 additions and 0 deletions

46
yt2mpd.sh Executable file
View File

@ -0,0 +1,46 @@
#!/bin/zsh
# Adds an Youtube video to MPD, with correct title and duration.
# Change music_dir to the MPD music directory. It must be writable.
# youtube-dl must be installed. mpc must be installed and configured.
music_dir=~"/Musik"
if [ -z "${1}" ]; then
echo "usage: ${0} <URL>" >&2
exit 1
fi
# Split by newline
oldIFS=${IFS}
IFS=$'\n'
url="${1}"
tmpfile="yt2mpd-$(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}