]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/downloader/hls.py
Merge pull request #9358 from dstftw/hls-native-to-ffmpeg-delegation
[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             r'#EXT-X-MEDIA-SEQUENCE:(?!0$)',  # live streams [3]
27             # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
28             # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
29             # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
30         )
31         return all(not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
32
33     def real_download(self, filename, info_dict):
34         man_url = info_dict['url']
35         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
36         manifest = self.ydl.urlopen(man_url).read()
37
38         s = manifest.decode('utf-8', 'ignore')
39
40         if not self.can_download(s):
41             self.report_warning(
42                 'hlsnative has detected features it does not support, '
43                 'extraction will be delegated to ffmpeg')
44             fd = FFmpegFD(self.ydl, self.params)
45             for ph in self._progress_hooks:
46                 fd.add_progress_hook(ph)
47             return fd.real_download(filename, info_dict)
48
49         fragment_urls = []
50         for line in s.splitlines():
51             line = line.strip()
52             if line and not line.startswith('#'):
53                 segment_url = (
54                     line
55                     if re.match(r'^https?://', line)
56                     else compat_urlparse.urljoin(man_url, line))
57                 fragment_urls.append(segment_url)
58                 # We only download the first fragment during the test
59                 if self.params.get('test', False):
60                     break
61
62         ctx = {
63             'filename': filename,
64             'total_frags': len(fragment_urls),
65         }
66
67         self._prepare_and_start_frag_download(ctx)
68
69         frags_filenames = []
70         for i, frag_url in enumerate(fragment_urls):
71             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
72             success = ctx['dl'].download(frag_filename, {'url': frag_url})
73             if not success:
74                 return False
75             down, frag_sanitized = sanitize_open(frag_filename, 'rb')
76             ctx['dest_stream'].write(down.read())
77             down.close()
78             frags_filenames.append(frag_sanitized)
79
80         self._finish_frag_download(ctx)
81
82         for frag_file in frags_filenames:
83             os.remove(encodeFilename(frag_file))
84
85         return True