summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/shuffle90
1 files changed, 37 insertions, 53 deletions
diff --git a/bin/shuffle b/bin/shuffle
index d8dd878..a8bfdc4 100755
--- a/bin/shuffle
+++ b/bin/shuffle
@@ -1,58 +1,42 @@
-#!/bin/sh -eu
+#!/usr/bin/env python3
-prev2=-1
-prev1=$prev2
-index=$prev1
+import argparse
+import random
+import os
+import subprocess
-path="."
-mpv_opts="--no-video"
+parser = argparse.ArgumentParser(
+ prog='shuffle',
+ description='play audio files in a directory in random order')
+parser.add_argument(
+ 'path',
+ default='.',
+ nargs='?',
+ help=
+ 'play files in <path>, or the current directory if <path> is not passed')
+parser.add_argument('-m',
+ dest='mpv_opts',
+ default=['--no-video'],
+ action='append',
+ help='pass <opts> to mpv')
+args = parser.parse_args()
-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>]
+prev = []
-Play audio files in a directory in (mostly) random order.
+while True:
+ files = [
+ os.path.join(root, file) for root, _, files in os.walk(args.path)
+ for file in files
+ ]
+ file = random.choice(files)
+ while file in prev:
+ file = random.choice(files)
+ prev.append(file)
+ if len(prev) > 2:
+ del prev[:len(prev) - 3]
-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
+ print('Playing', file)
+ try:
+ subprocess.run(['mpv', *args.mpv_opts, file])
+ except KeyboardInterrupt:
+ break