aboutsummaryrefslogtreecommitdiff
path: root/example-scripts/ffmpeg/record-all-audio-streams.py
blob: c30f73570c6359d196a49d19add4e31f65c7f0dd (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. # example call:
  43. # -------------
  44. # ffmpeg
  45. # -hide_banner
  46. # -y -nostdin
  47. # -i tcp://localhost:13000
  48. # -i tcp://localhost:13001
  49. # -i tcp://localhost:13002
  50. # -ac 2 -channel_layout stereo
  51. # -map 0:a -metadata:s:a:0 language=und
  52. # -map 1:a -metadata:s:a:1 language=und
  53. # -map 2:a -metadata:s:a:2 language=und
  54. # -c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
  55. # -flags +global_header -flags +ilme+ildct
  56. # -f mpegts
  57. # -vv
  58. cmd = """
  59. ffmpeg
  60. -hide_banner
  61. -y -nostdin
  62. {}
  63. -ac 2 -channel_layout stereo
  64. {}
  65. -c:a mp2 -b:a 192k -ac:a 2 -ar:a 48000
  66. -flags +global_header -flags +ilme+ildct
  67. -f mpegts
  68. {}
  69. """.format(' '.join(inputs), ' '.join(maps), output)
  70. log.info('running command:\n%s', cmd)
  71. args = shlex.split(cmd)
  72. p = subprocess.run(args)
  73. sys.exit(p.returncode)