Permalink
Browse files

Option --flexible-quality (-Q)

  • Loading branch information...
1 parent 80796c5 commit e69765943c3d14d248b9bd0fe2621cbdf099f959 @quite committed Mar 23, 2013
Showing with 27 additions and 11 deletions.
  1. +8 −1 lib/svtplay_dl/__init__.py
  2. +19 −10 lib/svtplay_dl/utils.py
@@ -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", default=0,
metavar="quality", help="Choose what format to download.\nIt will download the best format by default")
+ parser.add_option("-Q", "--flexible-quality", 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 = int(options.quality)
+ if optq:
+ optf = int(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]

0 comments on commit e697659

Please sign in to comment.