[downloader/multipart] add a simple implementation of a multipart downloader

This commit is contained in:
remitamine 2016-01-13 17:13:46 +01:00
parent 4479fccc2b
commit 21a867912e
2 changed files with 52 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from .http import HttpFD
from .rtsp import RtspFD from .rtsp import RtspFD
from .rtmp import RtmpFD from .rtmp import RtmpFD
from .dash import DashSegmentsFD from .dash import DashSegmentsFD
from .multipart import MultiPartFD
from ..utils import ( from ..utils import (
determine_protocol, determine_protocol,
@ -22,11 +23,16 @@ PROTOCOL_MAP = {
'rtsp': RtspFD, 'rtsp': RtspFD,
'f4m': F4mFD, 'f4m': F4mFD,
'http_dash_segments': DashSegmentsFD, 'http_dash_segments': DashSegmentsFD,
'multipart': MultiPartFD,
} }
def get_suitable_downloader(info_dict, params={}): def get_suitable_downloader(info_dict, params={}):
"""Get the downloader class that can handle the info dict.""" """Get the downloader class that can handle the info dict."""
if 'parts' in info_dict:
return MultiPartFD
protocol = determine_protocol(info_dict) protocol = determine_protocol(info_dict)
info_dict['protocol'] = protocol info_dict['protocol'] = protocol

View File

@ -0,0 +1,46 @@
from __future__ import unicode_literals
import os
from .fragment import FragmentFD
from ..utils import (
encodeFilename,
sanitize_open,
)
class MultiPartFD(FragmentFD):
""" A more limited implementation that does not require ffmpeg """
FD_NAME = 'multipart'
def real_download(self, filename, info_dict):
parts = info_dict['parts']
ctx = {
'filename': filename,
'total_frags': len(parts),
}
self._prepare_and_start_frag_download(ctx)
frags_filenames = []
for i in range(len(parts)):
frag_filename = '%s%d' % (ctx['tmpfilename'], i)
success = ctx['dl'].download(frag_filename, {'url': parts[i]['url']})
if not success:
return False
down, frag_sanitized = sanitize_open(frag_filename, 'rb')
ctx['dest_stream'].write(down.read())
down.close()
frags_filenames.append(frag_sanitized)
# We only download the first fragment during the test
if self.params.get('test', False):
break
self._finish_frag_download(ctx)
for frag_file in frags_filenames:
os.remove(encodeFilename(frag_file))
return True