summaryrefslogtreecommitdiff
path: root/bin/shuffle
blob: a8bfdc47a09919f7ae076b3e41eebaa303a9e232 (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
#!/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 <path>, or the current directory if <path> is not passed')
parser.add_argument('-m',
                    dest='mpv_opts',
                    default=['--no-video'],
                    action='append',
                    help='pass <opts> 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