diff options
author | pukkandan <pukkandan.ytdlp@gmail.com> | 2021-12-23 06:44:42 +0530 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2021-12-23 06:49:16 +0530 |
commit | e0fd95737d1a3c4a2bfb470c5408a396c8545ca5 (patch) | |
tree | 8c47cd22bc2b96b4b70c97b2854cd84756fe6b5e /yt_dlp/utils.py | |
parent | 4ac5b94807e75dc750459b8db6aa95d3b5275a1b (diff) | |
download | hypervideo-pre-e0fd95737d1a3c4a2bfb470c5408a396c8545ca5.tar.lz hypervideo-pre-e0fd95737d1a3c4a2bfb470c5408a396c8545ca5.tar.xz hypervideo-pre-e0fd95737d1a3c4a2bfb470c5408a396c8545ca5.zip |
[outtmpl] Add alternate forms `F`, `D`
and improve `id` detection
F = sanitize as filename (# = restricted)
D = add Decimal suffixes
Closes #2085, 2081
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 2919324c6..b1929f4db 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2110,18 +2110,19 @@ def unsmuggle_url(smug_url, default=None): return url, data +def format_decimal_suffix(num, fmt='%d%s', *, factor=1000): + """ Formats numbers with decimal sufixes like K, M, etc """ + num, factor = float_or_none(num), float(factor) + if num is None: + return None + exponent = 0 if num == 0 else int(math.log(num, factor)) + suffix = ['', *'KMGTPEZY'][exponent] + converted = num / (factor ** exponent) + return fmt % (converted, suffix) + + def format_bytes(bytes): - if bytes is None: - return 'N/A' - if type(bytes) is str: - bytes = float(bytes) - if bytes == 0.0: - exponent = 0 - else: - exponent = int(math.log(bytes, 1024.0)) - suffix = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'][exponent] - converted = float(bytes) / float(1024 ** exponent) - return '%.2f%s' % (converted, suffix) + return format_decimal_suffix(bytes, '%.2f%siB', factor=1024) or 'N/A' def lookup_unit_table(unit_table, s): |