Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Option --flexible-quality (-Q) #34

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+27 −11
Split
@@ -42,6 +42,7 @@ def __init__(self):
self.live = False
self.silent = False
self.quality = None
+ self.flexibleq = None
self.hls = False
self.other = None
self.subtitle = False
@@ -105,8 +106,10 @@ def main():
help="Enable for live streams")
parser.add_option("-s", "--silent",
action="store_true", dest="silent", default=False)
- parser.add_option("-q", "--quality",
+ parser.add_option("-q", "--quality", type="int", default=0,
metavar="quality", help="Choose what format to download.\nIt will download the best format by default")
+ parser.add_option("-Q", "--flexible-quality", type="int", default=0,
+ metavar="amount", dest="flexibleq", help="Allow given quality (as above) to differ by an amount.")
parser.add_option("-H", "--hls",
action="store_true", dest="hls", default=False)
parser.add_option("-S", "--subtitle",
@@ -122,5 +125,9 @@ def main():
setup_log(options.silent)
+ if options.flexibleq and not options.quality:
+ log.error("flexible-quality requires a quality")
+ sys.exit(4)
+
url = args[0]
get_media(url, options)
View
@@ -177,19 +177,28 @@ def subtitle_wsrt(options, data):
fd.close()
def select_quality(options, streams):
- sort = sorted(streams.keys(), key=int)
+ available = sorted(streams.keys(), key=int)
- if options.quality:
- quality = options.quality
+ optq = options.quality
+ if optq:
+ optf = options.flexibleq
+ if not optf:
+ wanted = [optq]
+ else:
+ wanted = range(optq-optf, optq+optf+1)
else:
- quality = sort.pop()
+ wanted = [available[-1]]
- try:
- selected = streams[int(quality)]
- except (KeyError, ValueError):
- log.error("Can't find that quality. (Try one of: %s)",
- ", ".join(map(str, sort)))
+ selected = None
+ for q in available:
+ if q in wanted:
+ selected = q
+ break
+
+ if not selected:
+ log.error("Can't find that quality. Try one of: %s (or try --flexible-quality)",
+ ", ".join(map(str, available)))
sys.exit(4)
- return selected
+ return streams[selected]