#!/bin/bash

# yt-dlp --extract-audio --audio-format mp3 LINK_HERE
#!/bin/bash                                                                                                                                                
set -euo pipefail                                                                                                                                          
echo "yt-dlp Batch Downloader"                                                                                                                             
                                                                                                                                                           
# 1. Select download type                                                                                                                                  
DL_MODE=""                                                                                                                                                 
while true; do                                                                                                                                             
    read -rp "Download (a)udio or (v)ideo? (a/v): " choice                                                                                                 
    choice=${choice,,} # Convert to lowercase                                                                                                              
    case "$choice" in                                                                                                                                      
        a) DL_MODE="--extract-audio --audio-format mp3"; echo "Audio selected."; 
	break ;;
        v)
            VIDEO_FORMAT=""
            while true; do
                read -rp "Video quality: (b)est or (7)20p max? (b/7): " vchoice
                vchoice=${vchoice,,}
                case "$vchoice" in
                    b|"")
                        VIDEO_FORMAT="best"
                        echo "Best quality selected."
                        break ;;
                    7)
                        VIDEO_FORMAT="bestvideo[height<=720]+bestaudio/best[height<=720]"
                        echo "720p max selected."
                        break ;;
                    *) echo "Invalid. Try 'b' or '7'." ;;
                esac
            done
            DL_MODE="-f $VIDEO_FORMAT"
            echo "Video selected."
            break ;;
    esac                                                                  
done                                                                      
                                                                              
# 2. Collect links                                                        
echo "Enter links (one per line, press Enter on empty line to finish):"   
links=()                                                                  
while read -rp "Link $(( ${#links[@]} + 1 )): " link; do                  
    [[ -z "$link" ]] && break # If empty line, stop                       
    links+=("$link")                                                      
done                                                                      
                                                                            
# 3. Process links                                                        
[[ ${#links[@]} -eq 0 ]] && { echo "No links entered. Exiting."; exit 0; }
                                                                            
echo "Starting ${#links[@]} download(s)..."                               
for link in "${links[@]}"; do
    echo "--> Processing: $link"
    if [[ "$DL_MODE" == "--extract-audio --audio-format mp3" ]]; then
        yt-dlp --extract-audio --audio-format mp3 "$link"
    else
        FORMAT_SPEC="${DL_MODE#-f }"
        yt-dlp -f "$FORMAT_SPEC" "$link"
    fi
done

echo "All downloads completed."
