From 264cf4f096b1d74665a823f1cf08086f07a9c190 Mon Sep 17 00:00:00 2001 From: beroth Date: Thu, 13 Mar 2025 17:12:20 -0600 Subject: [PATCH] downloader sh, a yt-dltp wrapper --- downloader.sh | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 downloader.sh diff --git a/downloader.sh b/downloader.sh new file mode 100644 index 0000000..54e5150 --- /dev/null +++ b/downloader.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +### +# downloader.sh +# Descarga video y/o auido usando yt-dlp +# uso: +# ./downloader.sh [-v] [-a] +# -v Descarga el video en video.mp4 +# -a Descarga el audio en audio.mp3 +### + +install_dependencies() { + if command -v apt &> /dev/null; then + sudo apt update + sudo apt install -y ffmpeg python3-pip + sudo pip3 install yt-dlp + elif command -v pacman &> /dev/null; then + sudo pacman -Syu --noconfirm ffmpeg yt-dlp + elif command -v dnf &> /dev/null; then + sudo dnf install -y ffmpeg yt-dlp + else + echo "Distro no soportada. Instala ffmpeg y yt-dlp manualmente en tu s.o." + exit 1 + fi +} + +# yt-dlp & ffmpeg son necesarios +if ! command -v yt-dlp &> /dev/null || ! command -v ffmpeg &> /dev/null; then + echo "No se encontraron yt-dlp y/o ffmpeg instaladas. Instalando..." + install_dependencies +fi + +# si no hay url te dice como se usa +if [ -z "$1" ]; then + echo "Uso: $0 [-v] [-a]" + echo " -v: Descargar video" + echo " -a: Descargar audio" + exit 1 +fi + +URL=$1 +DOWNLOAD_VIDEO=false +DOWNLOAD_AUDIO=false + +# Parse arguments +shift +while [[ $# -gt 0 ]]; do + case $1 in + -v) DOWNLOAD_VIDEO=true ;; + -a) DOWNLOAD_AUDIO=true ;; + *) echo "Esta opcion no es soportada por el script! : $1"; exit 1 ;; + esac + shift +done + +# Validate options +if ! $DOWNLOAD_VIDEO && ! $DOWNLOAD_AUDIO; then + echo "Especifica almenos una opcion: -v (video) o -a (audio)" + exit 1 +fi + +# Temporary directory for intermediate files +TEMP_DIR=$(mktemp -d) + +# descargar video +if $DOWNLOAD_VIDEO; then + echo "Descargando video..." + yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" -o "$TEMP_DIR/video.%(ext)s" "$URL" + VIDEO_FILE=$(find "$TEMP_DIR" -name "video.*") + if [ -f "$VIDEO_FILE" ]; then + mv "$VIDEO_FILE" "./$(basename "$VIDEO_FILE")" + echo "El video se encuentra en ./$(basename "$VIDEO_FILE")" + else + echo "Ocurrio un error al descargar el video" + fi +fi + +# descargar audio +if $DOWNLOAD_AUDIO; then + echo "Descargando audio..." + yt-dlp -f "bestaudio[ext=m4a]/bestaudio" -o "$TEMP_DIR/audio.%(ext)s" --extract-audio --audio-format mp3 "$URL" + AUDIO_FILE=$(find "$TEMP_DIR" -name "audio.*") + if [ -f "$AUDIO_FILE" ]; then + mv "$AUDIO_FILE" "./$(basename "$AUDIO_FILE")" + echo "El archivo se encuentra en ./$(basename "$AUDIO_FILE")" + else + echo "Ocurrio un error al descargar el audio" + fi +fi + +# Limpiar el kgadero +rm -rf "$TEMP_DIR"