What makes us different from other similar websites? › Forums › Tech › Play Random Video in Folder [Shebang Script] › Reply To: Play Random Video in Folder [Shebang Script]
May 25, 2026 at 5:30 pm
#8486
Moderator
Random play videos was fixed. It would sometimes not work.
#!/bin/bash
# Configuration
PLAYER="mpv"
shopt -s nullglob
# Check for dependencies
check_dependencies() {
if ! command -v "$PLAYER" &> /dev/null; then
echo -e "\e[31m[!] $PLAYER is not installed.\e[0m"
echo "Which Linux distribution are you using?"
echo "1) Debian/Ubuntu/Mint"
echo "2) Arch/Manjaro"
echo "3) Fedora/RHEL/CentOS"
read -p "Select (1-3): " dist_choice
case $dist_choice in
1) sudo apt update && sudo apt install -y mpv ;;
2) sudo pacman -Sy --noconfirm mpv ;;
3) sudo dnf install -y mpv ;;
*) echo "Unsupported distribution. Please install $PLAYER manually."; exit 1 ;;
esac
fi
}
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 -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() {
check_dependencies
echo -e "\e[1mEnter folder path:\e[0m"
read -r -e VAULT_DIR
cd "$VAULT_DIR" || return
while true; do
# Modified to include all common video formats
vids=(*.mp4 *.mkv *.avi *.mov *.webm)
if [ ${#vids[@]} -eq 0 ]; then
echo "No video files found!"
break
fi
random_index=$((RANDOM % ${#vids[@]}))
random_vid="${vids[$random_index]}"
echo -e "\e[35mPlaying Random Choice:\e[0m $random_vid"
# Verify file integrity before playing
if ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$random_vid" > /dev/null 2>&1; then
"$PLAYER" "$random_vid"
echo -e "\n\e[1mFinished.\e[0m"
echo "1) Keep version"
echo "2) Delete version"
read -p "Choose (1/2): " choice
[[ "$choice" == "2" ]] && rm "$random_vid" && echo "Deleted."
else
echo -e "\e[31m[ERROR]\e[0m File corrupted. Deleting..."
rm "$random_vid"
fi
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
