summaryrefslogtreecommitdiff
path: root/bin/shuffle
blob: d67696393b6cf7bd6fe7a9dceba7ddd5e8dd3c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/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