fc2.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import threading
  2. from .common import FileDownloader
  3. from .external import FFmpegFD
  4. class FC2LiveFD(FileDownloader):
  5. """
  6. Downloads FC2 live without being stopped. <br>
  7. Note, this is not a part of public API, and will be removed without notice.
  8. DO NOT USE
  9. """
  10. def real_download(self, filename, info_dict):
  11. ws = info_dict['ws']
  12. heartbeat_lock = threading.Lock()
  13. heartbeat_state = [None, 1]
  14. def heartbeat():
  15. if heartbeat_state[1] < 0:
  16. return
  17. try:
  18. heartbeat_state[1] += 1
  19. ws.send('{"name":"heartbeat","arguments":{},"id":%d}' % heartbeat_state[1])
  20. except Exception:
  21. self.to_screen('[fc2:live] Heartbeat failed')
  22. with heartbeat_lock:
  23. heartbeat_state[0] = threading.Timer(30, heartbeat)
  24. heartbeat_state[0]._daemonic = True
  25. heartbeat_state[0].start()
  26. heartbeat()
  27. new_info_dict = info_dict.copy()
  28. new_info_dict.update({
  29. 'ws': None,
  30. 'protocol': 'live_ffmpeg',
  31. })
  32. try:
  33. return FFmpegFD(self.ydl, self.params or {}).download(filename, new_info_dict)
  34. finally:
  35. # stop heartbeating
  36. heartbeat_state[1] = -1