From dc26126e47d4c2a9cd367a3998b819280ba26794 Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Sun, 4 Feb 2024 11:13:20 -0500 Subject: bin/shuffle: shellify --- bin/shuffle | 99 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) (limited to 'bin/shuffle') 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 , or the current directory if 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 <&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 -- cgit v1.2.3