BASH Post Services

Viewing: 1747808379_ytgram.sh

Try wget https://bash.commongrounds.cc/uploads/1747808379_ytgram.sh from the console

Raw File Link

#!/bin/bash

# Check dependencies
command -v yt-dlp >/dev/null 2>&1 || { echo "yt-dlp is required but not installed. Aborting."; exit 1; }
command -v mpv >/dev/null 2>&1 || { echo "mpv is required but not installed. Aborting."; exit 1; }

echo "ytgram 1.0 Provided by Page Telegram Volunteer Services 2025."

# Main loop
while true; do
    # Get query if no previous results
    if [ -z "${videos+x}" ]; then
        if [ -z "$1" ]; then
            read -p "Enter search query: " query
        else
            query="$*"
            # Clear command-line args after first use
            set --
        fi

        # Search and store top 10 results
        echo "Searching YouTube for: $query"
        mapfile -t videos < <(yt-dlp "ytsearch10:$query" --print "%(title)s|||%(webpage_url)s" --no-warnings)

        if [ "${#videos[@]}" -eq 0 ]; then
            echo "No results found."
            continue
        fi
    fi

    # Display results
    echo
    echo "Top 10 Results:"
    for i in "${!videos[@]}"; do
        title=$(echo "${videos[$i]}" | cut -d '|' -f 1)
        echo "[$i] $title"
    done

    echo
    echo "Select an option:"
    echo " - Press 0–9 to stream at 360p"
    echo " - Press d0–d9 or D0–D9 to download and play"
    echo " - Press s or S for a new search"
    echo " - Press q or Q to quit"

    read -n 2 -p "> " input
    echo

    # Input parsing
    if [[ "$input" =~ ^[dD]([0-9])$ ]]; then
        download=true
        index="${BASH_REMATCH[1]}"
    elif [[ "$input" =~ ^[0-9]$ ]]; then
        download=false
        index="$input"
    elif [[ "$input" =~ ^[sS]$ ]]; then
        unset videos
        continue
    elif [[ "$input" =~ ^[qQ]$ ]]; then
        echo "Exiting."
        exit 0
    else
        echo "Invalid input."
        continue
    fi

    # Validate index
    if [ "$index" -ge "${#videos[@]}" ]; then
        echo "Invalid video selection."
        continue
    fi

    # Get URL
    video_entry="${videos[$index]}"
    url=$(echo "$video_entry" | awk -F '\\|\\|\\|' '{print $2}' | xargs)

    if [ -z "$url" ]; then
        echo "Could not extract video URL."
        continue
    fi

    # Format string for 360p (best 360p MP4 or fallback)
    format="bestvideo[height<=360][ext=mp4]+bestaudio[ext=m4a]/best[height<=360]"

    if [ "$download" = true ]; then
        echo "Downloading..."
        # Use a temporary filename to avoid issues with special characters
        temp_output="ytvideo_%(id)s.%(ext)s"
        # Download the video
        yt-dlp -f "$format" -o "$temp_output" --no-part "$url"
        
        # Check if download was successful
        if [ $? -ne 0 ]; then
            echo "Download failed."
            continue
        fi
        
        # Get the actual filename after download
        filename=$(yt-dlp -f "$format" -o "$temp_output" --get-filename "$url")
        
        if [ -f "$filename" ]; then
            # Download title and description to a text file
            echo "Downloading title and description..."
            title=$(yt-dlp --get-title "$url")
            description=$(yt-dlp --get-description "$url")
            text_filename="${filename%.*}.txt"
            {
                echo "Title: $title"
                echo
                echo "Description:"
                echo "$description"
            } > "$text_filename"
            
            if [ $? -eq 0 ]; then
                echo "Saved title and description to: $text_filename"
            else
                echo "Failed to save title and description."
            fi
            
            echo "Playing downloaded file: $filename"
            mpv "$filename"
        else
            echo "Downloaded file not found."
            continue
        fi
    else
        echo "Streaming from YouTube..."
        mpv --ytdl-format="$format" "$url"
    fi
done
BASH to Home