]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/downloader/hls.py
[downloader/hls] PEP 8
[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 from .external import FFmpegFD
8
9 from ..compat import compat_urlparse
10 from ..utils import (
11     encodeFilename,
12     sanitize_open,
13 )
14
15
16 class HlsFD(FragmentFD):
17     """ A limited implementation that does not require ffmpeg """
18
19     FD_NAME = 'hlsnative'
20
21     @staticmethod
22     def can_download(manifest):
23         UNSUPPORTED_FEATURES = (
24             r'#EXT-X-KEY:METHOD=(?!NONE)',  # encrypted streams [1]
25             r'#EXT-X-BYTERANGE',  # playlists composed of byte ranges of media files [2]
26             # Live streams heuristic does not always work (e.g. geo restricted to Germany
27             # http://hls-geo.daserste.de/i/videoportal/Film/c_620000/622873/format,716451,716457,716450,716458,716459,.mp4.csmil/index_4_av.m3u8?null=0)
28             # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)',  # live streams [3]
29             r'#EXT-X-PLAYLIST-TYPE:EVENT',  # media segments may be appended to the end of
30                                             # event media playlists [4]
31             # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
32             # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
33             # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
34             # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
35         )
36         return all(not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
37
38     def real_download(self, filename, info_dict):
39         man_url = info_dict['url']
40         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
41         manifest = self.ydl.urlopen(man_url).read()
42
43         s = manifest.decode('utf-8', 'ignore')
44
45         if not self.can_download(s):
46             self.report_warning(
47                 'hlsnative has detected features it does not support, '
48                 'extraction will be delegated to ffmpeg')
49             fd = FFmpegFD(self.ydl, self.params)
50             for ph in self._progress_hooks:
51                 fd.add_progress_hook(ph)
52             return fd.real_download(filename, info_dict)
53
54         fragment_urls = []
55         for line in s.splitlines():
56             line = line.strip()
57             if line and not line.startswith('#'):
58                 segment_url = (
59                     line
60                     if re.match(r'^https?://', line)
61                     else compat_urlparse.urljoin(man_url, line))
62                 fragment_urls.append(segment_url)
63                 # We only download the first fragment during the test
64                 if self.params.get('test', False):
65                     break
66
67         ctx = {
68             'filename': filename,
69             'total_frags': len(fragment_urls),
70         }
71
72         self._prepare_and_start_frag_download(ctx)
73
74         frags_filenames = []
75         for i, frag_url in enumerate(fragment_urls):
76             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
77             success = ctx['dl'].download(frag_filename, {'url': frag_url})
78             if not success:
79                 return False
80             down, frag_sanitized = sanitize_open(frag_filename, 'rb')
81             ctx['dest_stream'].write(down.read())
82             down.close()
83             frags_filenames.append(frag_sanitized)
84
85         self._finish_frag_download(ctx)
86
87         for frag_file in frags_filenames:
88             os.remove(encodeFilename(frag_file))
89
90         return True