#!/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