diff options
| author | Ricardo Garcia <sarbalap+freshmeat@gmail.com> | 2008-07-24 09:47:07 +0200 | 
|---|---|---|
| committer | Ricardo Garcia <sarbalap+freshmeat@gmail.com> | 2010-10-31 11:23:31 +0100 | 
| commit | acd3d842987ee4c407093aaa4dedf4f301895adc (patch) | |
| tree | 68e1be53e7a58333ab088fd68a5d1b13a33cb834 | |
| parent | 7337efbfe48d87d34935b49ce64d4b434fa4cc4c (diff) | |
Add --rate-limit program option
| -rwxr-xr-x | youtube-dl | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/youtube-dl b/youtube-dl index 135d14809..4dea34376 100755 --- a/youtube-dl +++ b/youtube-dl @@ -78,6 +78,7 @@ class FileDownloader(object):  	format:		Video format code.  	outtmpl:	Template for output names.  	ignoreerrors:	Do not stop on download errors. +	ratelimit:	Download speed limit, in bytes/sec.  	"""  	_params = None @@ -149,6 +150,16 @@ class FileDownloader(object):  			return int(new_min)  		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):  		"""Sets parameters."""  		if type(params) != dict: @@ -193,6 +204,19 @@ class FileDownloader(object):  			raise DownloadError(message)  		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):  		"""Report destination filename."""  		self.to_stdout('[download] Destination: %s' % filename) @@ -296,6 +320,9 @@ class FileDownloader(object):  			stream.write(data_block)  			block_size = self.best_block_size(after - before, data_block_len) +			# Apply rate limit +			self.slow_down(start, byte_counter) +  		self.report_finish()  		if data_len is not None and str(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')  		parser.add_option('-i', '--ignore-errors',  				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()  		# Conflicting, missing and erroneous options @@ -590,6 +619,11 @@ if __name__ == '__main__':  			sys.exit('ERROR: using title conflicts with using literal title')  		if opts.username is not None and opts.password is None:  			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  		youtube_ie = YoutubeIE() @@ -609,6 +643,7 @@ if __name__ == '__main__':  				or (opts.useliteral and '%(title)s-%(id)s.%(ext)s')  				or '%(id)s.%(ext)s'),  			'ignoreerrors': opts.ignoreerrors, +			'ratelimit': opts.ratelimit,  			})  		fd.add_info_extractor(youtube_ie)  		retcode = fd.download(args) | 
