]> jfr.im git - yt-dlp.git/blame - youtube_dl/downloader/hls.py
[downloader/hls] PEP 8
[yt-dlp.git] / youtube_dl / downloader / hls.py
CommitLineData
f0b5d6af
PH
1from __future__ import unicode_literals
2
12b84ac8 3import os.path
f0b5d6af 4import re
3bc2ddcc 5
f9a5affa 6from .fragment import FragmentFD
0d66bd0e 7from .external import FFmpegFD
f9a5affa
S
8
9from ..compat import compat_urlparse
1cc79574 10from ..utils import (
3bc2ddcc 11 encodeFilename,
fcd9e423 12 sanitize_open,
3bc2ddcc
JMF
13)
14
15
12b84ac8 16class HlsFD(FragmentFD):
17 """ A limited implementation that does not require ffmpeg """
f0b5d6af 18
f9a5affa
S
19 FD_NAME = 'hlsnative'
20
0d66bd0e
S
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]
1e236d7e 26
c15c47d1
S
27 # Live streams heuristic does not always work (e.g. geo restricted to Germany
28 # 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)
2937590e 29 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
1e236d7e
S
30
31 # This heuristic also is not correct since segments may not be appended as well.
32 # Twitch vods have EXT-X-PLAYLIST-TYPE:EVENT despite no segments will definitely
33 # be appended to the end of the playlist.
34 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
51c4d85c 35 # # event media playlists [4]
1e236d7e 36
0d66bd0e
S
37 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
38 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
39 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
6104cc29 40 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
0d66bd0e
S
41 )
42 return all(not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
43
f0b5d6af 44 def real_download(self, filename, info_dict):
f9a5affa
S
45 man_url = info_dict['url']
46 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
47 manifest = self.ydl.urlopen(man_url).read()
f0b5d6af 48
f9a5affa 49 s = manifest.decode('utf-8', 'ignore')
0d66bd0e
S
50
51 if not self.can_download(s):
52 self.report_warning(
53 'hlsnative has detected features it does not support, '
54 'extraction will be delegated to ffmpeg')
55 fd = FFmpegFD(self.ydl, self.params)
56 for ph in self._progress_hooks:
57 fd.add_progress_hook(ph)
58 return fd.real_download(filename, info_dict)
59
f9a5affa 60 fragment_urls = []
f0b5d6af
PH
61 for line in s.splitlines():
62 line = line.strip()
63 if line and not line.startswith('#'):
64 segment_url = (
65 line
66 if re.match(r'^https?://', line)
f9a5affa
S
67 else compat_urlparse.urljoin(man_url, line))
68 fragment_urls.append(segment_url)
69 # We only download the first fragment during the test
70 if self.params.get('test', False):
b686fc18 71 break
f0b5d6af 72
f9a5affa 73 ctx = {
f0b5d6af 74 'filename': filename,
f9a5affa
S
75 'total_frags': len(fragment_urls),
76 }
77
78 self._prepare_and_start_frag_download(ctx)
79
80 frags_filenames = []
81 for i, frag_url in enumerate(fragment_urls):
82 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
83 success = ctx['dl'].download(frag_filename, {'url': frag_url})
84 if not success:
85 return False
fcd9e423
S
86 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
87 ctx['dest_stream'].write(down.read())
133a2b4a 88 down.close()
fcd9e423 89 frags_filenames.append(frag_sanitized)
f9a5affa
S
90
91 self._finish_frag_download(ctx)
92
93 for frag_file in frags_filenames:
fcd9e423 94 os.remove(encodeFilename(frag_file))
f9a5affa 95
f0b5d6af 96 return True