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