-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplay.py
executable file
·65 lines (53 loc) · 1.49 KB
/
multiplay.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# This is for playing multiple audio streams at the same time.
# Video is not presently implemented.
#
# See https://superuser.com/a/1325668 for more information.
import subprocess
import sys
def execute(file_args, mpv_args):
args = ['mpv']
args.extend(mpv_args)
args.extend(file_args)
ret = subprocess.call(args)
sys.exit(ret)
def single_play(mpv_args, file):
args = [file]
execute(args, mpv_args)
def double_play(mpv_args, file0, file1):
args = [
'--lavfi-complex=[aid1][aid2]amix[ao]',
file0,
f'--external-file={file1}',
]
execute(args, mpv_args)
def multi_play(mpv_args, files):
# build the lavfi-complex argument
lavfi = ['--lavfi-complex=']
for i in range(len(files)):
lavfi.append(f'[aid{i+1}]')
lavfi.append(f'amix=inputs={len(files)}[ao]')
args = [''.join(lavfi)]
del lavfi
# add the audio files
args.append(files[0])
for file in files[1:]:
args.append(f'--external-file={file}')
# run
execute(args, mpv_args)
if __name__ == '__main__':
mpv_args = []
files = []
# Gather arguments
for arg in sys.argv[1:]:
if arg.startswith('-'):
mpv_args.append(arg)
else:
files.append(arg)
# Use the correct mpv invocation
if len(files) == 1:
single_play(mpv_args, files[0])
elif len(files) == 2:
double_play(mpv_args, files[0], files[1])
else:
multi_play(mpv_args, files)