scripts/kodi.youtube

134 lines
4.3 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# From:
# https://github.com/NapoleonWils0n/ubuntu-bin/blob/master/yank
#===============================================================================
# send video links to kodi
#===============================================================================
#===============================================================================
# script usage
#===============================================================================
usage () {
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
2022-03-03 09:29:09 +01:00
$(basename "$0") -u url
-u http link or a video file.(mp4|mov|mkv|m4v|aac|m4a|wav|mp3)
-i IP.AD.DR.ESS (default=127.0.0.1)"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
2021-01-06 17:34:32 +01:00
#===============================================================================
# try to get a youtube URL from clipboard
#===============================================================================
is_clipboard_youtube_url () {
2022-03-03 09:29:09 +01:00
url_arg=$(xclip -out -selection clipboard)
if printf "%s" "${url_arg}" | grep -q -E "http.*youtube.*"; then
printf "%b\n" "Clipboard content can be used: ${url_arg}"
2021-01-06 17:34:32 +01:00
else
usage "${WRONG_ARGS_ERR}"
fi
}
#===============================================================================
# check number of aruments passed to script
#===============================================================================
2021-01-06 17:34:32 +01:00
[ $# -gt 0 ] || is_clipboard_youtube_url
#===============================================================================
# getopts check options passed to script
#===============================================================================
2022-03-03 09:29:09 +01:00
while getopts ':u:i:h' opt
do
case ${opt} in
2022-03-03 09:29:09 +01:00
u) url_arg="${OPTARG}";;
i) ip="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
2022-03-03 09:29:09 +01:00
#===============================================================================
# ip address localhost - change to your ip
#===============================================================================
## If ip wasn't defined
if [ -z "${ip}" ]; then
### Set it to localhost
ip='127.0.0.1'
fi
#===============================================================================
# url
#===============================================================================
# youtube-dl best format
2022-03-03 09:29:09 +01:00
url=$(youtube-dl -f best -g --no-playlist "${url_arg}")
# check url with ffprobe for errors and swap stderr and stdout to store error
url_check=$(ffprobe -hide_banner -v warning -i "${url}" 3>&2 2>&1 1>&3 >/dev/null)
# broken header error regex
atom_error='.*stream 1, missing mandatory atoms, broken header$'
error_check=$(expr "${url_check}" : "${atom_error}")
#===============================================================================
# one stream containing audio and video
#===============================================================================
one_stream () {
kodi-send --host="${ip}" --port=9777 --action="PlayMedia(${url},[noresume])" 1>/dev/null
}
#===============================================================================
# two streams containing audio and video
#===============================================================================
two_streams () {
2022-03-03 09:29:09 +01:00
url=$(youtube-dl -g --no-playlist "${url_arg}")
video_url=$(echo "${url}" | awk 'BEGIN{ RS ="" ; FS ="\n" }{print $1}')
audio_url=$(echo "${url}" | awk 'BEGIN{ RS ="" ; FS ="\n" }{print $2}')
# kodi-send
2021-01-06 17:34:32 +01:00
kodi-send --host="${ip}" --port=9777 --action="PlayMedia(udp://${ip}:1234?pkt_size=1316)" 1>/dev/null
# ffmpeg join audio and video and stream
ffmpeg \
-hide_banner \
-re \
-i "${video_url}" \
-i "${audio_url}" \
-c:a copy -c:v copy \
-tune zerolatency \
-map 0:0 -map 1:0 \
-f mpegts "udp://${ip}:1234?pkt_size=1316"
}
#===============================================================================
# case statement check for ffprobe error
#===============================================================================
case "${error_check}" in
0) one_stream;; # no error
*) two_streams;; # error
esac