2019-06-12 05:41:31 +00:00
|
|
|
'''
|
|
|
|
Find time, filesize, or bitrate, given two of the three.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
kbps.py --time 1:00:00 --size 2g
|
|
|
|
kbps.py --time 1:00:00 --kbps 4660
|
|
|
|
kbps.py --size 2g --kpbps 4660
|
|
|
|
'''
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from voussoirkit import bytestring
|
2020-07-16 21:15:35 +00:00
|
|
|
from voussoirkit import hms
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def kbps(time=None, size=None, kbps=None):
|
|
|
|
if [time, size, kbps].count(None) != 1:
|
2020-07-16 21:15:35 +00:00
|
|
|
raise ValueError('Incorrect number of unknowns.')
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if time is None:
|
|
|
|
size = bytestring.parsebytes(size)
|
|
|
|
kilobits = size / 128
|
|
|
|
time = kilobits / int(kbps)
|
2020-07-16 21:15:35 +00:00
|
|
|
return time
|
|
|
|
|
|
|
|
if size is None:
|
|
|
|
seconds = hms.hms_to_seconds(time)
|
|
|
|
kibs = int(kbps) / 8
|
|
|
|
size = kibs * 1024
|
|
|
|
size *= seconds
|
|
|
|
return size
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if kbps is None:
|
2020-07-16 21:15:35 +00:00
|
|
|
seconds = hms.hms_to_seconds(time)
|
2019-06-12 05:41:31 +00:00
|
|
|
size = bytestring.parsebytes(size)
|
|
|
|
kibs = size / 1024
|
|
|
|
kilobits = kibs * 8
|
|
|
|
kbps = kilobits / seconds
|
|
|
|
return kbps
|
|
|
|
|
2020-07-16 21:15:35 +00:00
|
|
|
def kbps_argparse(args):
|
|
|
|
result = kbps(time=args.time, size=args.size, kbps=args.kbps)
|
|
|
|
if args.time is None:
|
|
|
|
print(hms.seconds_to_hms(time))
|
|
|
|
if args.size is None:
|
|
|
|
print(bytestring.bytestring(size))
|
|
|
|
if args.kbps is None:
|
|
|
|
print('%d kbps' % round(result))
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument('-t', '--time', dest='time', default=None)
|
|
|
|
parser.add_argument('-s', '--size', dest='size', default=None)
|
|
|
|
parser.add_argument('-k', '--kbps', dest='kbps', default=None)
|
2020-07-16 21:15:35 +00:00
|
|
|
parser.set_defaults(func=kbps_argparse)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:18:50 +00:00
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-02-09 01:18:50 +00:00
|
|
|
raise SystemExit(main(sys.argv[1:]))
|