I made a bash script to play random music from Music directory

#!/bin/bash

shopt -s nullglob;

# defining rand function which produces unbiased random number
rand() { local max=$((32768 / $1 * $1)) r; while (( (r=$RANDOM) >= max )); do :; done; printf '%s\n' "$(( r % $1 ))"; }

pkill mpv; # killing already playing song

songs=(~/Music/*)
len=${#songs[@]}
random_index="$(rand len)"

if [[ $len == 0 ]]; then
	echo "No songs to play :("
	exit
fi
mpv --no-video "${songs[random_index]}" & disown

=> Posted in: s/bash | ๐Ÿš€ random2934

2024-09-23 ยท 4 months ago

4 Comments โ†“

=> ๐Ÿ‘บ daruma ยท Sep 23 at 20:07:

Nice! you can also use the -shuffle in MPV but you need a playlist.

=> ๐Ÿš€ random2934 [OP] ยท Sep 24 at 05:03:

I thought about just having mpv -shuffle ~/Music however I couldn't key bind this command since I want Ctrl+Shift+M to play a random song.

=> ๐Ÿ‘บ daruma ยท Sep 24 at 05:35:

right but you could do a one liner script with that move -shuffle and chmod +x it to bind it to your ctrl-shift-m too

=> ๐Ÿš€ random2934 [OP] ยท Sep 24 at 05:39:

yeah, that would be much simpler :D

Proxy Information
Original URL
gemini://bbs.geminispace.org/s/bash/19863
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
77.171027 milliseconds
Gemini-to-HTML Time
0.752047 milliseconds

This content has been proxied by September (3851b).