#!/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"