Voici un script qui permet de chercher, télécharger et écouter/regarder sur son ordinateur des vidéos ou des musiques de Youtube. Cela permet aux anciens ordinateurs de profiter de Youtube sans la lourdeur du site (pubs, javascript, ...) et sans la lourdeur du navigateur ou via SSH.





0x01. SCRIPT


 
#!/bin/bash
 
 
 
#
# Path for temp files an programs
#
## Temp files
tmp="/tmp/youtube-dl.temp"
tmy="/tmp/youtube-dl.out"
tmf="/tmp/youtube-dl.mp4"
res="/tmp/youtube-dl.result.txt"
## Programs:
youtubedl="/usr/local/bin/youtube-dl"
openvideo="mplayer"
openmusic="mplayer"
 
 
 
#
# Settings by default
#
APP=$(basename $0)
MUSIC_DEFAULT="Y"
CHOICE_DEFAULT="Y"
 
 
 
#
# User-Agent
#
ua="Windows 10 / Firefox 39: Mozilla/5.0 (Windows NT 6.4; rv:39.0; fr_FR) Gecko/20100101 Firefox/39.0"
 
 
 
#
# If no args, then ask for it
#
if [ $# -eq 0 ]; then
  printf "\\n[x] Search .............: "           ; read search
  printf "[x] Is this a music ....? ($MUSIC_DEFAULT/n) "  ; read music
  printf "[x] Play after download ? ($CHOICE_DEFAULT/n) " ; read doirun
  [ -z "$music" ] && music="$MUSIC_DEFAULT"
  [ -z "$choice" ] && doirun="$CHOICE_DEFAULT"
  music=$(echo $music  |tr "A-Z" "a-z")
  doirun=$(echo $choice|tr "A-Z" "a-z")
elif [ $# -eq 3 ]; then
  search="$1"
  music="$2"
  doirun="$3"
else
  echo "
Usage: $APP <-i> | <search> <music> <choice>
 
    -i : interactive
 search : term of search
 music  : y or n (extract audio if yes)
 choice : play file or not after download 
 
Exemple:
--------
$APP -i
$APP napalm+death y y
"
  exit 0
fi
 
 
#
# URL encode space
#
search=$(echo "$search"|sed 's/ /+/g')
 
 
## Empty result file
echo  > "$res";
 
 
 
#
# Download result page and parse HTML
#
function youtube_search()
{
  search="$1"
  page="$2"
 
  ## Count / index for choice
  index="$3"
 
  ## Download Youtube web page
  wget -q \\
   --no-check-certificate --header "Accept-Language: en-us,fr-fr;q=0.8,en;q=0.5,fr;q=0.3" \\
   --header "Accept-Encoding: html" \\
   --header "DNT: 1" \\
   --user-agent "$ua" \\
   'https://www.youtube.com/results?search_query='$search'&page='$page -O "$tmp"
 
  ## Got by code analysis
  grep 'class="yt-uix-sessionlink yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2' $tmp | \\
   egrep -o 'href="/watch\\?v=(.){1,12}"|title="(.){1,}" rel='|\\
   egrep "^href=|^title="|cut -d'"' -f1,2|\\
   sed 's/href=/%href:/g'|tr "\\n" ";"|\\
   tr "%" "\\n"|\\
   while read line ; do
     link=$(echo $line|cut -d';' -f1|sed 's/href:"//g')
     title=$(echo $line|cut -d';' -f2|sed 's/title="//g')
     [ -z "$link" -o -z "$title" ] && continue
     i=$((i+1))
     [ -z "$link" ] && continue
     echo "$i;$title;$link" >> $res
   done
}
 
 
 
#
# Show result for choice
#
function display_results()
{
  while read line ; do
    i=$(echo $line|cut -d';' -f1)
    [ -z "$i" ] && continue
    link=$(echo $line|cut -d';' -f3)
    title=$(echo $line|cut -d';' -f2)
    printf "\\n<%-3s>   %s" "$i" "$title"
  done < "$res"
  printf "\\n\\n< + >   %s" "<More results>"
  printf "\\n< n >   %s" "<New search>"
  printf "\\n< q >   %s" "<Quit>"
}
 
 
 
#
#
#
clean_name()
{
  str="$1"
  echo $str|sed 's/\\!//g;s/\\*//g;s/\\?//g;s:\\/::g'
}
 
 
 
#
# Ask for choice, loop if choice is invalid
#
page=1
youtube_search $search $page 0
display_results
 
title=""
while true ; do
  printf "\\n\\n[?] Choice : " ; read choice
  ## Show more results
  if [ "$choice" = "+" ]; then
    page=$((page+1))
    count=$(tail -n1 "$res"|cut -d';' -f1)
    youtube_search $search $page $count
    display_results
  elif [ "$choice" = "n" ]; then
    $0
  elif [ "$choice" = "q" ]; then
    exit
  else
    link=https://www.youtube.com$(grep "^$choice;" $res|cut -d';' -f3)
    title=$(grep "^$choice;" $res|cut -d';' -f2)
    [ ! -z "$title" -a ! -z "$link" ] && break
  fi
done
 
 
 
#
# Display some info, download and convert
#
printf "\\n[-] Title  : $title"
printf "\\n[-] URL    : $link"
 
## Name the file with the title
[ "$music" = "y" ] && file="$(clean_name "$title.mp3")"
[ "$music" = "n" ] && file="$(clean_name "$title.mp4")"
 
## Download if file not already downloaded
if [ ! -f "$file" ]; then
  printf "\\n[-] File   : $file"
  if [ "$music" = "y" ]; then
    printf "\\n\\n[+] Downloading and converting to MP3 ...  \\n"
    $youtubedl --user-agent "$ua" --format mp4 -o "$tmf" "$link" 2>/dev/null
    printf "\\n[ffmpeg] Converting to '$file'..."
    ffmpeg -i "$tmf" -f mp3 -vn "$file" >/dev/null 2>&1
    [ -f "$file" ] && \\
     printf "\\n[ffmpeg] Conversion complete." || \\
     printf "\\n[error] Conversion failed."
    rm -f "$tmf"
  else
    printf "\\n\\n[+] Downloading video ...  \\n"
    $youtubedl --format mp4 --user-agent "$ua" "$link" -o  "$file"  2>/dev/null
  fi
else
  printf "\\n[-] File already downloaded : $file"
fi
 
 
## If default choice set to empty
[ -z "$choice" ] && ( printf "\\n[?] Play file ? (Y/n) " ; read choice ; choice=$(echo $choice|tr "A-Z" "a-z") )
 
## Clean temporary files
# rm -f "$tmp" "$tmy" "$res"
 
## yes by default
[ "$doirun" != "y" ] && exit
 
## Open file with user-defined program (by editting this script)
printf "\\n\\n"
[ "$music" = "y" ] && ( echo "[+] Running: $openmusic '$file'" ; "$openmusic" "$file" )
[ "$music" = "n" ] && ( echo "[+] Running: $openvideo '$file'" ; "$openvideo" "$file" )
printf "\\n\\n"
 



   =>   Écrit par : Nicolas, le 27 janvier 2016


 
Mots clés :  
  network 
  
  bash 
  
  linux 
  
  macos 
    >   Articles connexes :

GitVuln



HTTP Server, tell me who you are ?


Discuter avec un serveur web *apparement* muet ? Voici comment faire...

Chiffrement multicouche



HTTP Download



Cheat SHeet OpenSSL



Comment gagner du temps sur Internet



/tmp et /var/log en noexec sur macOS



8075309