aboutsummaryrefslogtreecommitdiff
path: root/example-scripts/ffmpeg/record-all-audio-streams.py
blob: f5bff8f744eb01cea255cee1b8262c23e1c9057b (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. words = line.split(' ')
  22. args = words[1:]
  23. server_config_json = " ".join(args)
  24. log.info('Received Config from Server')
  25. break
  26. log.info('Parsing Server-Config')
  27. server_config = json.loads(server_config_json)
  28. def getlist(self, section, option):
  29. return [x.strip() for x in self.get(section, option).split(',')]
  30. SafeConfigParser.getlist = getlist
  31. config = SafeConfigParser()
  32. config.read_dict(server_config)
  33. sources = config.getlist('mix', 'sources')
  34. inputs = []
  35. maps = []
  36. for idx, source in enumerate(sources):
  37. inputs.append('-i tcp://localhost:%u' % (13000+idx))
  38. maps.append('-map %u:a -metadata:s:a:%u language=und' % (idx, idx))
  39. try:
  40. output = sys.argv[1]
  41. except:
  42. output = 'output.ts'
  43. cmd = """
  44. ffmpeg \
  45. -hide_banner
  46. -y -nostdin
  47. %s
  48. -ac 2 -channel_layout stereo
  49. %s
  50. -c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
  51. -flags +global_header -flags +ilme+ildct
  52. -f mpegts
  53. %s
  54. """ % (' '.join(inputs), ' '.join(maps), output)
  55. log.info('running command:\n%s', cmd)
  56. args = shlex.split(cmd)
  57. p = subprocess.run(args)
  58. sys.exit(p.returncode)