def select_quality(options, streams): high = 0 if isinstance(options.quality, str): try: quality = int(options.quality.split("-")[0]) if len(options.quality.split("-")) > 1: high = int(options.quality.split("-")[1]) except ValueError: raise error.UIException( "Requested quality is invalid. use a number or range lowerNumber-higherNumber" ) else: quality = options.quality try: optq = int(quality) except ValueError: raise error.UIException("Requested quality needs to be a number") try: optf = int(options.flexibleq) except ValueError: raise error.UIException("Flexible-quality needs to be a number") if optf == 0 and high: optf = (high - quality) / 2 optq = quality + (high - quality) / 2 # Extract protocol prio, in the form of "hls,hds,http,rtmp", # we want it as a list proto_prio = DEFAULT_PROTOCOL_PRIO if options.stream_prio: proto_prio = options.stream_prio.split(',') # Filter away any unwanted protocols, and prioritize # based on --stream-priority. streams = protocol_prio(streams, proto_prio) if len(streams) == 0: raise error.NoRequestedProtocols(requested=proto_prio, found=list( set([s.name() for s in streams]))) # Build a dict indexed by bitrate, where each value # is the stream with the highest priority protocol. stream_hash = {} for s in streams: if not s.bitrate in stream_hash: stream_hash[s.bitrate] = s avail = sorted(stream_hash.keys(), reverse=True) # wanted_lim is a two element tuple defines lower/upper bounds # (inclusive). By default, we want only the best for you # (literally!). wanted_lim = (avail[0], ) * 2 if optq: wanted_lim = (optq - optf, optq + optf) # wanted is the filtered list of available streams, having # a bandwidth within the wanted_lim range. wanted = [a for a in avail if a >= wanted_lim[0] and a <= wanted_lim[1]] # If none remains, the bitrate filtering was too tight. if len(wanted) == 0: data = sort_quality(streams) quality = ", ".join("%s (%s)" % (str(x), str(y)) for x, y in data) raise error.UIException("Can't find that quality. Try one of: %s (or " "try --flexible-quality)" % quality) return stream_hash[wanted[0]]
def select_quality(config, streams): high = 0 if isinstance(config.get("quality"), str): try: quality = int(config.get("quality").split("-")[0]) if len(config.get("quality").split("-")) > 1: high = int(config.get("quality").split("-")[1]) except ValueError: raise error.UIException( "Requested quality is invalid. use a number or range lowerNumber-higherNumber" ) else: quality = config.get("quality") try: optq = int(quality) except ValueError: raise error.UIException("Requested quality needs to be a number") try: optf = int(config.get("flexibleq")) except ValueError: raise error.UIException("Flexible-quality needs to be a number") if optf == 0 and high: optf = (high - quality) / 2 optq = quality + (high - quality) / 2 # Extract protocol prio, in the form of "hls,hds,http", # we want it as a list if config.get("stream_prio"): proto_prio = config.get("stream_prio").split(",") elif config.get("live") or streams[0].config.get("live"): proto_prio = LIVE_PROTOCOL_PRIO else: proto_prio = DEFAULT_PROTOCOL_PRIO # Filter away any unwanted protocols, and prioritize # based on --stream-priority. streams = protocol_prio(streams, proto_prio) if len(streams) == 0: raise error.NoRequestedProtocols(requested=proto_prio, found=list({s.name for s in streams})) # Build a dict indexed by bitrate, where each value # is the stream with the highest priority protocol. stream_hash = {} for s in streams: if s.bitrate not in stream_hash: stream_hash[s.bitrate] = s avail = sorted(stream_hash.keys(), reverse=True) # wanted_lim is a two element tuple defines lower/upper bounds # (inclusive). By default, we want only the best for you # (literally!). wanted_lim = (avail[0], ) * 2 if optq: wanted_lim = (optq - optf, optq + optf) # wanted is the filtered list of available streams, having # a bandwidth within the wanted_lim range. wanted = [a for a in avail if a >= wanted_lim[0] and a <= wanted_lim[1]] # If none remains, the bitrate filtering was too tight. if len(wanted) == 0: data = sort_quality(streams) quality = ", ".join("{} ({})".format(str(x), str(y)) for x, y in data) raise error.UIException("Can't find that quality. Try one of: %s (or " "try --flexible-quality)" % quality) http = HTTP(config) # Test if the wanted stream is available. If not try with the second best and so on. for w in wanted: res = http.get(stream_hash[w].url, cookies=stream_hash[w].kwargs.get("cookies", None)) if res is not None and res.status_code < 404: return stream_hash[w] raise error.UIException("Streams not available to download.")