From bae4ce394c3b4cc61fd7012f513168b62a2b7831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Mon, 20 Jul 2015 22:59:17 +0200 Subject: [PATCH] Use a custom downloader for merging formats --- youtube_dl/YoutubeDL.py | 12 +--------- youtube_dl/downloader/__init__.py | 4 ++++ youtube_dl/downloader/common.py | 3 ++- youtube_dl/downloader/merge.py | 37 +++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 youtube_dl/downloader/merge.py diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 00af78e06..4fa708cb5 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -71,7 +71,6 @@ from .utils import ( write_json_file, write_string, YoutubeDLHandler, - prepend_extension, replace_extension, args_to_str, age_restricted, @@ -1377,7 +1376,6 @@ class YoutubeDL(object): return fd.download(name, info) if info_dict.get('requested_formats') is not None: - downloaded = [] success = True merger = FFmpegMergerPP(self) if not merger.available: @@ -1420,16 +1418,8 @@ class YoutubeDL(object): '[download] %s has already been downloaded and ' 'merged' % filename) else: - for f in requested_formats: - new_info = dict(info_dict) - new_info.update(f) - fname = self.prepare_filename(new_info) - fname = prepend_extension(fname, 'f%s' % f['format_id'], new_info['ext']) - downloaded.append(fname) - partial_success = dl(fname, new_info) - success = success and partial_success + success = dl(filename, info_dict) info_dict['__postprocessors'] = postprocessors - info_dict['__files_to_merge'] = downloaded else: # Just a single file success = dl(filename, info_dict) diff --git a/youtube_dl/downloader/__init__.py b/youtube_dl/downloader/__init__.py index dccc59212..7b0aca8ad 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 .merge import MergeFD from ..utils import ( determine_protocol, @@ -27,6 +28,9 @@ PROTOCOL_MAP = { def get_suitable_downloader(info_dict, params={}): """Get the downloader class that can handle the info dict.""" + if info_dict.get('requested_formats') is not None: + return MergeFD + protocol = determine_protocol(info_dict) info_dict['protocol'] = protocol diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index 97e755d4b..ad971772b 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -137,7 +137,8 @@ class FileDownloader(object): return int(round(number * multiplier)) def to_screen(self, *args, **kargs): - self.ydl.to_screen(*args, **kargs) + if not self.params.get('quiet'): + self.ydl.to_screen(*args, **kargs) def to_stderr(self, message): self.ydl.to_screen(message) diff --git a/youtube_dl/downloader/merge.py b/youtube_dl/downloader/merge.py new file mode 100644 index 000000000..7eadeb9b1 --- /dev/null +++ b/youtube_dl/downloader/merge.py @@ -0,0 +1,37 @@ +from __future__ import unicode_literals + +from .common import FileDownloader +import youtube_dl +from ..utils import prepend_extension + + +class MergeFD(FileDownloader): + def real_download(self, filename, info_dict): + infos = [] + for f in info_dict['requested_formats']: + new_info = dict(info_dict) + del new_info['requested_formats'] + new_info.update(f) + fname = self.ydl.prepare_filename(new_info) + fname = prepend_extension(fname, 'f%s' % f['format_id'], new_info['ext']) + infos.append((fname, new_info)) + success = True + for fname, info in infos: + params = dict(self.params) + params.update({ + 'quiet': True, + 'noprogress': True, + }) + fd = youtube_dl.downloader.get_suitable_downloader(info, self.params)(self.ydl, params) + + def hook(status): + self._hook_progress(status) + + fd.add_progress_hook(hook) + self.report_destination(fname) + partial_success = fd.download(fname, info) + success = success and partial_success + + info_dict['__files_to_merge'] = [fname for fname, _ in infos] + + return True