summaryrefslogtreecommitdiff
path: root/bin/shuffle
blob: d8dd878b420a43737447aee855e347bf6c86d959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh -eu

prev2=-1
prev1=$prev2
index=$prev1

path="."
mpv_opts="--no-video"

while getopts hm:p: opt 2>/dev/null; do
	case "$opt" in
	m)
		mpv_opts="$mpv_opts $OPTARG"
		;;
	p)
		path="$OPTARG"
		;;
	h)
		echo "\
Usage:	$0 [-h] [-m <opts>] [-p <path>]

Play audio files in a directory in (mostly) random order.

Options:
	-m <opts>	Pass <opts> as extra options to mpv
	-p <path>	Play files in <path> instead of the current directory
	-h		Print this help text
" >&2
		exit 0
		;;
	?)
		echo "Usage: $0 [-h] [-m <opts>] [-p <path>]" >&2
		exit 1
		;;
	esac
done

while true; do
	files=$(find "$path" -type f)
	n=$(echo "$files" | wc -l)

	while [ $index -eq $prev2 ] || [ $index -eq $prev1 ]; do
		index=$((RANDOM % n))
	done
	prev2=$prev1
	prev1=$index
	echo "$files" | (
		i=0
		while read -r file; do
			if [ $i -eq $index ]; then
				echo "Playing $file"
				mpv $mpv_opts "$file" || true
				break
			fi
			i=$((i + 1))
		done
	)
done