Files
arch-dotfiles/scripts/download-youtube-xjadeo.sh

130 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Script de téléchargement et conversion pour xjadeo
# Dépendances : yt-dlp, ffmpeg
set -euo pipefail
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonction d'affichage
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[OK]${NC} $1"; }
error() { echo -e "${RED}[ERREUR]${NC} $1" >&2; }
warn() { echo -e "${YELLOW}[ATTENTION]${NC} $1"; }
# Vérification des dépendances
check_deps() {
local missing=()
if ! command -v yt-dlp &> /dev/null; then
missing+=("yt-dlp")
fi
if ! command -v ffmpeg &> /dev/null; then
missing+=("ffmpeg")
fi
if [ ${#missing[@]} -ne 0 ]; then
error "Dépendances manquantes : ${missing[*]}"
error "Installe-les avec : sudo pacman -S yt-dlp ffmpeg (Arch) ou sudo apt install yt-dlp ffmpeg (Debian/Ubuntu)"
exit 1
fi
}
# Demande de l'URL
get_url() {
if [ $# -eq 1 ]; then
URL="$1"
info "URL fournie en argument : $URL"
else
read -rp "URL YouTube : " URL
fi
if [[ -z "$URL" ]]; then
error "Aucune URL saisie."
exit 1
fi
}
# Demande du nom de fichier
get_filename() {
# Essaie d'extraire un titre temporaire avec yt-dlp --get-title
local suggested=""
if command -v yt-dlp &> /dev/null; then
suggested=$(yt-dlp --get-title "$URL" 2>/dev/null | sed 's/[^a-zA-Z0-9._-]/_/g' | head -c 80)
fi
if [ -z "$suggested" ]; then
suggested="video"
fi
read -rp "Nom du fichier (sans extension) [$suggested] : " input_name
FILENAME="${input_name:-$suggested}"
}
# Téléchargement
download_video() {
info "Téléchargement de la vidéo depuis $URL..."
# Télécharge la meilleure qualité vidéo+audio et fusionne en MKV
yt-dlp -f "bestvideo+bestaudio" --merge-output-format mkv -o "${FILENAME}.%(ext)s" "$URL"
# Recherche le fichier produit (peut être .mkv ou .webm)
SOURCE_FILE=$(find . -maxdepth 1 -name "${FILENAME}.*" -type f | head -1)
if [ -z "$SOURCE_FILE" ]; then
error "Fichier téléchargé introuvable."
exit 1
fi
success "Téléchargement terminé : $SOURCE_FILE"
}
# Proposer la conversion
ask_conversion() {
echo ""
read -rp "Souhaites-tu convertir cette vidéo au format MJPEG/AVI pour xjadeo ? (o/N) " convert_choice
case "$convert_choice" in
o|O|oui|Oui|y|Y|yes|Yes) CONVERT="yes" ;;
*) CONVERT="no" ;;
esac
}
# Conversion
convert_video() {
local output_file="${FILENAME}_xjadeo.avi"
info "Conversion en cours vers $output_file..."
# Qualité vidéo : on demande un paramètre
read -rp "Qualité vidéo MJPEG (q:v de 2 à 31, 2=meilleure) [2] : " qv
qv="${qv:-2}"
if ! [[ "$qv" =~ ^[0-9]+$ ]] || [ "$qv" -lt 2 ] || [ "$qv" -gt 31 ]; then
warn "Valeur invalide, utilisation de 2"
qv=2
fi
ffmpeg -i "$SOURCE_FILE" -c:v mjpeg -q:v "$qv" -c:a pcm_s16le -fflags +genpts "$output_file"
if [ $? -eq 0 ]; then
success "Conversion terminée : $output_file"
read -rp "Supprimer le fichier source ($SOURCE_FILE) ? (o/N) " del_choice
case "$del_choice" in
o|O|oui|Oui|y|Y|yes|Yes) rm -v "$SOURCE_FILE" ;;
*) info "Fichier source conservé." ;;
esac
else
error "La conversion a échoué."
fi
}
# Fonction principale
main() {
check_deps
get_url "$@"
get_filename
download_video
ask_conversion
if [ "$CONVERT" = "yes" ]; then
convert_video
else
info "Aucune conversion demandée. Le fichier source est $SOURCE_FILE"
fi
info "Terminé."
}
# Exécution
main "$@"