X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Futils.py;h=9fd0ec8d5856cbee27534c0f4a02cc90b05f8389;hb=2f7ae819ac844f4052dcd4ef031fdae01daff3b8;hp=0e04e91a4202397b4a3a1c768f49827ac3a1a542;hpb=64f08d4ff2392135be07774f2d5371f111f21592;p=youtube-dl diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 0e04e91a4..9fd0ec8d5 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -1316,6 +1316,17 @@ def format_bytes(bytes): return '%.2f%s' % (converted, suffix) +def lookup_unit_table(unit_table, s): + units_re = '|'.join(re.escape(u) for u in unit_table) + m = re.match( + r'(?P[0-9]+(?:[,.][0-9]*)?)\s*(?P%s)' % units_re, s) + if not m: + return None + num_str = m.group('num').replace(',', '.') + mult = unit_table[m.group('unit')] + return int(float(num_str) * mult) + + def parse_filesize(s): if s is None: return None @@ -1359,15 +1370,28 @@ def parse_filesize(s): 'Yb': 1000 ** 8, } - units_re = '|'.join(re.escape(u) for u in _UNIT_TABLE) - m = re.match( - r'(?P[0-9]+(?:[,.][0-9]*)?)\s*(?P%s)' % units_re, s) - if not m: + return lookup_unit_table(_UNIT_TABLE, s) + + +def parse_count(s): + if s is None: return None - num_str = m.group('num').replace(',', '.') - mult = _UNIT_TABLE[m.group('unit')] - return int(float(num_str) * mult) + s = s.strip() + + if re.match(r'^[\d,.]+$', s): + return str_to_int(s) + + _UNIT_TABLE = { + 'k': 1000, + 'K': 1000, + 'm': 1000 ** 2, + 'M': 1000 ** 2, + 'kk': 1000 ** 2, + 'KK': 1000 ** 2, + } + + return lookup_unit_table(_UNIT_TABLE, s) def month_by_name(name): @@ -1893,22 +1917,6 @@ def mimetype2ext(mt): }.get(res, res) -def codec2ext(codec): - codec_type = codec.split('.')[0] - - # Leave the return value None for unknown values as codec_type - # is not a good fallback for file extensions - return { - 'avc1': 'mp4', - 'avc2': 'mp4', - 'avc3': 'mp4', - 'avc4': 'mp4', - 'mp4a': 'm4a', - 'vorbis': 'webm', - 'vp9': 'webm', - }.get(codec_type) - - def urlhandle_detect_ext(url_handle): try: url_handle.headers