summaryrefslogtreecommitdiff
path: root/example-scripts/ffmpeg/record-all-audio-streams.py
blob: 29cfd6b21517d6a6b3b2ae79f8a48ea24147fcce (plain)
  1. #!/usr/bin/env python3
  2. import socket
  3. import sys
  4. import json
  5. import shlex
  6. import subprocess
  7. import logging
  8. from configparser import SafeConfigParser
  9. logging.basicConfig(level=logging.DEBUG)
  10. log = logging.getLogger('record-all-audio-streams')
  11. host = 'localhost'
  12. port = 9999
  13. log.info('Connecting to %s:%u', host, port)
  14. conn = socket.create_connection((host, port))
  15. fd = conn.makefile('rw')
  16. log.info('Fetching Config from Server')
  17. fd.write("get_config\n")
  18. fd.flush()
  19. for line in fd:
  20. if line.startswith('server_config'):
  21. [cmd, arg] = line.split(' ', 1)
  22. server_config_json = arg
  23. log.info('Received Config from Server')
  24. break
  25. log.info('Parsing Server-Config')
  26. server_config = json.loads(server_config_json)
  27. def getlist(self, section, option):
  28. return [x.strip() for x in self.get(section, option).split(',')]
  29. SafeConfigParser.getlist = getlist
  30. config = SafeConfigParser()
  31. config.read_dict(server_config)
  32. sources = config.getlist('mix', 'sources')
  33. inputs = []
  34. maps = []
  35. for idx, source in enumerate(sources):
  36. inputs.append('-i tcp://localhost:{:d}'.format(13000 + idx))
  37. maps.append('-map {0:d}:a -metadata:s:a:{0:d} language=und'.format(idx))
  38. try:
  39. output = sys.argv[1]
  40. except:
  41. output = 'output.ts'
  42. cmd = """
  43. ffmpeg \
  44. -hide_banner
  45. -y -nostdin
  46. {}
  47. -ac 2 -channel_layout stereo
  48. {}
  49. -c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
  50. -flags +global_header -flags +ilme+ildct
  51. -f mpegts
  52. {}
  53. """.format(' '.join(inputs), ' '.join(maps), output)
  54. log.info('running command:\n%s', cmd)
  55. args = shlex.split(cmd)
  56. p = subprocess.run(args)
  57. sys.exit(p.returncode)