]> jfr.im git - yt-dlp.git/blame - youtube_dl/downloader/hls.py
Merge pull request #8611 from remitamine/ffmpegfd
[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
S
6from .fragment import FragmentFD
7
8from ..compat import compat_urlparse
1cc79574 9from ..utils import (
3bc2ddcc 10 encodeFilename,
fcd9e423 11 sanitize_open,
3bc2ddcc
JMF
12)
13
14
12b84ac8 15class HlsFD(FragmentFD):
16 """ A limited implementation that does not require ffmpeg """
f0b5d6af 17
f9a5affa
S
18 FD_NAME = 'hlsnative'
19
f0b5d6af 20 def real_download(self, filename, info_dict):
f9a5affa
S
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()
f0b5d6af 24
f9a5affa
S
25 s = manifest.decode('utf-8', 'ignore')
26 fragment_urls = []
f0b5d6af
PH
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)
f9a5affa
S
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):
b686fc18 37 break
f0b5d6af 38
f9a5affa 39 ctx = {
f0b5d6af 40 'filename': filename,
f9a5affa
S
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
fcd9e423
S
52 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
53 ctx['dest_stream'].write(down.read())
133a2b4a 54 down.close()
fcd9e423 55 frags_filenames.append(frag_sanitized)
f9a5affa
S
56
57 self._finish_frag_download(ctx)
58
59 for frag_file in frags_filenames:
fcd9e423 60 os.remove(encodeFilename(frag_file))
f9a5affa 61
f0b5d6af 62 return True