#!/usr/bin/env python3 import argparse import random import os import subprocess 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('-m', dest='mpv_opts', default=['--no-video'], action='append', help='pass to mpv') args = parser.parse_args() prev = [] 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] print('Playing', file) try: subprocess.run(['mpv', *args.mpv_opts, file]) except KeyboardInterrupt: break