1 from __future__
import unicode_literals
7 from Crypto
.Cipher
import AES
8 can_decrypt_frag
= True
10 can_decrypt_frag
= False
12 from .fragment
import FragmentFD
13 from .external
import FFmpegFD
15 from ..compat
import (
23 parse_m3u8_attributes
,
28 class HlsFD(FragmentFD
):
29 """ A limited implementation that does not require ffmpeg """
34 def can_download(manifest
, info_dict
):
35 UNSUPPORTED_FEATURES
= (
36 r
'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1]
37 r
'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
39 # Live streams heuristic does not always work (e.g. geo restricted to Germany
40 # 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)
41 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
43 # This heuristic also is not correct since segments may not be appended as well.
44 # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
45 # no segments will definitely be appended to the end of the playlist.
46 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
47 # # event media playlists [4]
49 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
50 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
51 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
52 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
54 check_results
= [not re
.search(feature
, manifest
) for feature
in UNSUPPORTED_FEATURES
]
55 check_results
.append(can_decrypt_frag
or '#EXT-X-KEY:METHOD=AES-128' not in manifest
)
56 check_results
.append(not info_dict
.get('is_live'))
57 return all(check_results
)
59 def real_download(self
, filename
, info_dict
):
60 man_url
= info_dict
['url']
61 self
.to_screen('[%s] Downloading m3u8 manifest' % self
.FD_NAME
)
63 manifest
= self
.ydl
.urlopen(self
._prepare
_url
(info_dict
, man_url
)).read()
65 s
= manifest
.decode('utf-8', 'ignore')
67 if not self
.can_download(s
, info_dict
):
68 if info_dict
.get('extra_param_to_segment_url'):
69 self
.report_error('pycrypto not found. Please install it.')
72 'hlsnative has detected features it does not support, '
73 'extraction will be delegated to ffmpeg')
74 fd
= FFmpegFD(self
.ydl
, self
.params
)
75 for ph
in self
._progress
_hooks
:
76 fd
.add_progress_hook(ph
)
77 return fd
.real_download(filename
, info_dict
)
80 for line
in s
.splitlines():
82 if line
and not line
.startswith('#'):
87 'total_frags': total_frags
,
90 self
._prepare
_and
_start
_frag
_download
(ctx
)
92 fragment_retries
= self
.params
.get('fragment_retries', 0)
93 skip_unavailable_fragments
= self
.params
.get('skip_unavailable_fragments', True)
94 test
= self
.params
.get('test', False)
97 extra_param_to_segment_url
= info_dict
.get('extra_param_to_segment_url')
98 if extra_param_to_segment_url
:
99 extra_query
= compat_urlparse
.parse_qs(extra_param_to_segment_url
)
102 decrypt_info
= {'METHOD': 'NONE'}
104 for line
in s
.splitlines():
107 if not line
.startswith('#'):
110 if re
.match(r
'^https?://', line
)
111 else compat_urlparse
.urljoin(man_url
, line
))
112 frag_name
= 'Frag%d' % i
113 frag_filename
= '%s-%s' % (ctx
['tmpfilename'], frag_name
)
115 frag_url
= update_url_query(frag_url
, extra_query
)
117 while count
<= fragment_retries
:
119 success
= ctx
['dl'].download(frag_filename
, {
121 'http_headers': info_dict
.get('http_headers'),
125 down
, frag_sanitized
= sanitize_open(frag_filename
, 'rb')
126 frag_content
= down
.read()
129 except compat_urllib_error
.HTTPError
as err
:
130 # Unavailable (possibly temporary) fragments may be served.
131 # First we try to retry then either skip or abort.
132 # See https://github.com/rg3/youtube-dl/issues/10165,
133 # https://github.com/rg3/youtube-dl/issues/10448).
135 if count
<= fragment_retries
:
136 self
.report_retry_fragment(err
, frag_name
, count
, fragment_retries
)
137 if count
> fragment_retries
:
138 if skip_unavailable_fragments
:
141 self
.report_skip_fragment(frag_name
)
144 'giving up after %s fragment retries' % fragment_retries
)
146 if decrypt_info
['METHOD'] == 'AES-128':
147 iv
= decrypt_info
.get('IV') or compat_struct_pack('>8xq', media_sequence
)
148 frag_content
= AES
.new(
149 decrypt_info
['KEY'], AES
.MODE_CBC
, iv
).decrypt(frag_content
)
150 ctx
['dest_stream'].write(frag_content
)
151 frags_filenames
.append(frag_sanitized
)
152 # We only download the first fragment during the test
157 elif line
.startswith('#EXT-X-KEY'):
158 decrypt_info
= parse_m3u8_attributes(line
[11:])
159 if decrypt_info
['METHOD'] == 'AES-128':
160 if 'IV' in decrypt_info
:
161 decrypt_info
['IV'] = binascii
.unhexlify(decrypt_info
['IV'][2:].zfill(32))
162 if not re
.match(r
'^https?://', decrypt_info
['URI']):
163 decrypt_info
['URI'] = compat_urlparse
.urljoin(
164 man_url
, decrypt_info
['URI'])
166 decrypt_info
['URI'] = update_url_query(decrypt_info
['URI'], extra_query
)
167 decrypt_info
['KEY'] = self
.ydl
.urlopen(decrypt_info
['URI']).read()
168 elif line
.startswith('#EXT-X-MEDIA-SEQUENCE'):
169 media_sequence
= int(line
[22:])
171 self
._finish
_frag
_download
(ctx
)
173 for frag_file
in frags_filenames
:
174 os
.remove(encodeFilename(frag_file
))