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
18 class HlsFD(FileDownloader):
19 def real_download(self, filename, info_dict):
20 url = info_dict['url']
21 self.report_destination(filename)
22 tmpfilename = self.temp_name(filename)
24 ffpp = FFmpegPostProcessor(downloader=self)
25 if not ffpp.available:
26 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
32 for opt in (ffpp.executable, '-y', '-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
33 args.append(encodeFilename(tmpfilename, True))
35 retval = subprocess.call(args)
37 fsize = os.path.getsize(encodeFilename(tmpfilename))
38 self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
39 self.try_rename(tmpfilename, filename)
41 'downloaded_bytes': fsize,
49 self.report_error('%s exited with code %d' % (ffpp.basename, retval))
53 class NativeHlsFD(FragmentFD):
54 """ A more limited implementation that does not require ffmpeg """
58 def real_download(self, filename, info_dict):
59 man_url = info_dict['url']
60 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
61 manifest = self.ydl.urlopen(man_url).read()
63 s = manifest.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(man_url, line))
72 fragment_urls.append(segment_url)
73 # We only download the first fragment during the test
74 if self.params.get('test', False):
79 'total_frags': len(fragment_urls),
82 self._prepare_and_start_frag_download(ctx)
85 for i, frag_url in enumerate(fragment_urls):
86 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
87 success = ctx['dl'].download(frag_filename, {'url': frag_url})
90 with open(frag_filename, 'rb') as down:
91 ctx['dest_stream'].write(down.read())
92 frags_filenames.append(frag_filename)
94 self._finish_frag_download(ctx)
96 for frag_file in frags_filenames: