[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: youtube video downloader for chrome



On Wed 20 Mar 2019 at 15:00:12 (-0400), rhkramer@gmail.com wrote:
> On Wednesday, March 20, 2019 02:10:57 PM David Wright wrote:
> > Actually I don't call youtube-dl as above, because I have two helper
> > functions which do things like history logging to prevent me
> > accidentally downloading the same video twice.
> 
> Are you willing to share?

Sure. geto(thers) is what gy-in-quotes calls:

function geto {
    local History="$HOME/.get-youtube-download-history"
    local Youtube="$HOME/Youtube"
    [ -z "$1" ] && printf '%s\n' "Usage: $FUNCNAME [-e] [--force] [-s] http...
	gets the specified video into $Youtube.
	If the file already exists (any .ext), it just continues.
	-e just echoes the command that would be used for downloading.
	--force overrides the check that prevents redownloading a file.
	Downloads are logged in $History" >&2 && return 1
    [ ! -r "$History" ] && printf '%s\n' "$History not found!" >&2 && return 1
    [ ! -d "$Youtube" ] && printf '%s\n' "Destination directory $Youtube not found!" >&2 && return 1
    local Echoonly="" Forcing="" Theurl=""
    while [ $# -gt 0 ] ; do
	case "$1" in
	    -e)
		Echoonly=echo
		;;
	    --force)
		Forcing=force
		;;
	    *)
		[ -n "$Theurl" ] && printf '%s\n' "Too many operands" && return 1
		Theurl="$1"
		;;
	esac
	shift
    done
    [ -z "$Theurl" ] && printf '%s\n' "Missing operand" && return 1
    grep -q -e "$Theurl" "$History"
    [ $? -eq 0 -a -z "$Forcing" ] && printf '%s\n' "$Theurl already in download history - override with --force" && return 1
    printf '%s\n' "youtube-dl $Theurl"
    [ -n "$Echoonly" ] && return 0
    ( cd "$Youtube" && youtube-dl -k "$Theurl" && printf '%s\n' "$Theurl" >> "$History" )
    printf '%s\n' "Remember to remove the audio/video files as appropriate."
}

geto handles all the different sites that youtube-dl supports.

The second helper I use is similar except that it only uses the last
11 characters of the argument (only with youtube.com), which can be
useful when cutting is awkward, or the code has to be typed from, say,
an image.

function gy {
    local History="$HOME/.get-youtube-download-history"
    local Youtube="$HOME/Youtube"
    [ -z "$1" ] && printf '%s\n' "Usage: $FUNCNAME [-e] [--force] [-s] foo-9_bar-8
	gets the specified video from www.youtube.com into $Youtube
	Only the last 11 characters of the argument are used.
	If the file already exists (any .ext), it just continues.
	-e just echoes the command that would be used for downloading.
	--force overrides the check that prevents redownloading a file.
	-s use a different format appropriate for my.strathspey.org
	Downloads are logged in $History" >&2 && return 1
    [ ! -r "$History" ] && printf '%s\n' "$History not found!" >&2 && return 1
    [ ! -d "$Youtube" ] && printf '%s\n' "Destination directory $Youtube not found!" >&2 && return 1
    local Echoonly="" Forcing="" Programmecode=""
    while [ $# -gt 0 ] ; do
	case "$1" in
	    -e)
		Echoonly=echo
		;;
	    --force)
		Forcing=force
		;;
	    -s)
		Spey=spey
		;;
	    *)
		[ -n "$Programmecode" ] && printf '%s\n' "Too many operands" && return 1
		Programmecode="$1"
		;;
	esac
	shift
    done
    [ -z "$Programmecode" ] && printf '%s\n' "Missing operand" && return 1
    Programmecode="${Programmecode:0-11:11}"
    [ -z "$Programmecode" ] && printf '%s\n' "Operand too short" && return 1
    grep -q -e "$Programmecode" "$History"
    [ $? -eq 0 -a -z "$Forcing" ] && printf '%s\n' "$Programmecode already in download history - override with --force" && return 1
    local Theurl=http://www.youtube.com/watch?v="$Programmecode";
    [ "$Spey" = "spey" ] && Theurl=https://www.youtube.com/v/"$Programmecode"?version=3
    printf '%s\n' "youtube-dl $Theurl"
    [ -n "$Echoonly" ] && return 0
    ( cd "$Youtube" && youtube-dl -k "$Theurl" && printf '%s\n' "$Theurl" >> "$History" )
    printf '%s\n' "Remember to remove the audio/video files as appropriate."
}

gy developed from getradio which treats 8 character BBC programme
codes in a similar way for get_iplayer, but the latter maintains
its own history cache. However, downloaded files don't retain their
timestamp, so bbc-redate does that empirically:

function getradio {
    [ -z "$1" ] && printf '%s\n' "Usage:	$FUNCNAME p00hgjc5
	gets the specified programme from the BBC radio website.
	Only the last 8 characters of the argument are used." >&2 && return 1
    local Programmecode="${1:0-8:8}"
    shift
    "$HOME"/bin/get_iplayer --pid "$Programmecode" --type radio --radiomode=worst,worse,good,vgood,better,best,daf,haf,hlsaac "$@"
    printf '%s\n' *"$Programmecode"* | tee /dev/stderr | wc -l | grep -q -e 1
    if [ $? -eq 0 ] ; then
	bbc-redate *"$Programmecode"*
    else
	printf '%s\n' "Too many files!" >&2
    fi
}

bbc-redate looks in each file for an occurrence of the pattern:
[12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]T[012][0-9]:[0-5][0-9]:[0-5][0-9]

Make of it all what you will.

Cheers,
David.


Reply to: