Use a custom downloader for merging formats

This commit is contained in:
Jaime Marquínez Ferrándiz 2015-07-20 22:59:17 +02:00
parent 2fe1ff8582
commit bae4ce394c
4 changed files with 44 additions and 12 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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