1
0
mirror of https://github.com/l1ving/youtube-dl synced 2025-03-07 03:17:19 +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
available
--keep-fragments Keep downloaded fragments on disk after
downloading is finished; fragments are
erased by default
downloading is finished; fragments are not
saved by default
--buffer-size SIZE Size of download buffer (e.g. 1024 or 16K)
(default is 1024)
--no-resize-buffer Do not automatically adjust the buffer

View File

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

View File

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

View File

@ -473,7 +473,7 @@ def parseOpts(overrideArguments=None):
downloader.add_option(
'--keep-fragments',
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(
'--buffer-size',
dest='buffersize', metavar='SIZE', default='1024',