Skip to content

Instantly share code, notes, and snippets.

@jasonwhite jasonwhite/vlcopen.sh
Last active Dec 25, 2016

Embed
What would you like to do?
Opens a youtube-dl supported video URL in your clipboard in VLC (or another video player).
#!/bin/bash
# Opens the video on your clipboard and plays it in VLC. I recommend binding
# this to a keyboard shortcut.
#
# This script is based on:
# https://gist.github.com/swarminglogic/0cd2f6d55390243e693b
#
# Usage: vlcopen.sh
#
# Authors: Roald Fernandez, Jason White
#
# Dependencies:
# * xclip (for accessing your X11 clipboard)
# * youtube-dl (for downloading the video)
# * vlc (for playing the video of course)
# * inotify-tools (for inotifywait)
# Timeout (in seconds) after which to give up on downloading the video.
timeout=10
# Time to wait for the video to buffer before playing. Increase/decrease this
# depending on the quality of your connection. VLC will fail to auto-play the
# video if there isn't enough data downloaded yet.
buffertime=2
# Video player to play the video with.
player=vlc
# Video quality control (see Video Format Options in youtube-dl man page for
# more info).
quality="--max-quality 18" # Selects 360p as max quality
#quality="--max-quality best" # Selects the best possible quality
if [ $# -eq 1 ] ; then
path=$1
else
clipboard=$(xclip -selection clipboard -o)
if [ $(<<<$clipboard grep -P "^http") ] ; then
path=$clipboard
else
if [ -t 1 ] ; then
echo "Bad clipboard path: $clipboard" >&2
else
notify-send "Invalid Video URL" "$clipboard"
fi
exit 1
fi
fi
# Let the user know something is happening
! [[ -t 1 ]] && notify-send "Opening video..." "Just wait a sec, okay?"
# Parameters: $1 processid, $2 video, $3 log
function cleanup {
kill -9 $1 &> /dev/null
rm -f -- "$2" "$3"
}
# Video path
video=$(mktemp --tmpdir=/tmp)
# Download log path
log=$(mktemp --tmpdir=/tmp)
echo "Getting video: $path"
(youtube-dl --no-part $quality --output $video -- "$path" &> $log)&
processId=$!
#echo "ProcessID: $processId"
echo -n "Waiting for download to start..."
# Wait for the video to be written to. Time out after 10 seconds.
inotifywait -e modify -t $timeout $video &> /dev/null
if [ $? -ne 0 ]; then
echo
case $? in
2)
notify-send "Video Download Timed Out" "Download did not start after $timeout seconds.\n$path"
;;
*)
notify-send "Video Download Failed" "The video failed to download:\n$path"
[[ -t 1 ]] && cat $log >&2
;;
esac
cleanup $processId $video $log
exit 1
fi
echo " Done."
# Wait a couple of seconds for it to buffer. Otherwise VLC may not auto-play the
# video.
sleep $buffertime
# Open in VLC
$player -- "$video" &> /dev/null
# Clean up our mess after the video player is closed.
cleanup $processId "$video" "$log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.