1
0
mirror of https://github.com/l1ving/youtube-dl synced 2025-03-07 07:27:15 +08:00

[FragmentFD] only write fragment to disk if --keep-fragments is given

This commit is contained in:
Mickey Rose 2020-06-23 12:20:12 +02:00
parent 2915ae8eb6
commit 6ec9586e02
4 changed files with 37 additions and 22 deletions

View File

@ -192,8 +192,8 @@ Alternatively, refer to the [developer instructions](#developer-instructions) fo
--abort-on-unavailable-fragment Abort downloading when some fragment is not --abort-on-unavailable-fragment Abort downloading when some fragment is not
available available
--keep-fragments Keep downloaded fragments on disk after --keep-fragments Keep downloaded fragments on disk after
downloading is finished; fragments are downloading is finished; fragments are not
erased by default saved by default
--buffer-size SIZE Size of download buffer (e.g. 1024 or 16K) --buffer-size SIZE Size of download buffer (e.g. 1024 or 16K)
(default is 1024) (default is 1024)
--no-resize-buffer Do not automatically adjust the buffer --no-resize-buffer Do not automatically adjust the buffer

View File

@ -1,5 +1,6 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import io
import os import os
import time import time
import json import json
@ -96,18 +97,26 @@ class FragmentFD(FileDownloader):
frag_index_stream.close() frag_index_stream.close()
def _download_fragment(self, ctx, frag_url, info_dict, headers=None): def _download_fragment(self, ctx, frag_url, info_dict, headers=None):
fragment_filename = '%s-Frag%d' % (ctx['tmpfilename'], ctx['fragment_index']) if self.params.get('keep_fragments', False):
success = ctx['dl'].download(fragment_filename, { frag_file = None
frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], ctx['fragment_index'])
else:
frag_file = io.BytesIO()
frag_filename = None
success = ctx['dl'].download(frag_file or frag_filename, {
'url': frag_url, 'url': frag_url,
'http_headers': headers or info_dict.get('http_headers'), 'http_headers': headers or info_dict.get('http_headers'),
}) })
if not success: if not success:
return False, None frag_content = None
down, frag_sanitized = sanitize_open(fragment_filename, 'rb') elif self.params.get('keep_fragments', False):
ctx['fragment_filename_sanitized'] = frag_sanitized frag_file, frag_sanitized = sanitize_open(frag_filename, 'rb')
frag_content = down.read() frag_content = frag_file.read()
down.close() else:
return True, frag_content frag_content = frag_file.getvalue()
if frag_file:
frag_file.close()
return success, frag_content
def _append_fragment(self, ctx, frag_content): def _append_fragment(self, ctx, frag_content):
try: try:
@ -116,9 +125,6 @@ class FragmentFD(FileDownloader):
finally: finally:
if self.__do_ytdl_file(ctx): if self.__do_ytdl_file(ctx):
self._write_ytdl_file(ctx) self._write_ytdl_file(ctx)
if not self.params.get('keep_fragments', False):
os.remove(encodeFilename(ctx['fragment_filename_sanitized']))
del ctx['fragment_filename_sanitized']
def _prepare_frag_download(self, ctx): def _prepare_frag_download(self, ctx):
if 'live' not in ctx: if 'live' not in ctx:

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import errno import errno
import io
import os import os
import socket import socket
import time import time
@ -33,9 +34,15 @@ class DownloadContext(dict):
class HttpFD(FileDownloader): class HttpFD(FileDownloader):
def create_context(self, filename, info_dict): def create_context(self, filename, info_dict):
ctx = DownloadContext() ctx = DownloadContext()
ctx.filename = filename
ctx.tmpfilename = self.temp_name(filename) if isinstance(filename, io.IOBase):
ctx.stream = None ctx.filename = '-'
ctx.tmpfilename = '-'
ctx.stream = filename
else:
ctx.filename = filename
ctx.tmpfilename = self.temp_name(filename)
ctx.stream = None
ctx.open_mode = 'wb' ctx.open_mode = 'wb'
ctx.resume_len = 0 ctx.resume_len = 0
@ -44,7 +51,8 @@ class HttpFD(FileDownloader):
ctx.start_time = time.time() ctx.start_time = time.time()
ctx.chunk_size = None ctx.chunk_size = None
if self.params.get('continuedl', True): if (self.params.get('continuedl', True)
and ctx.tmpfilename != '-'):
# Establish possible resume length # Establish possible resume length
if os.path.isfile(encodeFilename(ctx.tmpfilename)): if os.path.isfile(encodeFilename(ctx.tmpfilename)):
ctx.resume_len = os.path.getsize( ctx.resume_len = os.path.getsize(
@ -221,11 +229,12 @@ class HttpFD(FileDownloader):
before = start # start measuring before = start # start measuring
def retry(e): def retry(e):
to_stdout = ctx.tmpfilename == '-' if ctx.tmpfilename == '-':
if not to_stdout: ctx.resume_len = byte_counter
else:
ctx.stream.close() ctx.stream.close()
ctx.stream = None ctx.stream = None
ctx.resume_len = byte_counter if to_stdout else os.path.getsize(encodeFilename(ctx.tmpfilename)) ctx.resume_len = os.path.getsize(encodeFilename(ctx.tmpfilename))
raise RetryDownload(e) raise RetryDownload(e)
while True: while True:

View File

@ -473,7 +473,7 @@ def parseOpts(overrideArguments=None):
downloader.add_option( downloader.add_option(
'--keep-fragments', '--keep-fragments',
action='store_true', dest='keep_fragments', default=False, action='store_true', dest='keep_fragments', default=False,
help='Keep downloaded fragments on disk after downloading is finished; fragments are erased by default') help='Keep downloaded fragments on disk after downloading is finished; fragments are not saved by default')
downloader.add_option( downloader.add_option(
'--buffer-size', '--buffer-size',
dest='buffersize', metavar='SIZE', default='1024', dest='buffersize', metavar='SIZE', default='1024',