zmqshell.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. import argparse
  3. import cmd
  4. import logging
  5. import sys
  6. import zmq
  7. HELP = '''
  8. Provide a shell used to send interactive commands to a zmq filter.
  9. The command assumes there is a running zmq or azmq filter acting as a
  10. ZMQ server.
  11. You can send a command to it, follwing the syntax:
  12. TARGET COMMAND [COMMAND_ARGS]
  13. * TARGET is the target filter identifier to send the command to
  14. * COMMAND is the name of the command sent to the filter
  15. * COMMAND_ARGS is the optional specification of command arguments
  16. See the zmq/azmq filters documentation for more details, and the
  17. zeromq documentation at:
  18. https://zeromq.org/
  19. '''
  20. logging.basicConfig(format='zmqshell|%(levelname)s> %(message)s', level=logging.INFO)
  21. log = logging.getLogger()
  22. class LavfiCmd(cmd.Cmd):
  23. prompt = 'lavfi> '
  24. def __init__(self, bind_address):
  25. context = zmq.Context()
  26. self.requester = context.socket(zmq.REQ)
  27. self.requester.connect(bind_address)
  28. cmd.Cmd.__init__(self)
  29. def onecmd(self, cmd):
  30. if cmd == 'EOF':
  31. sys.exit(0)
  32. log.info(f"Sending command: {cmd}")
  33. self.requester.send_string(cmd)
  34. response = self.requester.recv_string()
  35. log.info(f"Received response: {response}")
  36. class Formatter(
  37. argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter
  38. ):
  39. pass
  40. def main():
  41. parser = argparse.ArgumentParser(description=HELP, formatter_class=Formatter)
  42. parser.add_argument('--bind-address', '-b', default='tcp://localhost:5555', help='specify bind address used to communicate with ZMQ')
  43. args = parser.parse_args()
  44. try:
  45. LavfiCmd(args.bind_address).cmdloop('FFmpeg libavfilter interactive shell')
  46. except KeyboardInterrupt:
  47. pass
  48. if __name__ == '__main__':
  49. main()