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