Welcome!
Important information
-- Spectre and Meltdown vulnerabilities
-- Change in MX sources

News
-- MX Linux on social media: here
-- Mepis support still here

Current releases
-- MX-17.1 Final release info here
-- antiX-17 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

A good name for a yad UI wrapper for youtube-dl?

Post Reply
Message
Author
spidermouse
Forum Novice
Forum  Novice
Posts: 27
Joined: Sat Mar 17, 2018 10:19 pm

A good name for a yad UI wrapper for youtube-dl?

#1 Post by spidermouse » Wed Apr 25, 2018 9:32 pm

I managed to make an old script to work last night.
The script downloads videos with the best video + best audio option available in ~/Videos.

I can't think of a good name for it.
Image
script requires xev,xdotool,yad,youtube-dl,wget
xev with -event option so it should work in debian jessie or newer.
xev is in x11-utils package

Code: Select all

#!/bin/bash

ERR(){ echo "ERROR: $1" 1>&2; }

declare -i DEPCOUNT=0
for DEP in /usr/bin/{xev,xdotool,yad,youtube-dl,wget}; {
	[ -x "$DEP" ] || {
		ERR "$LINENO Dependency '$DEP' not met."
		DEPCOUNT+=1
	}
}
[ $DEPCOUNT -eq 0 ] || exit 1

#download location
cd ~/Videos

export ytdownload='@bash -c "download_video %1"'

# We need this to store the youtube Link
export ytlink=$(mktemp  /tmp/ytlnk.XXXXXXXX)

export ytdpipe=$(mktemp -u /tmp/ytd.XXXXXXXX)
export ytdpipetwo=$(mktemp -u /tmp/ytd2.XXXXXXXX)
mkfifo "$ytdpipe"
mkfifo "$ytdpipetwo"

function on_exit {
    rm -f "$ytdpipe"
    rm -f "$ytdpipetwo"
    rm -f "$ytlink"
}
trap "on_exit" EXIT

ytdkey=$(($RANDOM * $$))

function download_video () {
  echo "2:@disabled@"
# Check if the URL is valid with the spider
if wget -q --spider "$1"; then
  echo "$1" > "$ytlink"
  echo "#Preparing to download" >> "$ytdpipe"

   while read line; do
     if [[ "$(echo $line | grep '[0-9]*%')" ]];then
        percent=$(echo $line | awk '{print $2}')
        echo "${percent%.*}" >> "$ytdpipe"
     fi

     if [[ "$(echo $line | grep '\[download\]')" ]];then
        progress=$(echo $line | awk '{$1=""; print $0}')
        echo "#$progress" >> "$ytdpipe"
     fi
   done < "$ytdpipetwo" &
   LOOP_PID="$!"

    youtube-dl -f 'bestvideo [ext = mp4] + bestaudio [ext = m4a] / bestvideo + bestaudio' --merge-output-format mp4 --newline -i -o "%(title)s.%(ext)s" "$1" 2>&1 >> $ytdpipetwo &
    ytproc_pid=$!
    wait $ytproc_pid 2>/dev/null  # supress message from kill command later

    if [[ "$?" = 0 ]]
      then
         echo "0" >> "$ytdpipe"
         echo "#Download complete." >> "$ytdpipe"
         kill "$LOOP_PID"
    elif [[ ! -s "$ytdpid" ]]; then
         echo "#Download canceled" >> "$ytdpipe"
         echo "0" >> "$ytdpipe"
         kill "$LOOP_PID"
    else
         echo "#Download error" >> "$ytdpipe"
         kill "$LOOP_PID"
    fi
else
    echo "#Invalid URL" >> "$ytdpipe"
fi

    echo "2:$ytdownload"
}
export -f download_video

function ytdl_version () {
    echo "#Checking youtube-dl version" >> "$ytdpipe"
    wget --spider --user-agent="Mozilla/5.0 Gecko/20100101" --timeout=30 -q "https://rg3.github.io/youtube-dl/" -O /dev/null
    if [[ "$?" -ne "0" ]]; then
       echo "#Can't connect to youtube-dl server site" >> "$ytdpipe"
    fi
    ytdlcv=$(youtube-dl --version)
    ytdllv=$(wget -O- -q "https://rg3.github.io/youtube-dl/update/LATEST_VERSION")
    if [[ "$ytdlcv" == "$ytdllv" ]]; then
       echo "#youtube-dl is up-to-date"  >> "$ytdpipe"
    else
       echo "#youtube-dl update is available" >> "$ytdpipe"
    fi
}
export -f ytdl_version

function stop_clicked () {
   if [[ ! -s "${ytlink}" ]]; then
      echo "#Nothing to stop." >> "$ytdpipe"
   else
      YoutubeLink="$(cat $ytlink)"
      YtCommand="youtube-dl -f bestvideo [ext = mp4] + bestaudio [ext = m4a] / bestvideo + bestaudio --merge-output-format mp4 --newline -i -o %(title)s.%(ext)s $YoutubeLink"
      YoutubePID="$(ps -eo pid,cmd | grep -F "$YtCommand" | grep -v "grep" | awk '{ print $1 }')"
      if [[ "$YoutubePID" != "" ]]; then
         kill -9 $YoutubePID
      fi
   fi
}
export -f stop_clicked

exec 3<> $ytdpipe
exec 4<> $ytdpipetwo

yad --plug="$ytdkey" --tabnum=1 --form --field "Enter video url":CE "" \
    --image=browser-download --field="Download!browser-download:fbtn" "$ytdownload" &

yad --plug="$ytdkey" --tabnum=2 --window-icon="$ICON" \
      --progress  --borders=6 --auto-close --auto-kill <&3 &

  echo "#Enter video URL." >> "$ytdpipe"
  #ytdl_version &

yad --class="YtDownloader" --paned --key="$ytdkey" \
    --button="Check yoututube-dl":'bash -c "ytdl_version" 2>/dev/null' \
    --button="Stop":'bash -c "stop_clicked" 2>/dev/null' --text="" --width=500 --height=200 \
    --title=$"???" --window-icon="browser-download" --center &
YPID="$!"

# Wait untill window opend, that is until windowname appears
until xdotool getwindowname "$(xdotool search --any --pid $YPID --class "YtDownloader" 2>/dev/null | tail -1 2>/dev/null)" &>/dev/null; do
        # sleep until the window opens
        sleep .1
done

# Decimal window id
WindowID="$(xdotool search --any --pid $YPID --class "YtDownloader" 2>/dev/null | tail -1)"

# Hexadecimal
WindowIDHex=$(printf "0x%08x" ${WindowID})

# Base seven
WindowBSeven=$(printf "0x%07x" ${WindowID})

# This is needed because youtube-dl prevents one yad --paned tab from closing
# Monitor for DestroyNotify event and kill youtube-dl
xev -event structure -id ${WindowIDHex} 2>/dev/null |
       while IFS=$',' read -a A; do
          if [[ "${A[0]}" =~ "DestroyNotify event" ]]; then
             YoutubeLink="$(cat $ytlink)"
             YtCommand="youtube-dl -f bestvideo [ext = mp4] + bestaudio [ext = m4a] / bestvideo + bestaudio --merge-output-format mp4 --newline -i -o %(title)s.%(ext)s $YoutubeLink"
             YoutubePID="$(ps -eo pid,cmd | grep -F "$YtCommand" | grep -v "grep" | awk '{ print $1 }')"
             if [[ "$YoutubePID" != "" ]]; then
                  kill -9 $YoutubePID
                  wait $YoutubePID
             fi
             XevPID="$(ps -eo pid,cmd | grep "xev -event structure -id $WindowIDHex" | grep -v "grep" | awk '{ print $1 }')"
             kill $XevPID 
             wait $XevPID 2> /dev/null
          fi
         A=()
       done & xev_pid=$!

wait $YPID
echo $?

wait $xev_pid

exec 3>&-
exec 4>&-

exit 0

User avatar
Stevo
Forum Veteran
Forum Veteran
Posts: 16310
Joined: Fri Dec 15, 2006 8:07 pm

Re: A good name for a yad UI wrapper for youtube-dl?

#2 Post by Stevo » Wed Apr 25, 2018 10:21 pm

Youtube-dlg (or download-gui) is already taken. One obvious one that comes to me is "Yadtube-dl".

skidoo
Forum Regular
Forum Regular
Posts: 864
Joined: Tue Sep 22, 2015 6:56 pm

Re: A good name for a yad UI wrapper for youtube-dl?

#3 Post by skidoo » Wed Apr 25, 2018 10:31 pm

itchyvideo ( i try to collect high-quality video )

Thanks for including comments inline within the script. Still, I had to go lookup the 3>&- redirection

suggestion: add details from your post into the script header

### Downloads videos (with the best video + best audio option available) into ~/Videos
### Requires: xev (provided by "x11-utils" package), xdotool, yad, youtube-dl, wget
### Note: uses xev with -event option (so, should work in debian jessie or newer)
https://github.com/rg3/youtube-dl/blob/ ... -selection

bestvideo: Select the best quality video-only format (e.g. DASH video). May not be available.
Can forcing this lead to an otherwise inexplicable "video not available" result?
(I think so. Using previous scripts, passing to youtube-dl the url of a video I had just watched in browser yielded a "not available" result.)

Post Reply

Return to “Chat”