redge.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import functools
  2. from .common import InfoExtractor
  3. from ..networking import HEADRequest
  4. from ..utils import (
  5. float_or_none,
  6. int_or_none,
  7. join_nonempty,
  8. parse_qs,
  9. update_url_query,
  10. )
  11. from ..utils.traversal import traverse_obj
  12. class RedCDNLivxIE(InfoExtractor):
  13. _VALID_URL = r'https?://[^.]+\.(?:dcs\.redcdn|atmcdn)\.pl/(?:live(?:dash|hls|ss)|nvr)/o2/(?P<tenant>[^/?#]+)/(?P<id>[^?#]+)\.livx'
  14. IE_NAME = 'redcdnlivx'
  15. _TESTS = [{
  16. 'url': 'https://r.dcs.redcdn.pl/livedash/o2/senat/ENC02/channel.livx?indexMode=true&startTime=638272860000&stopTime=638292544000',
  17. 'info_dict': {
  18. 'id': 'ENC02-638272860000-638292544000',
  19. 'ext': 'mp4',
  20. 'title': 'ENC02',
  21. 'duration': 19683.982,
  22. 'live_status': 'was_live',
  23. },
  24. }, {
  25. 'url': 'https://r.dcs.redcdn.pl/livedash/o2/sejm/ENC18/live.livx?indexMode=true&startTime=722333096000&stopTime=722335562000',
  26. 'info_dict': {
  27. 'id': 'ENC18-722333096000-722335562000',
  28. 'ext': 'mp4',
  29. 'title': 'ENC18',
  30. 'duration': 2463.995,
  31. 'live_status': 'was_live',
  32. },
  33. }, {
  34. 'url': 'https://r.dcs.redcdn.pl/livehls/o2/sportevolution/live/triathlon2018/warsaw.livx/playlist.m3u8?startTime=550305000000&stopTime=550327620000',
  35. 'info_dict': {
  36. 'id': 'triathlon2018-warsaw-550305000000-550327620000',
  37. 'ext': 'mp4',
  38. 'title': 'triathlon2018/warsaw',
  39. 'duration': 22619.98,
  40. 'live_status': 'was_live',
  41. },
  42. }, {
  43. 'url': 'https://n-25-12.dcs.redcdn.pl/nvr/o2/sejm/Migacz-ENC01/1.livx?startTime=722347200000&stopTime=722367345000',
  44. 'only_matching': True,
  45. }, {
  46. 'url': 'https://redir.atmcdn.pl/nvr/o2/sejm/ENC08/1.livx?startTime=503831270000&stopTime=503840040000',
  47. 'only_matching': True,
  48. }]
  49. '''
  50. Known methods (first in url path):
  51. - `livedash` - DASH MPD
  52. - `livehls` - HTTP Live Streaming
  53. - `livess` - IIS Smooth Streaming
  54. - `nvr` - CCTV mode, directly returns a file, typically flv, avc1, aac
  55. - `sc` - shoutcast/icecast (audio streams, like radio)
  56. '''
  57. def _real_extract(self, url):
  58. tenant, path = self._match_valid_url(url).group('tenant', 'id')
  59. qs = parse_qs(url)
  60. start_time = traverse_obj(qs, ('startTime', 0, {int_or_none}))
  61. stop_time = traverse_obj(qs, ('stopTime', 0, {int_or_none}))
  62. def livx_mode(mode):
  63. suffix = ''
  64. if mode == 'livess':
  65. suffix = '/manifest'
  66. elif mode == 'livehls':
  67. suffix = '/playlist.m3u8'
  68. file_qs = {}
  69. if start_time:
  70. file_qs['startTime'] = start_time
  71. if stop_time:
  72. file_qs['stopTime'] = stop_time
  73. if mode == 'nvr':
  74. file_qs['nolimit'] = 1
  75. elif mode != 'sc':
  76. file_qs['indexMode'] = 'true'
  77. return update_url_query(f'https://r.dcs.redcdn.pl/{mode}/o2/{tenant}/{path}.livx{suffix}', file_qs)
  78. # no id or title for a transmission. making ones up.
  79. title = path \
  80. .replace('/live', '').replace('live/', '') \
  81. .replace('/channel', '').replace('channel/', '') \
  82. .strip('/')
  83. video_id = join_nonempty(title.replace('/', '-'), start_time, stop_time)
  84. formats = []
  85. # downloading the manifest separately here instead of _extract_ism_formats to also get some stream metadata
  86. ism_res = self._download_xml_handle(
  87. livx_mode('livess'), video_id,
  88. note='Downloading ISM manifest',
  89. errnote='Failed to download ISM manifest',
  90. fatal=False)
  91. ism_doc = None
  92. if ism_res is not False:
  93. ism_doc, ism_urlh = ism_res
  94. formats, _ = self._parse_ism_formats_and_subtitles(ism_doc, ism_urlh.url, 'ss')
  95. nvr_urlh = self._request_webpage(
  96. HEADRequest(livx_mode('nvr')), video_id, 'Follow flv file redirect', fatal=False,
  97. expected_status=lambda _: True)
  98. if nvr_urlh and nvr_urlh.status == 200:
  99. formats.append({
  100. 'url': nvr_urlh.url,
  101. 'ext': 'flv',
  102. 'format_id': 'direct-0',
  103. 'preference': -1, # might be slow
  104. })
  105. formats.extend(self._extract_mpd_formats(livx_mode('livedash'), video_id, mpd_id='dash', fatal=False))
  106. formats.extend(self._extract_m3u8_formats(
  107. livx_mode('livehls'), video_id, m3u8_id='hls', ext='mp4', fatal=False))
  108. time_scale = traverse_obj(ism_doc, ('@TimeScale', {int_or_none})) or 10000000
  109. duration = traverse_obj(
  110. ism_doc, ('@Duration', {functools.partial(float_or_none, scale=time_scale)})) or None
  111. live_status = None
  112. if traverse_obj(ism_doc, '@IsLive') == 'TRUE':
  113. live_status = 'is_live'
  114. elif duration:
  115. live_status = 'was_live'
  116. return {
  117. 'id': video_id,
  118. 'title': title,
  119. 'formats': formats,
  120. 'duration': duration,
  121. 'live_status': live_status,
  122. }