Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Download video URL (youtube-dl supported), and automatically open with VLC as soon as possible. Assign to global hotkey for easy use: (1. Copy URL. 2. Hit hotkey shortcut. 3. Wait a few seconds. 4. Enjoy in VLC)
#!/bin/bash
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 "Bad clipboard path: $clipboard"
fi
exit 1
fi
fi
#$1 processid, $2 tempdir $3 logfile
function cleanup {
echo "-------------------------------------------"
echo "Cleaning up"
kill -9 $1 &> /dev/null
if [ -n "$2" ] ; then rm $2/* ; fi
if [ -n "$2" ] ; then rmdir $2 ; fi
if [ -n "$3" ] ; then rm $3 ; fi
}
echo "Opening $path"
tmpvlcopen=/tmp/vlcopen/
[ ! -d $tmpvlcopen ] && mkdir $tmpvlcopen
tmpdir=$(mktemp -d --tmpdir=$tmpvlcopen)
# If the tmpdir path doesn't include "/tmp/vlcopen" something went bad!
if [ "${tmpdir/$tmpvlcopen}" = "$tmpdir" ] ; then
echo "Aborting: failed to create proper temporary directories" >&2
exit 1
fi
cd $tmpdir
echo "Temporary directory: $tmpdir"
echo "Getting video: $path"
downloadlog=$(tempfile)
(youtube-dl --restrict-filenames --no-part "$path" &> $downloadlog)&
processId=$!
echo "ProcessID: $processId"
echo "Waiting for file creation"
count=0
while [ $(ls -1 | wc -l) -eq 0 ] ; do
# check if process is done.
if ! kill -0 $processId &>/dev/null ; then
if [ -t 1 ] ; then
echo "-------------------------------------------">&2
echo "Error: youtube-dl finished without output." >&2
echo "Error: Bad URL?" >&2
echo "---------------- Download Log -------------">&2
cat $downloadlog >&2
else
notify-send "Bad URL: $path $(cat $downloadlog)"
fi
cleanup $processId "$tmpdir" "$downloadlog"
exit 0
fi
sleep 0.5
((count++))
if [ $count -eq 20 ] ; then
if [ -t 1 ] ; then
echo "Timed out" >&2
else
notify-send "Timed out"
fi
exit 0
fi
done
sleep 0.1
echo "Opening file: $(ls -1 | head -n 1)"
vlc "$(ls -1 | head -n 1)" &>/dev/null
cleanup $processId "$tmpdir" "$downloadlog"

jaseg commented Sep 10, 2014

Maybe try mpv, that's an mplayer fork that among other things is able to directly play youtube urls. Integration via a firefox shortcut is a one-liner if you are using pentadactyl.

bentley commented Sep 11, 2014

You should use “mktemp -p foo” instead of “mktemp --tmpdir=foo”; the former is more portable and works on BSDs and Mac OS X.

Doesn't work for me, I get these error messages (Arch Linux):

/home/***/.scripts/vlcopen.sh: line 44: tempfile: command not found
and
/home/**
/.scripts/vlcopen.sh: line 45: $downloadlog: ambiguous redirect

nvm, I made the script in python, here it is:

import subprocess, pyperclip
subprocess.Popen(["/usr/bin/vlc", pyperclip.paste()])

Gotta love Python.

I cleaned this script up a bit and fixed some of the errors I was getting in the process.

I added some configuration options and additional notifications. It now also uses inotifywait to wait for the download to start instead of polling the file.

Here it is: https://gist.github.com/jasonwhite/7e6c24ecd29508c1276d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment