[hlsnative] Add support for download completion

This commit is contained in:
remitamine 2015-07-19 05:41:42 +01:00
parent e58066e244
commit 59158f40c2

View File

@ -13,8 +13,9 @@ from ..compat import (
from ..utils import ( from ..utils import (
encodeArgument, encodeArgument,
encodeFilename, encodeFilename,
sanitize_open,
) )
from .http import HttpFD
class HlsFD(FileDownloader): class HlsFD(FileDownloader):
def real_download(self, filename, info_dict): def real_download(self, filename, info_dict):
@ -58,6 +59,7 @@ class NativeHlsFD(FileDownloader):
url = info_dict['url'] url = info_dict['url']
self.report_destination(filename) self.report_destination(filename)
tmpfilename = self.temp_name(filename) tmpfilename = self.temp_name(filename)
(dest_stream, tmpfilename) = sanitize_open(tmpfilename, 'wb')
self.to_screen( self.to_screen(
'[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id']) '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
@ -73,32 +75,32 @@ class NativeHlsFD(FileDownloader):
else compat_urlparse.urljoin(url, line)) else compat_urlparse.urljoin(url, line))
segment_urls.append(segment_url) segment_urls.append(segment_url)
is_test = self.params.get('test', False) downloader = HttpFD(
remaining_bytes = self._TEST_FILE_SIZE if is_test else None self.ydl,
byte_counter = 0 {
with open(tmpfilename, 'wb') as outf: 'continuedl': True,
'quiet': True,
'noprogress': True,
}
)
for i, segurl in enumerate(segment_urls): for i, segurl in enumerate(segment_urls):
self.to_screen( self.to_screen(
'[hlsnative] %s: Downloading segment %d / %d' % '[hlsnative] %s: Downloading segment %d / %d' %
(info_dict['id'], i + 1, len(segment_urls))) (info_dict['id'], i + 1, len(segment_urls)))
seg_req = compat_urllib_request.Request(segurl) success = downloader.download(tmpfilename + str(i), {'url': segurl})
if remaining_bytes is not None: if not success:
seg_req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1)) return False
for i, segurl in enumerate(segment_urls):
segment = self.ydl.urlopen(seg_req).read() with open(tmpfilename + str(i), 'rb') as down:
if remaining_bytes is not None: dest_stream.write(down.read())
segment = segment[:remaining_bytes] os.remove(tmpfilename + str(i))
remaining_bytes -= len(segment) dest_stream.close()
outf.write(segment) self.try_rename(tmpfilename, filename)
byte_counter += len(segment) fsize = os.path.getsize(encodeFilename(filename))
if remaining_bytes is not None and remaining_bytes <= 0:
break
self._hook_progress({ self._hook_progress({
'downloaded_bytes': byte_counter, 'downloaded_bytes': fsize,
'total_bytes': byte_counter, 'total_bytes': fsize,
'filename': filename, 'filename': filename,
'status': 'finished', 'status': 'finished',
}) })
self.try_rename(tmpfilename, filename)
return True return True