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 14, 2026 at 12:48 pm
#8477
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
