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.

104 lines
3.6 KiB

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