#!/bin/bash ## ## yad dialog to download and save youtube video ## ## written by May 2016 ## ## Progress pipe method contributed by # Check youtube-dl is installed if ! type youtube-dl &>/dev/null;then DLG=$(yad --form --window-icon=distributor-logo-bunsenlabs \ --borders=10 \ --text=" youtube-dl not found\n\n Install it first and run the script again" --button="OK" \ --title="Youtube downloader" --center --undecorated \ ) exit fi while :; do ENTRY=$(yad --form --window-icon=distributor-logo-bunsenlabs --center \ --borders=10 \ --title="Youtube downloader" \ --field="Enter Save directory:DIR" \ --field="Enter youtube url" \ --field="Play file when downloaded:CHK" \ --field="Audio only:CHK" \ ) RET=$? OIFS=$IFS # save Internal Field Separator IFS="|" # separator is "|" in returned choices i=0 retChoice=() for ret in $ENTRY;do retChoice[$i]="$ret" i=$(($i+1)) done IFS=$OIFS # reset IFS back to default SAVEDIR=${retChoice[0]} # directory path URL=${retChoice[1]} URL=${URL##*'='} # extract filename PLAY=${retChoice[2]} AUDIO=${retChoice[3]} if (( $RET == 1 ));then # Cancelled exit fi if [[ -z $SAVEDIR ]] || [[ -z $URL ]];then yad --form --text="Complete both fields" --center --window-icon=distributor-logo-bunsenlabs else break fi done # Run youtube download, pipe to progress dialog if [[ $AUDIO == FALSE ]]; then UTUBE="youtube-dl --newline -i -o $SAVEDIR/%(id)s.%(ext)s $URL" else UTUBE="youtube-dl -f 141/bestaudio -i -o $SAVEDIR/%(id)s.%(ext)s $URL" fi $UTUBE 2>/dev/null | while read -r line ; do if [[ "$(echo $line | grep '[0-9]*%')" ]];then # get % of download percent=$(echo $line | awk '{print $2}') echo "${percent%.*}%" fi done | yad --progress --auto-close \ --window-icon=distributor-logo-bunsenlabs \ --center --undecorated --borders=10 \ --text="Youtube downloader\n\nDownloading: $URL" --button="gtk-cancel:1" if (( $PIPESTATUS == 1 ));then # Download cancelled, so clean up rm $(find $SAVEDIR -type f -name $URL.* | grep part) &>/dev/null notify-send -t 3000 --icon "dialog-info" "Download cancelled" exit fi if [[ $PLAY = TRUE ]] &>/dev/null;then xdg-open "$SAVEDIR/$URL".* fi exit 0