U redu ovako se trimuje višak.
Dodado sam i “refresh” dugme na dijalogu za odabir formata i malo očistio skript.
Code:
#!/bin/bash
[HEADING=1]DESCRIPTION: YAD frontend for youtube-dl[/HEADING]
[HEADING=1]LICENSE: GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)[/HEADING]
[HEADING=1]NOTICE: THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.[/HEADING]
[HEADING=1]EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES[/HEADING]
[HEADING=1]PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR[/HEADING]
[HEADING=1]IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY[/HEADING]
[HEADING=1]AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND[/HEADING]
[HEADING=1]PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,[/HEADING]
[HEADING=1]YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.[/HEADING]
[HEADING=1]IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY[/HEADING]
[HEADING=1]COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS[/HEADING]
[HEADING=1]PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,[/HEADING]
[HEADING=1]INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE[/HEADING]
[HEADING=1]THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED[/HEADING]
[HEADING=1]INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE[/HEADING]
[HEADING=1]PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER[/HEADING]
[HEADING=1]PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.[/HEADING]
[HEADING=1]based on a script by May 2016 @BunsenLabs[/HEADING]
[HEADING=1]Misko_2083 <Miloš Pavlović> June 2016[/HEADING]
[HEADING=1]Progress bar method used from Glutanimate’s yad_wget script[/HEADING]
[HEADING=1]https://github.com/Glutanimate/misc-scripts/blob/master/Internet/yad_wget[/HEADING]
declare -i SIMULTANEOUS
declare -i FIELDS
[HEADING=1]Set GUI variables up[/HEADING]
TITLE=“YAD youtube-dl wrapper” # dialog title
TEXT=“Downloads in progress…
Cancel and Clean- removes *.part files
Cancel - Just cancels the download” # progress dialog text
ICON=“browser-download” # window icon (appears in launcher)
IMAGE=“browser-download” # window image (appears in dialog)
declare -i MODE
MODE=1 # Defaiult dialog mode: 0 - Multi, 1 - Manual
#Multy download dialog options
SIMULTANEOUS=2 # set default number of simultaneous downloads (1-5)
FIELDS=8 # number of URL fields
[HEADING=1]Set global variables[/HEADING]
FORMAT="" # Set global variable for the format selection
ENTRY="" # Set global variable for the entry
[HEADING=1]Check youtube-dl is installed[/HEADING]
if ! hash youtube-dl &>/dev/null;then
DLG=$(yad --form
–borders=10 --image-on-top
–text=" youtube-dl not found\n\n Install it first and run the script again" --button=“gtk-ok:0”
–center --title “$TITLE” --window-icon “$ICON” --image “$IMAGE”
)
exit
fi
[HEADING=1]Check ffmpeg is installed[/HEADING]
if ! hash ffmpeg &>/dev/null;then
DLG=$(yad --form
–borders=10 --image-on-top
–text=" ffmpeg not found\n\n Install it first and run the script again" --button=“gtk-ok:0”
–center --title “$TITLE” --window-icon “$ICON” --image “$IMAGE”
)
exit
fi
download() {
cd $SAVEDIR
youtube-dl ${UTUBE} --newline -i -o “%(title)s-%(id)s.%(ext)s” “https://www.youtube.com/watch?v=$1” 2>&1
| while read -r line; do
echo “$2:#Preparing to download $1”
[CODE] if [[ "$(echo $line | grep '[0-9]*%')" ]];then
percent=$(echo $line | awk '{print $2}')
if [[ ! "$(echo "${percent}" | grep "100")" ]]; then #do not print 100% yet in case of conversion (mp3)
echo "$2:${percent%.*}%"
fi
fi
if [[ "$(echo $line | grep -E '\[download\]|\[ffmpeg\]')" ]];then
progress=$(echo $line | awk '{$1=""; print $0}')
echo "$2:#$progress"
fi
done
RET_YT="${PIPESTATUS[0]}"
if [[ “$RET_YT” = 0 ]]
then
echo “$2:100%”
echo “$2:#Download completed.”
Code:
else
echo "$2:#Download error."
fi
}
format_selection(){
#Format selection
FORMAT="$(youtube-dl -F “$YTEMP” --newline
| sed ‘s/audio only/audio only /;s/DASH video/DASH video /’
| awk -F’\s{2,}| , , ’ ‘NR>6 {for(i=1;i<=5;i++){print $i}}’
| yad --list --width=600 --height=500 --column=“format”
–column=“extension” --column=“resolution” --column=“type” --column=“note”
–button=“back!back:2” --button=“Refresh!reload:3” --button=“Exit!gtk-cancel:1” --button=“Download!gtk-ok:0”
–window-icon="$ICON" --image="$IMAGE" --image-on-top
–text “$FILENAME\nSelect a format to download” --borders=10
–center --title “$TITLE” --window-icon “$ICON” --image “$IMAGE”
)"
RET=$?
case “$RET” in
0) # Continue
;;
1) # Canceled
exit
;;
2)
main
;;
3)
format_selection
;;
*)
exit
;;
esac
Code:
FORMAT=$(echo "$FORMAT" | cut -d '|' -f 1)
if [[ -z $FORMAT ]];then
yad --text="You haven't selected a download format\nDo you want to return to the main window?" \
--borders=10 --form --button="No!gtk-cancel:1" --button="Yes!gtk-ok:0" \
--center --title "$TITLE" --window-icon "$ICON" --image "$IMAGE"
RET=$?
if (( $RET == 1 )) || (( $RET == 252 ));then # Canceled
exit
elif (( $RET == 0 )); then
main
fi
fi
}
download_format(){
FILENAME="$(youtube-dl --get-filename “$1”)"
YTEMP="$1" # temp variable to store the url
format_selection
}
dialog(){
if [[ “$MODE” = “0” ]]; then
OIFS=$IFS # save Internal Field Separator
IFS=$’\n’ # separator is “\n” in returned choices
c=0
YADFIELD=()
while [[ ${c} -lt $FIELDS ]];do
c=$(($c+1))
YADFIELD+=($(echo “–field=YouTube url: ${c}:NORM”; echo “”))
done
Code:
ENTRY="$(yad --form --columns=1 --window-icon "$ICON" --center \
--borders=10 --title "$TITLE" --image "$IMAGE" --image-on-top \
--text "Select a folder to download\n and paste URL(s) into any field" \
--button="Switch GUI!reload:2" --button="Exit!gtk-cancel:1" --button="Download!gtk-ok:0" \
--field="Enter Save directory:DIR" "$HOME/Downloads" \
--field="Download Options:CB" "Audio only (mp3)!Video (22/18/17)" \
--field="Simultaneous Downloads:NUM" "$SIMULTANEOUS!1..5!1" \
${YADFIELD[@]} \
)"
RET=$?
IFS=$OIFS # reset IFS back to default
case "$RET" in
0) # Continue
;;
1) # Canceled
exit
;;
2)
MODE=1
dialog
;;
*)
exit
;;
esac
elif [[ “$MODE” = “1” ]]; then
ENTRY="$(yad --form --columns=1 --window-icon “$ICON” --center
–borders=10 --title “$TITLE” --image “$IMAGE” --image-on-top
–text “Select a folder to download\n and paste URL(s) into any field”
–button=“Switch GUI!reload:2” --button=“Exit!gtk-cancel:1” --button=“Format!gtk-ok:0”
–field=“Enter Save directory
IR” “$HOME/Downloads”
–field=“Download Options:CB” “Manual”
–field=“Simultaneous Downloads:NUM” “1!1…1!1”
–field=“youtube url:” “” \ # Only one url field for the Manual format selection
)"
RET=$?
Code:
case "$RET" in
0) # Continue
;;
1) # Canceled
exit
;;
2)
MODE=0
dialog
;;
*)
exit
;;
esac
fi
}
main(){
while :; do
dialog # calls the main dialog
Code:
OIFS=$IFS # save Internal Field Separator
IFS="|" # separator is "|" in returned choices
i=0
retChoice=()
e=0
URLIST=()
for ret in $ENTRY;do
retChoice[$i]="$ret"
i=$(($i+1)) # iterate i
if [[ “$i” -gt 3 ]] && [[ “$ret” != “” ]];then # get form fields if they are not empty
ret="${ret%%&}" # trim
URLIST[$e]="${ret##’=’}" # extract filename
YADBARS="$YADBARS --bar=${URLIST[$e]}:NORM" # add bars to a yad multiprogress dialog
e=$(($e+1)) # iterate e
fi
done
Code:
IFS=$OIFS # reset IFS back to default
SAVEDIR=${retChoice[0]} # directory path
OPTION=${retChoice[1]}
case $OPTION in
"Video (22/18/17)")
UTUBE="-f 22/18/17"
;;
"Audio only (mp3)")
UTUBE="--extract-audio --audio-format mp3 --audio-quality 0"
;;
esac
MAXDLS=${retChoice[2]} # maximum number of simultaneous downloads
if [[ -z $SAVEDIR ]] || [[ ${#URLIST[@]} -eq 0 ]];then
yad --form --text="Enter at least one url field\nand select a folder" --borders=10 \
--center --title "$TITLE" --window-icon "$ICON" --image "$IMAGE" --button="gtk-ok:0"
else
break
fi
done
if [[ “$MODE” = “1” ]]; then
for URL in ${URLIST[@]}; do
download_format “https://www.youtube.com/watch?v=$URL” # call format selection dialog
UTUBE="-f $FORMAT"
done
fi
}
download_urls(){
OIFS=$IFS # save Internal Field Separator
IFS=" " # separator is " " in returned choices
COUNTER=“1”
DYNAMIC_COUNTER=“1”
PID=()
for URL in ${URLIST[@]}; do
if [[ “$DYNAMIC_COUNTER” = “$MAXDLS” ]] # only download n files at a time
then
download “$URL” “$COUNTER” & # pass URL and counter number to download()
PID+=("$!") # gets process pid
max="$(($COUNTER-1))" # -1, since start counter is 1, and arrays begin at 0
MAX_PID=${PID[$max]} # sets the variable pid from array PID
wait $MAX_PID # wait until process with MAX_PID finishes
DYNAMIC_COUNTER=“1” # reset dynamic counter
else
download “$URL” “$COUNTER” & # pass URL and counter number to download()
PID+=("$!")
DYNAMIC_COUNTER="$(($DYNAMIC_COUNTER+1))" # iterate dinamic counter
fi
COUNTER="$[$COUNTER+1]" # iterate counter
done | yad --multi-progress $YADBARS --title “$TITLE” --button=“Cancel and Clean!gtk-close:1” --button=“Cancel!gtk-close:0”
–text “$TEXT” --center --window-icon “$ICON” --image “$IMAGE” --image-on-top --auto-close
if (( $? == 1 ));then # Download canceled, so clean up
for pid in ${PID[@]};do
kill $pid &>/dev/null # kill any running download process
done
for url in ${URLIST[@]}; do
rm “$(find $SAVEDIR -type f -name “-$url.” | grep part)” &>/dev/null #removes the part file
done
notify-send -t 3000 --icon “dialog-info” “Download cancelled”
exit
fi
IFS=$OIFS # reset IFS back to default
}
main # Call the main loop
download_urls # Launch the download
[/CODE]