1 from __future__ import unicode_literals
7 from .common import FileDownloader
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)
22 '-y', '-i', url, '-f', 'mp4', '-c', 'copy',
23 '-bsf:a', 'aac_adtstoasc',
24 encodeFilename(tmpfilename, for_subprocess=True)]
26 for program in ['avconv', 'ffmpeg']:
27 if check_executable(program, ['-version']):
30 self.report_error(u'm3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
32 cmd = [program] + args
34 retval = subprocess.call(cmd)
36 fsize = os.path.getsize(encodeFilename(tmpfilename))
37 self.to_screen(u'\r[%s] %s bytes' % (cmd[0], fsize))
38 self.try_rename(tmpfilename, filename)
40 'downloaded_bytes': fsize,
48 self.report_error(u'%s exited with code %d' % (program, retval))
52 class NativeHlsFD(FileDownloader):
53 """ A more limited implementation that does not require ffmpeg """
55 def real_download(self, filename, info_dict):
56 url = info_dict['url']
57 self.report_destination(filename)
58 tmpfilename = self.temp_name(filename)
61 '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
62 data = self.ydl.urlopen(url).read()
63 s = data.decode('utf-8', 'ignore')
65 for line in s.splitlines():
67 if line and not line.startswith('#'):
70 if re.match(r'^https?://', line)
71 else compat_urlparse.urljoin(url, line))
72 segment_urls.append(segment_url)
75 with open(tmpfilename, 'wb') as outf:
76 for i, segurl in enumerate(segment_urls):
77 segment = self.ydl.urlopen(segurl).read()
79 byte_counter += len(segment)
81 '[hlsnative] %s: Downloading segment %d / %d' %
82 (info_dict['id'], i + 1, len(segment_urls)))
85 'downloaded_bytes': byte_counter,
86 'total_bytes': byte_counter,
90 self.try_rename(tmpfilename, filename)