47 lines
1.1 KiB
Bash
47 lines
1.1 KiB
Bash
|
#!/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}
|