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.

103 lines
3.6 KiB

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