+
+def validate_options(opts):
+ def validate(cndn, name, value=None, msg=None):
+ if cndn:
+ return True
+ raise ValueError((msg or 'invalid {name} "{value}" given').format(name=name, value=value))
+
+ def validate_in(name, value, items, msg=None):
+ return validate(value is None or value in items, name, value, msg)
+
+ def validate_regex(name, value, regex):
+ return validate(value is None or re.match(regex, value), name, value)
+
+ def validate_positive(name, value, strict=False):
+ return validate(value is None or value > 0 or (not strict and value == 0),
+ name, value, '{name} "{value}" must be positive' + ('' if strict else ' or 0'))
+
+ def validate_minmax(min_val, max_val, min_name, max_name=None):
+ if max_val is None or min_val is None or max_val >= min_val:
+ return
+ if not max_name:
+ min_name, max_name = f'min {min_name}', f'max {min_name}'
+ raise ValueError(f'{max_name} "{max_val}" must be must be greater than or equal to {min_name} "{min_val}"')
+
+ # Usernames and passwords
+ validate(not opts.usenetrc or (opts.username is None and opts.password is None),
+ '.netrc', msg='using {name} conflicts with giving username/password')
+ validate(opts.password is None or opts.username is not None, 'account username', msg='{name} missing')
+ validate(opts.ap_password is None or opts.ap_username is not None,
+ 'TV Provider account username', msg='{name} missing')
+ validate_in('TV Provider', opts.ap_mso, MSO_INFO,
+ 'Unsupported {name} "{value}", use --ap-list-mso to get a list of supported TV Providers')
+
+ # Numbers
+ validate_positive('autonumber start', opts.autonumber_start)
+ validate_positive('autonumber size', opts.autonumber_size, True)
+ validate_positive('concurrent fragments', opts.concurrent_fragment_downloads, True)
+ validate_positive('playlist start', opts.playliststart, True)
+ if opts.playlistend != -1:
+ validate_minmax(opts.playliststart, opts.playlistend, 'playlist start', 'playlist end')
+
+ # Time ranges
+ validate_positive('subtitles sleep interval', opts.sleep_interval_subtitles)
+ validate_positive('requests sleep interval', opts.sleep_interval_requests)
+ validate_positive('sleep interval', opts.sleep_interval)
+ validate_positive('max sleep interval', opts.max_sleep_interval)
+ if opts.sleep_interval is None:
+ validate(
+ opts.max_sleep_interval is None, 'min sleep interval',
+ msg='{name} must be specified; use --min-sleep-interval')
+ elif opts.max_sleep_interval is None:
+ opts.max_sleep_interval = opts.sleep_interval
+ else:
+ validate_minmax(opts.sleep_interval, opts.max_sleep_interval, 'sleep interval')
+
+ if opts.wait_for_video is not None:
+ min_wait, max_wait, *_ = map(parse_duration, opts.wait_for_video.split('-', 1) + [None])
+ validate(min_wait is not None and not (max_wait is None and '-' in opts.wait_for_video),
+ 'time range to wait for video', opts.wait_for_video)
+ validate_minmax(min_wait, max_wait, 'time range to wait for video')
+ opts.wait_for_video = (min_wait, max_wait)
+
+ # Format sort
+ for f in opts.format_sort:
+ validate_regex('format sorting', f, InfoExtractor.FormatSort.regex)
+
+ # Postprocessor formats
+ validate_in('audio format', opts.audioformat, ['best'] + list(FFmpegExtractAudioPP.SUPPORTED_EXTS))
+ validate_in('subtitle format', opts.convertsubtitles, FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS)
+ validate_in('thumbnail format', opts.convertthumbnails, FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS)
+ if opts.recodevideo is not None:
+ opts.recodevideo = opts.recodevideo.replace(' ', '')
+ validate_regex('video recode format', opts.recodevideo, FFmpegVideoConvertorPP.FORMAT_RE)
+ if opts.remuxvideo is not None:
+ opts.remuxvideo = opts.remuxvideo.replace(' ', '')
+ validate_regex('video remux format', opts.remuxvideo, FFmpegVideoRemuxerPP.FORMAT_RE)
+ if opts.audioquality:
+ opts.audioquality = opts.audioquality.strip('k').strip('K')
+ # int_or_none prevents inf, nan
+ validate_positive('audio quality', int_or_none(float_or_none(opts.audioquality), default=0))
+
+ # Retries
+ def parse_retries(name, value):
+ if value is None:
+ return None
+ elif value in ('inf', 'infinite'):
+ return float('inf')
+ try:
+ return int(value)
+ except (TypeError, ValueError):
+ validate(False, f'{name} retry count', value)
+
+ opts.retries = parse_retries('download', opts.retries)
+ opts.fragment_retries = parse_retries('fragment', opts.fragment_retries)
+ opts.extractor_retries = parse_retries('extractor', opts.extractor_retries)
+ opts.file_access_retries = parse_retries('file access', opts.file_access_retries)
+
+ # Bytes
+ def parse_bytes(name, value):
+ if value is None:
+ return None
+ numeric_limit = FileDownloader.parse_bytes(value)
+ validate(numeric_limit is not None, 'rate limit', value)
+ return numeric_limit
+
+ opts.ratelimit = parse_bytes('rate limit', opts.ratelimit)
+ opts.throttledratelimit = parse_bytes('throttled rate limit', opts.throttledratelimit)
+ opts.min_filesize = parse_bytes('min filesize', opts.min_filesize)
+ opts.max_filesize = parse_bytes('max filesize', opts.max_filesize)
+ opts.buffersize = parse_bytes('buffer size', opts.buffersize)
+ opts.http_chunk_size = parse_bytes('http chunk size', opts.http_chunk_size)
+
+ # Output templates