hls.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
  23. # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
  24. # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
  25. )
  26. return all(not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
  27. def real_download(self, filename, info_dict):
  28. man_url = info_dict['url']
  29. self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
  30. manifest = self.ydl.urlopen(man_url).read()
  31. s = manifest.decode('utf-8', 'ignore')
  32. if not self.can_download(s):
  33. self.report_warning(
  34. 'hlsnative has detected features it does not support, '
  35. 'extraction will be delegated to ffmpeg')
  36. fd = FFmpegFD(self.ydl, self.params)
  37. for ph in self._progress_hooks:
  38. fd.add_progress_hook(ph)
  39. return fd.real_download(filename, info_dict)
  40. fragment_urls = []
  41. for line in s.splitlines():
  42. line = line.strip()
  43. if line and not line.startswith('#'):
  44. segment_url = (
  45. line
  46. if re.match(r'^https?://', line)
  47. else compat_urlparse.urljoin(man_url, line))
  48. fragment_urls.append(segment_url)
  49. # We only download the first fragment during the test
  50. if self.params.get('test', False):
  51. break
  52. ctx = {
  53. 'filename': filename,
  54. 'total_frags': len(fragment_urls),
  55. }
  56. self._prepare_and_start_frag_download(ctx)
  57. frags_filenames = []
  58. for i, frag_url in enumerate(fragment_urls):
  59. frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
  60. success = ctx['dl'].download(frag_filename, {'url': frag_url})
  61. if not success:
  62. return False
  63. down, frag_sanitized = sanitize_open(frag_filename, 'rb')
  64. ctx['dest_stream'].write(down.read())
  65. down.close()
  66. frags_filenames.append(frag_sanitized)
  67. self._finish_frag_download(ctx)
  68. for frag_file in frags_filenames:
  69. os.remove(encodeFilename(frag_file))
  70. return True