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.

105 lines
3.6 KiB

  1. from __future__ import unicode_literals
  2. import os
  3. import re
  4. import subprocess
  5. from ..postprocessor.ffmpeg import FFmpegPostProcessor
  6. from .common import FileDownloader
  7. from ..compat import (
  8. compat_urlparse,
  9. compat_urllib_request,
  10. )
  11. from ..utils import (
  12. encodeFilename,
  13. )
  14. class HlsFD(FileDownloader):
  15. def real_download(self, filename, info_dict):
  16. url = info_dict['url']
  17. self.report_destination(filename)
  18. tmpfilename = self.temp_name(filename)
  19. args = [
  20. '-y', '-i', url, '-f', 'mp4', '-c', 'copy',
  21. '-bsf:a', 'aac_adtstoasc',
  22. encodeFilename(tmpfilename, for_subprocess=True)]
  23. ffpp = FFmpegPostProcessor(downloader=self)
  24. program = ffpp._executable
  25. if program is None:
  26. self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
  27. return False
  28. ffpp.check_version()
  29. cmd = [program] + args
  30. retval = subprocess.call(cmd)
  31. if retval == 0:
  32. fsize = os.path.getsize(encodeFilename(tmpfilename))
  33. self.to_screen('\r[%s] %s bytes' % (cmd[0], fsize))
  34. self.try_rename(tmpfilename, filename)
  35. self._hook_progress({
  36. 'downloaded_bytes': fsize,
  37. 'total_bytes': fsize,
  38. 'filename': filename,
  39. 'status': 'finished',
  40. })
  41. return True
  42. else:
  43. self.to_stderr('\n')
  44. self.report_error('%s exited with code %d' % (program, retval))
  45. return False
  46. class NativeHlsFD(FileDownloader):
  47. """ A more limited implementation that does not require ffmpeg """
  48. def real_download(self, filename, info_dict):
  49. url = info_dict['url']
  50. self.report_destination(filename)
  51. tmpfilename = self.temp_name(filename)
  52. self.to_screen(
  53. '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
  54. data = self.ydl.urlopen(url).read()
  55. s = data.decode('utf-8', 'ignore')
  56. segment_urls = []
  57. for line in s.splitlines():
  58. line = line.strip()
  59. if line and not line.startswith('#'):
  60. segment_url = (
  61. line
  62. if re.match(r'^https?://', line)
  63. else compat_urlparse.urljoin(url, line))
  64. segment_urls.append(segment_url)
  65. is_test = self.params.get('test', False)
  66. remaining_bytes = self._TEST_FILE_SIZE if is_test else None
  67. byte_counter = 0
  68. with open(tmpfilename, 'wb') as outf:
  69. for i, segurl in enumerate(segment_urls):
  70. self.to_screen(
  71. '[hlsnative] %s: Downloading segment %d / %d' %
  72. (info_dict['id'], i + 1, len(segment_urls)))
  73. seg_req = compat_urllib_request.Request(segurl)
  74. if remaining_bytes is not None:
  75. seg_req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1))
  76. segment = self.ydl.urlopen(seg_req).read()
  77. if remaining_bytes is not None:
  78. segment = segment[:remaining_bytes]
  79. remaining_bytes -= len(segment)
  80. outf.write(segment)
  81. byte_counter += len(segment)
  82. if remaining_bytes is not None and remaining_bytes <= 0:
  83. break
  84. self._hook_progress({
  85. 'downloaded_bytes': byte_counter,
  86. 'total_bytes': byte_counter,
  87. 'filename': filename,
  88. 'status': 'finished',
  89. })
  90. self.try_rename(tmpfilename, filename)
  91. return True