mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-11-03 21:48:24 +01:00
Add --rate-limit program option
This commit is contained in:
parent
7337efbfe4
commit
acd3d84298
35
youtube-dl
35
youtube-dl
@ -78,6 +78,7 @@ class FileDownloader(object):
|
|||||||
format: Video format code.
|
format: Video format code.
|
||||||
outtmpl: Template for output names.
|
outtmpl: Template for output names.
|
||||||
ignoreerrors: Do not stop on download errors.
|
ignoreerrors: Do not stop on download errors.
|
||||||
|
ratelimit: Download speed limit, in bytes/sec.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_params = None
|
_params = None
|
||||||
@ -149,6 +150,16 @@ class FileDownloader(object):
|
|||||||
return int(new_min)
|
return int(new_min)
|
||||||
return int(rate)
|
return int(rate)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_bytes(bytestr):
|
||||||
|
"""Parse a string indicating a byte quantity into a long integer."""
|
||||||
|
matchobj = re.match(r'(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$', bytestr)
|
||||||
|
if matchobj is None:
|
||||||
|
return None
|
||||||
|
number = float(matchobj.group(1))
|
||||||
|
multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
|
||||||
|
return long(round(number * multiplier))
|
||||||
|
|
||||||
def set_params(self, params):
|
def set_params(self, params):
|
||||||
"""Sets parameters."""
|
"""Sets parameters."""
|
||||||
if type(params) != dict:
|
if type(params) != dict:
|
||||||
@ -193,6 +204,19 @@ class FileDownloader(object):
|
|||||||
raise DownloadError(message)
|
raise DownloadError(message)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
def slow_down(self, start_time, byte_counter):
|
||||||
|
"""Sleep if the download speed is over the rate limit."""
|
||||||
|
rate_limit = self._params.get('ratelimit', None)
|
||||||
|
if rate_limit is None or byte_counter == 0:
|
||||||
|
return
|
||||||
|
now = time.time()
|
||||||
|
elapsed = now - start_time
|
||||||
|
if elapsed <= 0.0:
|
||||||
|
return
|
||||||
|
speed = float(byte_counter) / elapsed
|
||||||
|
if speed > rate_limit:
|
||||||
|
time.sleep((byte_counter - rate_limit * (now - start_time)) / rate_limit)
|
||||||
|
|
||||||
def report_destination(self, filename):
|
def report_destination(self, filename):
|
||||||
"""Report destination filename."""
|
"""Report destination filename."""
|
||||||
self.to_stdout('[download] Destination: %s' % filename)
|
self.to_stdout('[download] Destination: %s' % filename)
|
||||||
@ -296,6 +320,9 @@ class FileDownloader(object):
|
|||||||
stream.write(data_block)
|
stream.write(data_block)
|
||||||
block_size = self.best_block_size(after - before, data_block_len)
|
block_size = self.best_block_size(after - before, data_block_len)
|
||||||
|
|
||||||
|
# Apply rate limit
|
||||||
|
self.slow_down(start, byte_counter)
|
||||||
|
|
||||||
self.report_finish()
|
self.report_finish()
|
||||||
if data_len is not None and str(byte_counter) != data_len:
|
if data_len is not None and str(byte_counter) != data_len:
|
||||||
raise ValueError('Content too short: %s/%s bytes' % (byte_counter, data_len))
|
raise ValueError('Content too short: %s/%s bytes' % (byte_counter, data_len))
|
||||||
@ -575,6 +602,8 @@ if __name__ == '__main__':
|
|||||||
action='store_const', dest='format', help='alias for -f 17', const='17')
|
action='store_const', dest='format', help='alias for -f 17', const='17')
|
||||||
parser.add_option('-i', '--ignore-errors',
|
parser.add_option('-i', '--ignore-errors',
|
||||||
action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
|
action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
|
||||||
|
parser.add_option('-r', '--rate-limit',
|
||||||
|
dest='ratelimit', metavar='L', help='download rate limit (e.g. 50k or 44.6m)')
|
||||||
(opts, args) = parser.parse_args()
|
(opts, args) = parser.parse_args()
|
||||||
|
|
||||||
# Conflicting, missing and erroneous options
|
# Conflicting, missing and erroneous options
|
||||||
@ -590,6 +619,11 @@ if __name__ == '__main__':
|
|||||||
sys.exit('ERROR: using title conflicts with using literal title')
|
sys.exit('ERROR: using title conflicts with using literal title')
|
||||||
if opts.username is not None and opts.password is None:
|
if opts.username is not None and opts.password is None:
|
||||||
opts.password = getpass.getpass('Type account password and press return:')
|
opts.password = getpass.getpass('Type account password and press return:')
|
||||||
|
if opts.ratelimit is not None:
|
||||||
|
numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
|
||||||
|
if numeric_limit is None:
|
||||||
|
sys.exit('ERROR: invalid rate limit specified')
|
||||||
|
opts.ratelimit = numeric_limit
|
||||||
|
|
||||||
# Information extractors
|
# Information extractors
|
||||||
youtube_ie = YoutubeIE()
|
youtube_ie = YoutubeIE()
|
||||||
@ -609,6 +643,7 @@ if __name__ == '__main__':
|
|||||||
or (opts.useliteral and '%(title)s-%(id)s.%(ext)s')
|
or (opts.useliteral and '%(title)s-%(id)s.%(ext)s')
|
||||||
or '%(id)s.%(ext)s'),
|
or '%(id)s.%(ext)s'),
|
||||||
'ignoreerrors': opts.ignoreerrors,
|
'ignoreerrors': opts.ignoreerrors,
|
||||||
|
'ratelimit': opts.ratelimit,
|
||||||
})
|
})
|
||||||
fd.add_info_extractor(youtube_ie)
|
fd.add_info_extractor(youtube_ie)
|
||||||
retcode = fd.download(args)
|
retcode = fd.download(args)
|
||||||
|
Loading…
Reference in New Issue
Block a user