]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/downloader/hls.py
Merge branch 'akamai_pv' of https://github.com/remitamine/youtube-dl into remitamine...
[youtube-dl.git] / youtube_dl / downloader / hls.py
1 from __future__ import unicode_literals
2
3 import os.path
4 import re
5
6 from .fragment import FragmentFD
7
8 from ..compat import compat_urlparse
9 from ..utils import (
10     encodeFilename,
11     sanitize_open,
12 )
13
14
15 class HlsFD(FragmentFD):
16     """ A limited implementation that does not require ffmpeg """
17
18     FD_NAME = 'hlsnative'
19
20     def real_download(self, filename, info_dict):
21         man_url = info_dict['url']
22         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
23         manifest = self.ydl.urlopen(man_url).read()
24
25         s = manifest.decode('utf-8', 'ignore')
26         fragment_urls = []
27         for line in s.splitlines():
28             line = line.strip()
29             if line and not line.startswith('#'):
30                 segment_url = (
31                     line
32                     if re.match(r'^https?://', line)
33                     else compat_urlparse.urljoin(man_url, line))
34                 fragment_urls.append(segment_url)
35                 # We only download the first fragment during the test
36                 if self.params.get('test', False):
37                     break
38
39         ctx = {
40             'filename': filename,
41             'total_frags': len(fragment_urls),
42         }
43
44         self._prepare_and_start_frag_download(ctx)
45
46         frags_filenames = []
47         for i, frag_url in enumerate(fragment_urls):
48             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
49             success = ctx['dl'].download(frag_filename, {'url': frag_url})
50             if not success:
51                 return False
52             down, frag_sanitized = sanitize_open(frag_filename, 'rb')
53             ctx['dest_stream'].write(down.read())
54             down.close()
55             frags_filenames.append(frag_sanitized)
56
57         self._finish_frag_download(ctx)
58
59         for frag_file in frags_filenames:
60             os.remove(encodeFilename(frag_file))
61
62         return True