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_json_file,
write_string, write_string,
YoutubeDLHandler, YoutubeDLHandler,
prepend_extension,
replace_extension, replace_extension,
args_to_str, args_to_str,
age_restricted, age_restricted,
@ -1377,7 +1376,6 @@ class YoutubeDL(object):
return fd.download(name, info) return fd.download(name, info)
if info_dict.get('requested_formats') is not None: if info_dict.get('requested_formats') is not None:
downloaded = []
success = True success = True
merger = FFmpegMergerPP(self) merger = FFmpegMergerPP(self)
if not merger.available: if not merger.available:
@ -1420,16 +1418,8 @@ class YoutubeDL(object):
'[download] %s has already been downloaded and ' '[download] %s has already been downloaded and '
'merged' % filename) 'merged' % filename)
else: else:
for f in requested_formats: success = dl(filename, info_dict)
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
info_dict['__postprocessors'] = postprocessors info_dict['__postprocessors'] = postprocessors
info_dict['__files_to_merge'] = downloaded
else: else:
# Just a single file # Just a single file
success = dl(filename, info_dict) success = dl(filename, info_dict)

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 .merge import MergeFD
from ..utils import ( from ..utils import (
determine_protocol, determine_protocol,
@ -27,6 +28,9 @@ PROTOCOL_MAP = {
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 info_dict.get('requested_formats') is not None:
return MergeFD
protocol = determine_protocol(info_dict) protocol = determine_protocol(info_dict)
info_dict['protocol'] = protocol info_dict['protocol'] = protocol

View File

@ -137,7 +137,8 @@ class FileDownloader(object):
return int(round(number * multiplier)) return int(round(number * multiplier))
def to_screen(self, *args, **kargs): 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): def to_stderr(self, message):
self.ydl.to_screen(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