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 1 post (of 1 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
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
TAKs Shack