diff options
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/shuffle | 99 |
1 files changed, 54 insertions, 45 deletions
diff --git a/bin/shuffle b/bin/shuffle index d676963..67c4de9 100755 --- a/bin/shuffle +++ b/bin/shuffle @@ -1,45 +1,54 @@ -#!/usr/bin/env python3 - -import argparse -import random -import os -import subprocess - -def main(): - 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('--volume', - default=100.0, - type=float, - help='volume for mpv (should be in 0-100 range)') - args = parser.parse_args() - - # Keep track of the last few files we played and avoid playing them again - prev = [] - - while True: - files = [ - os.path.join(root, file) for root, _, files in os.walk(args.path) - for file in files - ] - for f in prev: - files.remove(f) - file = random.choice(files) - prev.append(file) - del prev[:len(prev) - 2] - - print('Playing', file) - subprocess.run(['mpv', f'--volume={args.volume}', file]) - -if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - pass +#!/bin/sh + +printhelp() { + cat <<EOF +Usage: ${0##*/} [-v VOL] [DIR] + +Play audio files in DIR (or .) in random order + + -h Print this help + -v VOL Play with volume VOL (default 100) +EOF + exit 0 +} + +printusage() { + printf 'Usage: %s [-v volume] [path]\n' "${0##*/}" + exit 1 +} + +volume=100 +while getopts hv: opt; do + case "$opt" in + h) printhelp ;; + v) + if printf %d "$OPTARG" 2>&1 >/dev/null; then + volume="$OPTARG" + else + printf "%s: invalid number '%s'\n" "${0##*/}" "$OPTARG" + exit 1 + fi + ;; + ?) printusage ;; + esac +done + +shift $((OPTIND-1)) + +if [ $# -gt 1 ]; then + printusage +elif [ $# -eq 1 ]; then + dir="$1" +else + dir="$(. "${XDG_CONFIG_HOME:-"$HOME"/.config}/user-dirs.dirs" && printf '%s' "$XDG_MUSIC_DIR")" +fi + +prev1= +prev2= +while true; do + file="$(find -L "$dir" -type f \( -name '*.m4a' -o -name '*.opus' \) ! -path "$prev1" ! -path "$prev2" -exec shuf -n1 -e '{}' +)" + prev1="$prev2" + prev2="$file" + printf 'Playing %s\n' "${file#$dir/}" + mpv --volume="$volume" "$file" +done |
