1 from __future__ import unicode_literals
7 from .common import FileDownloader
8 from .fragment import FragmentFD
10 from ..compat import compat_urlparse
11 from ..postprocessor.ffmpeg import FFmpegPostProcessor
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)
25 ffpp = FFmpegPostProcessor(downloader=self)
26 if not ffpp.available:
27 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
33 for opt in (ffpp.executable, '-y', '-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
34 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
38 retval = subprocess.call(args)
40 fsize = os.path.getsize(encodeFilename(tmpfilename))
41 self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
42 self.try_rename(tmpfilename, filename)
44 'downloaded_bytes': fsize,
52 self.report_error('%s exited with code %d' % (ffpp.basename, retval))
56 class NativeHlsFD(FragmentFD):
57 """ A more limited implementation that does not require ffmpeg """
61 def real_download(self, filename, info_dict):
62 man_url = info_dict['url']
63 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
64 manifest = self.ydl.urlopen(man_url).read()
66 s = manifest.decode('utf-8', 'ignore')
68 for line in s.splitlines():
70 if line and not line.startswith('#'):
73 if re.match(r'^https?://', line)
74 else compat_urlparse.urljoin(man_url, line))
75 fragment_urls.append(segment_url)
76 # We only download the first fragment during the test
77 if self.params.get('test', False):
82 'total_frags': len(fragment_urls),
85 self._prepare_and_start_frag_download(ctx)
88 for i, frag_url in enumerate(fragment_urls):
89 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
90 success = ctx['dl'].download(frag_filename, {'url': frag_url})
93 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
94 ctx['dest_stream'].write(down.read())
96 frags_filenames.append(frag_sanitized)
98 self._finish_frag_download(ctx)
100 for frag_file in frags_filenames:
101 os.remove(encodeFilename(frag_file))