You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.7 KiB

  1. from __future__ import unicode_literals
  2. import os.path
  3. import re
  4. from .fragment import FragmentFD
  5. from .external import FFmpegFD
  6. from ..compat import compat_urlparse
  7. from ..utils import (
  8. encodeFilename,
  9. sanitize_open,
  10. )
  11. class HlsFD(FragmentFD):
  12. """ A limited implementation that does not require ffmpeg """
  13. FD_NAME = 'hlsnative'
  14. @staticmethod
  15. def can_download(manifest):
  16. UNSUPPORTED_FEATURES = (
  17. r'#EXT-X-KEY:METHOD=(?!NONE)', # encrypted streams [1]
  18. r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
  19. # Live streams heuristic does not always work (e.g. geo restricted to Germany
  20. # http://hls-geo.daserste.de/i/videoportal/Film/c_620000/622873/format,716451,716457,716450,716458,716459,.mp4.csmil/index_4_av.m3u8?null=0)
  21. # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
  22. # This heuristic also is not correct since segments may not be appended as well.
  23. # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
  24. # no segments will definitely be appended to the end of the playlist.
  25. # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
  26. # # event media playlists [4]
  27. # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
  28. # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
  29. # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
  30. # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
  31. )
  32. return all(not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
  33. def real_download(self, filename, info_dict):
  34. man_url = info_dict['url']
  35. self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
  36. manifest = self.ydl.urlopen(man_url).read()
  37. s = manifest.decode('utf-8', 'ignore')
  38. if not self.can_download(s):
  39. self.report_warning(
  40. 'hlsnative has detected features it does not support, '
  41. 'extraction will be delegated to ffmpeg')
  42. fd = FFmpegFD(self.ydl, self.params)
  43. for ph in self._progress_hooks:
  44. fd.add_progress_hook(ph)
  45. return fd.real_download(filename, info_dict)
  46. fragment_urls = []
  47. for line in s.splitlines():
  48. line = line.strip()
  49. if line and not line.startswith('#'):
  50. segment_url = (
  51. line
  52. if re.match(r'^https?://', line)
  53. else compat_urlparse.urljoin(man_url, line))
  54. fragment_urls.append(segment_url)
  55. # We only download the first fragment during the test
  56. if self.params.get('test', False):
  57. break
  58. ctx = {
  59. 'filename': filename,
  60. 'total_frags': len(fragment_urls),
  61. }
  62. self._prepare_and_start_frag_download(ctx)
  63. frags_filenames = []
  64. for i, frag_url in enumerate(fragment_urls):
  65. frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
  66. success = ctx['dl'].download(frag_filename, {'url': frag_url})
  67. if not success:
  68. return False
  69. down, frag_sanitized = sanitize_open(frag_filename, 'rb')
  70. ctx['dest_stream'].write(down.read())
  71. down.close()
  72. frags_filenames.append(frag_sanitized)
  73. self._finish_frag_download(ctx)
  74. for frag_file in frags_filenames:
  75. os.remove(encodeFilename(frag_file))
  76. return True