Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created December 4, 2025 14:03
Show Gist options
  • Select an option

  • Save skorotkiewicz/fcc79309bcf8afb43498397905583653 to your computer and use it in GitHub Desktop.

Select an option

Save skorotkiewicz/fcc79309bcf8afb43498397905583653 to your computer and use it in GitHub Desktop.
Play random movies by year with mpv
#!/bin/bash
## Play one random movie from before 2008
#./play_random_movie_by_year.sh 2008
## Play ALL movies from before 2008 in random order
#./play_random_movie_by_year.sh 2008 --playlist=true
# Default configuration
MOVIE_DIR="HD"
MAX_YEAR=""
PLAYLIST_MODE=false
# Parse arguments
for arg in "$@"; do
case $arg in
--playlist=true)
PLAYLIST_MODE=true
shift
;;
--playlist=false)
PLAYLIST_MODE=false
shift
;;
*)
if [[ "$arg" =~ ^[0-9]{4}$ ]]; then
MAX_YEAR="$arg"
fi
;;
esac
done
# Check if year was provided
if [ -z "$MAX_YEAR" ]; then
echo "Usage: $0 <year> [--playlist=true|false]"
echo "Example: $0 2008 --playlist=true"
exit 1
fi
# Check if directory exists
if [ ! -d "$MOVIE_DIR" ]; then
echo "Error: Directory '$MOVIE_DIR' not found!"
exit 1
fi
# Find all movies and filter by year
movies=()
while IFS= read -r -d '' file; do
# Extract year from filename using regex
if [[ "$file" =~ \(([0-9]{4})\) ]]; then
year="${BASH_REMATCH[1]}"
if [ "$year" -lt "$MAX_YEAR" ]; then
movies+=("$file")
fi
fi
done < <(find "$MOVIE_DIR" -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" -o -name "*.webm" \) -print0)
# Check if any movies were found
if [ ${#movies[@]} -eq 0 ]; then
echo "No movies found below year $MAX_YEAR in '$MOVIE_DIR'"
exit 1
fi
echo "Found ${#movies[@]} movies below year $MAX_YEAR"
echo ""
if [ "$PLAYLIST_MODE" = true ]; then
# Shuffle movies array for random playlist
shuffled=()
indices=($(seq 0 $((${#movies[@]} - 1))))
# Fisher-Yates shuffle
for ((i=${#indices[@]}-1; i>0; i--)); do
j=$((RANDOM % (i+1)))
temp=${indices[i]}
indices[i]=${indices[j]}
indices[j]=$temp
done
for idx in "${indices[@]}"; do
shuffled+=("${movies[$idx]}")
done
echo "=== RANDOM PLAYLIST MODE ==="
echo "Playing all ${#shuffled[@]} movies in random order"
echo ""
# Display playlist
for i in "${!shuffled[@]}"; do
echo "$((i+1)). $(basename "${shuffled[$i]}")"
done
echo ""
# Play shuffled playlist with mpv
mpv --playlist-start=0 "${shuffled[@]}"
else
# Single random movie mode
random_movie="${movies[$RANDOM % ${#movies[@]}]}"
movie_name=$(basename "$random_movie")
echo "Playing: $movie_name"
echo ""
mpv "$random_movie"
fi
@skorotkiewicz
Copy link
Author

skorotkiewicz commented Dec 4, 2025

Script plays movies below specific year.

Command-line year argument - specify the year directly when running the script.

  1. Playlist mode - when enabled, it:
  • Shuffles all matching movies using Fisher-Yates algorithm
  • Displays the full playlist order
  • Plays all movies back-to-back with mpv
  1. Single movie mode (default) - plays just one random movie like before

  2. The playlist mode will show you the order before starting, and mpv will automatically play through all the movies one after another. You can still skip between movies using mpv's playlist controls (< and > keys).

Script written 100% per Claude. Thanks Claude <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment