2014-09-24 06:16:56 -06:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-02-19 11:29:24 -07:00
|
|
|
import os.path
|
2014-09-24 06:16:56 -06:00
|
|
|
import re
|
2013-09-23 09:59:27 -06:00
|
|
|
|
2015-07-28 14:28:30 -06:00
|
|
|
from .fragment import FragmentFD
|
|
|
|
|
|
|
|
from ..compat import compat_urlparse
|
2014-12-13 04:24:42 -07:00
|
|
|
from ..utils import (
|
2013-09-23 09:59:27 -06:00
|
|
|
encodeFilename,
|
2015-08-30 14:28:36 -06:00
|
|
|
sanitize_open,
|
2013-09-23 09:59:27 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-19 11:29:24 -07:00
|
|
|
class HlsFD(FragmentFD):
|
|
|
|
""" A limited implementation that does not require ffmpeg """
|
2014-09-24 06:16:56 -06:00
|
|
|
|
2015-07-28 14:28:30 -06:00
|
|
|
FD_NAME = 'hlsnative'
|
|
|
|
|
2014-09-24 06:16:56 -06:00
|
|
|
def real_download(self, filename, info_dict):
|
2015-07-28 14:28:30 -06:00
|
|
|
man_url = info_dict['url']
|
|
|
|
self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
|
|
|
|
manifest = self.ydl.urlopen(man_url).read()
|
2014-09-24 06:16:56 -06:00
|
|
|
|
2015-07-28 14:28:30 -06:00
|
|
|
s = manifest.decode('utf-8', 'ignore')
|
|
|
|
fragment_urls = []
|
2014-09-24 06:16:56 -06:00
|
|
|
for line in s.splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line and not line.startswith('#'):
|
|
|
|
segment_url = (
|
|
|
|
line
|
|
|
|
if re.match(r'^https?://', line)
|
2015-07-28 14:28:30 -06:00
|
|
|
else compat_urlparse.urljoin(man_url, line))
|
|
|
|
fragment_urls.append(segment_url)
|
|
|
|
# We only download the first fragment during the test
|
|
|
|
if self.params.get('test', False):
|
2014-09-24 06:38:40 -06:00
|
|
|
break
|
2014-09-24 06:16:56 -06:00
|
|
|
|
2015-07-28 14:28:30 -06:00
|
|
|
ctx = {
|
2014-09-24 06:16:56 -06:00
|
|
|
'filename': filename,
|
2015-07-28 14:28:30 -06:00
|
|
|
'total_frags': len(fragment_urls),
|
|
|
|
}
|
|
|
|
|
|
|
|
self._prepare_and_start_frag_download(ctx)
|
|
|
|
|
|
|
|
frags_filenames = []
|
|
|
|
for i, frag_url in enumerate(fragment_urls):
|
|
|
|
frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
|
|
|
|
success = ctx['dl'].download(frag_filename, {'url': frag_url})
|
|
|
|
if not success:
|
|
|
|
return False
|
2015-08-30 14:28:36 -06:00
|
|
|
down, frag_sanitized = sanitize_open(frag_filename, 'rb')
|
|
|
|
ctx['dest_stream'].write(down.read())
|
2015-09-09 14:41:30 -06:00
|
|
|
down.close()
|
2015-08-30 14:28:36 -06:00
|
|
|
frags_filenames.append(frag_sanitized)
|
2015-07-28 14:28:30 -06:00
|
|
|
|
|
|
|
self._finish_frag_download(ctx)
|
|
|
|
|
|
|
|
for frag_file in frags_filenames:
|
2015-08-30 14:28:36 -06:00
|
|
|
os.remove(encodeFilename(frag_file))
|
2015-07-28 14:28:30 -06:00
|
|
|
|
2014-09-24 06:16:56 -06:00
|
|
|
return True
|