loader image

Play Random Video in Folder [Shebang Script]

What makes us different from other similar websites? Forums Tech Play Random Video in Folder [Shebang Script]

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8090
    thumbtak
    Moderator

    This script, when placed in a folder, will allow you to play a random video in that folder, with smplayer. After the video plays, it will ask if you want to delete the video, and if you want to play another.

    #!/bin/sh
    
    # This script plays a random video from the current directory.
    # After playback, it asks if you want to delete the video.
    # Finally, it asks if you want to play another video.
    
    # --- Main Script Logic ---
    clear_screen() {
    # Clear the terminal screen
    printf '\033[H\033[2J'
    }
    
    ask_yes_no() {
    # Function to ask a yes/no question and validate input
    local prompt="$1"
    local response
    while true; do
    printf "%s (y/n): " "$prompt"
    read -r response
    case "$response" in
    [yY]|[yY][eE][sS]) return 0 ;; # Yes
    [nN]|[nN][oO]) return 1 ;; # No
    *) printf "Invalid input. Please enter 'y' or 'n'.\n" ;;
    esac
    done
    }
    
    play_random_video() {
    # Find all video files in the current directory (and subdirectories)
    printf "Searching for videos in the current directory (and subdirectories)...\n"
    
    # CORRECTED LOGIC:
    # Directly pipe find's null-separated output to shuf -z,
    # which reads null-separated input and picks one.
    SELECTED_VIDEO=$(find . -type f -iname "*.mp4" -o -iname "*.mkv" -print0 | shuf -z -n 1)
    
    # Check if a video was found/selected (SELECTED_VIDEO will be empty if not)
    if [ -z "$SELECTED_VIDEO" ]; then
    printf "No video files found in the current directory (or subdirectories) with the specified extensions.\n"
    printf "Supported extensions: .mp4, .mkv\n" # Updated message
    return 1 # Indicate failure to find videos
    fi
    
    printf "\n--- Playing Video ---\n"
    printf "Now playing in SMPlayer: %s\n" "$SELECTED_VIDEO"
    
    # Play the video using smplayer
    smplayer "$SELECTED_VIDEO"
    
    # Check smplayer's exit status
    if [ $? -ne 0 ]; then
    printf "\nWarning: smplayer exited with an error. The video might not have played correctly.\n"
    fi
    
    printf "\n--- Playback Finished ---\n"
    
    # Ask to delete the video
    if ask_yes_no "Do you want to delete '%s'?" "$SELECTED_VIDEO"; then
    printf "Deleting '%s'...\n" "$SELECTED_VIDEO"
    rm -i "$SELECTED_VIDEO" # -i for interactive deletion (prompts before delete)
    if [ $? -eq 0 ]; then
    printf "'%s' deleted successfully.\n" "$SELECTED_VIDEO"
    else
    printf "Failed to delete '%s'.\n" "$SELECTED_VIDEO"
    fi
    else
    printf "'%s' was not deleted.\n" "$SELECTED_VIDEO"
    fi
    return 0 # Indicate success
    }
    
    # --- Script Execution Loop ---
    clear_screen
    
    # ASCII Art Functions
    # Function to print the main ASCII art banner for the script.
    print_banner() {
    echo "+---------------------------------+"
    echo "|===========TAKS SHACK============|"
    echo "|======https://taksshack.com======|"
    echo "+---------------------------------+"
    
    }
    
    printf "Welcome to the Random Video Player!\n"
    printf "This script will play videos using 'smplayer'.\n"
    printf "Make sure 'smplayer' is installed on your system.\n\n"
    
    while true; do
    play_random_video
    # If play_random_video returns 1 (no videos found), break the loop
    [ $? -ne 0 ] && break
    
    printf "\n" # Add a newline for readability
    if ! ask_yes_no "Do you want to play another random video?"; then
    printf "Exiting. Goodbye!\n"
    break
    fi
    clear_screen
    done
    #8477
    thumbtak
    Moderator

    This script has been updated to also have an option to compress files into the best compression for 720p, shutdown (if you want), and convert all it can within a certain time.

    #!/bin/bash
    
    # Configuration
    PLAYER="smplayer"
    shopt -s nullglob
    
    draw_header() {
    clear
    echo -e "\e[36m"
    cat << 'EOF'
    ░████████╗░█████╗░██╗░░██╗░██████╗░██████╗██╗░░██╗░█████╗░░██████╗██╗░░██╗
    ░╚══██╔══╝██╔══██╗██║░██╔╝██╔════╝██╔════╝██║░░██║██╔══██╗██╔════╝██║░██╔╝
    ░░░░██║░░░███████║█████═╝░╚█████╗░╚█████╗░███████║███████║██║░░░░░█████═╝░
    ░░░░██║░░░██╔══██║██╔═██╗░░╚═══██╗░╚═══██╗██╔══██║██╔══██║██║░░░░░██╔═██╗░
    ░░░░██║░░░██║░░██║██║░╚██╗██████╔╝██████╔╝██║░░██║██║░░██║╚██████╗██║░╚██╗
    ░░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝░╚═════╝╚═╝░░╚═╝
    ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░.com░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    EOF
    echo -e "\e[33m"
    echo "========================================================"
    echo " Created by Thumbtak "
    echo " 720p AUTO-SHUTDOWN TRANSCODER (WITH PROGRESS)"
    echo " Random Play videos "
    echo "========================================================"
    echo -e "\e[0m"
    }
    
    compress_mode() {
    echo -e "\e[1mEnter folder path:\e[0m"
    read -r -e TARGET_DIR
    cd "$TARGET_DIR" || return
    
    echo -e "\e[1mHow many MINUTES should this session last?\e[0m"
    read -r run_minutes
    
    echo -e "\e[1mShutdown computer when finished? (y/n):\e[0m"
    read -r shutdown_choice
    
    start_time=$(date +%s)
    end_time=$((start_time + (run_minutes * 60)))
    
    for file in *; do
    current_time=$(date +%s)
    
    if [ "$current_time" -ge "$end_time" ]; then
    echo -e "\n\e[33m[TIMER EXPIRED]\e[0m Finishing current task..."
    break
    fi
    
    if [[ -f "$file" && "$file" != *_720p_hevc.mp4 ]]; then
    output="${file%.*}_720p_hevc.mp4"
    
    echo -e "\n\e[34m[PROCESSING]\e[0m $file"
    rem=$(( (end_time - current_time) / 60 ))
    [[ $rem -lt 0 ]] && rem=0
    echo -e "\e[2mSession time remaining: $rem minutes.\e[0m"
    
    # FFMPEG with progress bar enabled via '-stats'
    # Quality: CRF 20 | Speed: Medium | Resolution: 720p
    ffmpeg -i "$file" -vf "scale=-2:720" -c:v libx265 -crf 20 -preset medium \
    -c:a libmp3lame -b:a 128k -stats -hide_banner "$output" && rm "$file"
    fi
    done
    
    echo -e "\n\e[32m[DONE]\e[0m Session complete."
    
    if [[ "$shutdown_choice" == "y" || "$shutdown_choice" == "Y" ]]; then
    echo "Shutting down in 60 seconds... (Ctrl+C to cancel)"
    sleep 60
    sudo shutdown -h now
    fi
    }
    
    play_mode() {
    echo -e "\e[1mEnter folder path:\e[0m"
    read -r -e VAULT_DIR
    cd "$VAULT_DIR" || return
    
    while true; do
    mapfile -t vids < <(ls *_720p_hevc.mp4 2>/dev/null)
    
    if [ ${#vids[@]} -eq 0 ]; then
    echo "No processed videos found!"
    break
    fi
    
    random_vid="${vids[$RANDOM % ${#vids[@]}]}"
    echo -e "\e[35mPlaying Random Choice:\e[0m $random_vid"
    $PLAYER "$random_vid" > /dev/null 2>&1
    
    echo -e "\n\e[1mFinished.\e[0m"
    echo "1) Keep compressed version"
    echo "2) Delete version"
    read -p "Choose (1/2): " choice
    
    [[ "$choice" == "2" ]] && rm "$random_vid" && echo "Deleted."
    
    read -p "Watch another? (y/n): " another
    [[ "$another" != "y" ]] && break
    draw_header
    done
    }
    
    # Main Menu
    draw_header
    echo "1) Start Timed Compression Session (w/ Shutdown)"
    echo "2) Random Play & Cleanup"
    read -p "Select: " main_choice
    
    case $main_choice in
    1) compress_mode ;;
    2) play_mode ;;
    *) exit ;;
    esac
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
TAKs Shack