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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#!/usr/bin/env python3
import socket
import sys
import json
import shlex
import subprocess
import logging
from configparser import SafeConfigParser
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger('record-all-audio-streams')
host = 'localhost'
port = 9999
log.info('Connecting to %s:%u', host, port)
conn = socket.create_connection((host, port))
fd = conn.makefile('rw')
log.info('Fetching Config from Server')
fd.write("get_config\n")
fd.flush()
for line in fd:
if line.startswith('server_config'):
[cmd, arg] = line.split(' ', 1)
server_config_json = arg
log.info('Received Config from Server')
break
log.info('Parsing Server-Config')
server_config = json.loads(server_config_json)
def getlist(self, section, option):
return [x.strip() for x in self.get(section, option).split(',')]
SafeConfigParser.getlist = getlist
config = SafeConfigParser()
config.read_dict(server_config)
sources = config.getlist('mix', 'sources')
inputs = []
maps = []
for idx, source in enumerate(sources):
inputs.append('-i tcp://localhost:{:d}'.format(13000 + idx))
maps.append('-map {0:d}:a -metadata:s:a:{0:d} language=und'.format(idx))
try:
output = sys.argv[1]
except IndexError:
output = 'output.ts'
# example call:
# -------------
# ffmpeg
# -hide_banner
# -y -nostdin
# -i tcp://localhost:13000
# -i tcp://localhost:13001
# -i tcp://localhost:13002
# -ac 2 -channel_layout stereo
# -map 0:a -metadata:s:a:0 language=und
# -map 1:a -metadata:s:a:1 language=und
# -map 2:a -metadata:s:a:2 language=und
# -c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
# -flags +global_header -flags +ilme+ildct
# -f mpegts
# -vv
cmd = """
ffmpeg
-hide_banner
-y -nostdin
{}
-ac 2 -channel_layout stereo
{}
-c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
-flags +global_header -flags +ilme+ildct
-f mpegts
{}
""".format(' '.join(inputs), ' '.join(maps), output)
log.info('running command:\n%s', cmd)
args = shlex.split(cmd)
p = subprocess.run(args)
sys.exit(p.returncode)
|