Skip to content

Instantly share code, notes, and snippets.

@DaWe35
Created July 31, 2024 14:22
Show Gist options
  • Save DaWe35/e96220ca6e8b86025506d3b27e29b556 to your computer and use it in GitHub Desktop.
Save DaWe35/e96220ca6e8b86025506d3b27e29b556 to your computer and use it in GitHub Desktop.
Play random songs from a folder with VLC
#!/bin/bash
# Directory containing your music files
# It will NOT search subfolders, you can change that by removing -maxdepth 1
MUSIC_DIR="/you/music/folder"
# Number of random songs to play
NUM_SONGS=100
# Check if VLC is installed
if ! command -v vlc &> /dev/null
then
echo "VLC is not installed. Please install VLC to use this script."
exit 1
fi
# Check if the directory exists
if [ ! -d "$MUSIC_DIR" ]; then
echo "Directory $MUSIC_DIR does not exist."
exit 1
fi
# Find all music files in the directory (excluding subdirectories) and select random ones
mapfile -t MUSIC_FILES < <(find "$MUSIC_DIR" -maxdepth 1 -type f \( -iname "*.mp3" -o -iname "*.wav" -o -iname "*.flac" -o -iname "*.ogg" \) | shuf -n $NUM_SONGS)
echo $MUSIC_FILES
# Check if enough files were found
if [ ${#MUSIC_FILES[@]} -lt $NUM_SONGS ]; then
echo "Not enough music files found in the directory."
exit 1
fi
# Play the random songs with VLC
vlc "${MUSIC_FILES[@]}" --play-and-exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment