diff --git a/youtube_dl/downloader/__init__.py b/youtube_dl/downloader/__init__.py index dccc59212..808b9b6d8 100644 --- a/youtube_dl/downloader/__init__.py +++ b/youtube_dl/downloader/__init__.py @@ -9,6 +9,7 @@ from .http import HttpFD from .rtsp import RtspFD from .rtmp import RtmpFD from .dash import DashSegmentsFD +from .multipart import MultiPartFD from ..utils import ( determine_protocol, @@ -22,11 +23,16 @@ PROTOCOL_MAP = { 'rtsp': RtspFD, 'f4m': F4mFD, 'http_dash_segments': DashSegmentsFD, + 'multipart': MultiPartFD, } def get_suitable_downloader(info_dict, params={}): """Get the downloader class that can handle the info dict.""" + + if 'parts' in info_dict: + return MultiPartFD + protocol = determine_protocol(info_dict) info_dict['protocol'] = protocol diff --git a/youtube_dl/downloader/multipart.py b/youtube_dl/downloader/multipart.py new file mode 100644 index 000000000..20ba32819 --- /dev/null +++ b/youtube_dl/downloader/multipart.py @@ -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