1 from __future__ import unicode_literals
7 from ..postprocessor.ffmpeg import FFmpegPostProcessor
8 from .common import FileDownloader
11 compat_urllib_request,
19 class HlsFD(FileDownloader):
20 def real_download(self, filename, info_dict):
21 url = info_dict['url']
22 self.report_destination(filename)
23 tmpfilename = self.temp_name(filename)
26 '-y', '-i', url, '-f', 'mp4', '-c', 'copy',
27 '-bsf:a', 'aac_adtstoasc',
28 encodeFilename(tmpfilename, for_subprocess=True)]
30 for program in ['avconv', 'ffmpeg']:
31 if check_executable(program, ['-version']):
34 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
36 cmd = [program] + args
38 ffpp = FFmpegPostProcessor(downloader=self)
41 retval = subprocess.call(cmd)
43 fsize = os.path.getsize(encodeFilename(tmpfilename))
44 self.to_screen('\r[%s] %s bytes' % (cmd[0], fsize))
45 self.try_rename(tmpfilename, filename)
47 'downloaded_bytes': fsize,
55 self.report_error('%s exited with code %d' % (program, retval))
59 class NativeHlsFD(FileDownloader):
60 """ A more limited implementation that does not require ffmpeg """
62 def real_download(self, filename, info_dict):
63 url = info_dict['url']
64 self.report_destination(filename)
65 tmpfilename = self.temp_name(filename)
68 '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
69 data = self.ydl.urlopen(url).read()
70 s = data.decode('utf-8', 'ignore')
72 for line in s.splitlines():
74 if line and not line.startswith('#'):
77 if re.match(r'^https?://', line)
78 else compat_urlparse.urljoin(url, line))
79 segment_urls.append(segment_url)
81 is_test = self.params.get('test', False)
82 remaining_bytes = self._TEST_FILE_SIZE if is_test else None
84 with open(tmpfilename, 'wb') as outf:
85 for i, segurl in enumerate(segment_urls):
87 '[hlsnative] %s: Downloading segment %d / %d' %
88 (info_dict['id'], i + 1, len(segment_urls)))
89 seg_req = compat_urllib_request.Request(segurl)
90 if remaining_bytes is not None:
91 seg_req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1))
93 segment = self.ydl.urlopen(seg_req).read()
94 if remaining_bytes is not None:
95 segment = segment[:remaining_bytes]
96 remaining_bytes -= len(segment)
98 byte_counter += len(segment)
99 if remaining_bytes is not None and remaining_bytes <= 0:
102 self._hook_progress({
103 'downloaded_bytes': byte_counter,
104 'total_bytes': byte_counter,
105 'filename': filename,
106 'status': 'finished',
108 self.try_rename(tmpfilename, filename)