Return to Snippet

Revision: 31442
at September 5, 2010 13:45 by ksaver


Initial Code
#! /usr/bin/env bash
#
#    eztube2.sh
#    Playing Youtube Videos without flash, using mplayer.
#    Revised and $(hopefully) better version.
#    ksaver (at identi.ca), Sep 2010.
#    Public Domain Code.
#    Dependences:  (Unix-like OS, python, bash implicit).
#                  Youtube-dl: http://bitbucket.org/rg3/youtube-dl/
#                  Zenity:     http://freshmeat.net/projects/zenity
#                  MPlayer:    http://www.mplayerhq.hu/
#
#    More Info: http://ur1.ca/0g3gu

scriptname=$(basename $0)
version='0.2.1'
dependences='zenity mplayer youtube-dl'

#Edit this as needed/
workdir="$HOME/Video"
wait_sec=60
##/

function check_dep()
    {
        for DEPEND in "$@"
        do
            /usr/bin/which $DEPEND &> /dev/null ||\
            (echo "[ERR]: Dependence '$DEPEND' Not found in your path!" &&\
            return 1)
            
        done
    }

function dl_video()
    {
        /usr/bin/env youtube-dl -q --continue "$1" -o "$2" &
        DLPID="$!"
    }
    
function get_url()
    {
        YTURL=$(zdialog --entry --text='Youtube Video URL:')
    }

function kill_dl()
    {
        PID=$1
        SEC=$2
        /bin/ps   -p  $PID &> /dev/null && waitload $SEC
        /bin/kill -15 $PID &> /dev/null
    }

function play_video()
    {
        /usr/bin/env mplayer -zoom "$1"
    }
    
function saveordrop()
    {
        zdialog --question --text="Save downloaded video?"
    }

function waitload()
    {
     #Not actually checking progress video size, just waits.
        CNT=0
        SEC=$1
        while [[ $CNT -lt $SEC ]]
        do
            echo  $(($CNT*100/$SEC)) #%percent%
            let   CNT=$CNT+1
            sleep 1
            
        done | zdialog --progress --auto-close \
               --text="Loading video...\nPlease wait about $wait_sec secs."
    }
        
function zdialog()
    {
        /usr/bin/env zenity --title="$scriptname $version" "$@"
    }
    
function _main_()
    {
        check_dep $dependences || exit
        
        YTURL=''
        while [[ ! $YTURL ]]
        do
            get_url || exit
        done         

        VIDNAME="$workdir/$(echo $YTURL |cut -d '&' -f 1 |cut -d '=' -f 2).flv"
        dl_video "$YTURL" "$VIDNAME"

        # If file already exists play it
        if [[ -f $VIDNAME ]]
        then
            play_video "$VIDNAME"
            
        else
            waitload $wait_sec
            play_video "$VIDNAME"
            
        fi
                
        #Save or delete file
        saveordrop
        if [ $? -eq 0 ]
        then
            FILENAME=$(zdialog --file-selection --filename="$VIDNAME")
            
            #wait to download...
            kill_dl $DLPID $wait_sec
            
            #and move downloaded video to new destiny
            /bin/mv "$VIDNAME" "$FILENAME"
            
        else
            #kill download and delete file
            kill_dl $DLPID 0
            if [ -f "$VIDNAME" ]
            then
                /bin/rm "$VIDNAME"
            fi
            
        fi        
    }

#Run script...
while true
do
    _main_
done

Initial URL
http://identi.ca/ksaver

Initial Description
Yes, my code sucks, but I hope it will be useful... :-)

Initial Title
EzTube: Playing YouTube Videos without Flash

Initial Tags
Bash

Initial Language
Bash