Permalink
Browse files

end table extended

  • Loading branch information...
1 parent 2c6acbc commit e92b7145b2f8ebee2106ac81c022de4332b5b62b @jabbalaci committed Mar 19, 2013
Showing with 46 additions and 15 deletions.
  1. +0 −2 TODO
  2. +36 −12 movie2android.py
  3. +10 −1 utils.py
View
2 TODO
@@ -1,2 +0,0 @@
-* Keep the original aspect ratio but make sure that the
- resized resolution respects the values set in the config part.
View
48 movie2android.py
@@ -45,8 +45,8 @@
"""
__author__ = "Laszlo Szathmary (jabba.laci@gmail.com)"
-__version__ = "0.1.3"
-__date__ = "20130318"
+__version__ = "0.1.4"
+__date__ = "20130319"
__copyright__ = "Copyright (c) 2013 Laszlo Szathmary"
__license__ = "GPL"
@@ -127,13 +127,24 @@ def frame(fname):
print termcolor.colored(horizontal, "green")
+class Result(object):
+ def __init__(self, status=True):
+ self.status = status
+ # status (True: OK, False: conversion failed)
+ # file_name (str)
+ # file_size (int)
+ pass
+
+
+
+
def resize(fname):
"""
Resize the current video file with ffmpeg.
"""
if not os.path.isfile(fname):
print termcolor.colored("Warning: the file {0} doesn't exist!".format(fname), "red")
- return False
+ return Result(False)
# else
fileBaseName = os.path.splitext(fname)[0]
@@ -142,52 +153,65 @@ def resize(fname):
output = "{0}-resized.mp4".format(fileBaseName)
if os.path.isfile(output):
print termcolor.colored('Warning: the file {0} exists!'.format(output), "red")
- return False
+ return Result(False)
+
# else
+ result = Result()
+
cmd = command % {'input': fname, 'output': output, 'audio_codec': config['audio_codec']}
+ result.file_name = output
print termcolor.colored(cmd, "green")
frame(fname)
exit_code = utils.call_and_get_exit_code(cmd)
if exit_code == 0:
print termcolor.colored("Success!", "green")
- return True
+ result.file_size = os.path.getsize(result.file_name)
+ return result
else:
print termcolor.colored(audio_codec_problem, "red")
os.unlink(output)
cmd = command % {'input': fname, 'output': output, 'audio_codec': config['audio_codec_failsafe']}
+ result.file_name = output
print termcolor.colored(cmd, "green")
frame(fname)
exit_code = utils.call_and_get_exit_code(cmd)
if exit_code == 0:
print termcolor.colored("Success!", "green")
- return True
+ result.file_size = os.path.getsize(result.file_name)
+ return result
else:
- return False
+ return Result(False)
def main(args):
"""
process each argument
"""
table = Texttable()
- table.set_cols_align(["r", "r", "r"])
- rows = [["Number", "File Name", "Elapsed Time (sec.)"]]
+ table.set_cols_align(["r", "r", "r", "r"])
+ rows = [["Number", "File Name", "File Size", "Elapsed Time (sec.)"]]
total_time = 0.0
+ total_file_size = 0
- for arg in args:
+ for index, arg in enumerate(args, start=1):
timer = utils.Timer()
with timer:
- status = resize(arg)
+ result = resize(arg)
#
- rows.append([len(rows), arg, timer.elapsed_time() if status else "failed"])
+ rows.append([index,
+ result.file_name,
+ utils.sizeof_fmt(result.file_size),
+ timer.elapsed_time() if result.status else "failed"])
#
t = rows[-1][-1]
if isinstance(t, float):
total_time += t
+ total_file_size += result.file_size
table.add_rows(rows)
print table.draw()
print 'Total time: {0} (H:MM:SS)'.format(str(datetime.timedelta(seconds=int(round(total_time)))))
+ print 'Total file size:', utils.sizeof_fmt(total_file_size)
#############################################################################
View
11 utils.py
@@ -9,7 +9,6 @@
class Timer(object):
-
def __enter__(self):
self.__start = time.time()
@@ -65,6 +64,16 @@ def call_and_get_exit_code(cmd):
process.communicate()
return process.wait()
+
+def sizeof_fmt(num):
+ """
+ Convert file size to human readable format.
+ """
+ for x in ['bytes','KB','MB','GB','TB']:
+ if num < 1024.0:
+ return "{0:.2f} {1}".format(num, x)
+ num /= 1024.0
+
#############################################################################
if __name__ == "__main__":

0 comments on commit e92b714

Please sign in to comment.