Permalink
Browse files

converted video's duration added

  • Loading branch information...
jabbalaci committed Mar 21, 2013
1 parent e92b714 commit 8edd6990e5b84996b131eb0cbb68046b093ef92f
Showing with 75 additions and 20 deletions.
  1. +21 −19 movie2android.py
  2. +54 −1 utils.py
View
@@ -45,8 +45,8 @@
"""
__author__ = "Laszlo Szathmary (jabba.laci@gmail.com)"
-__version__ = "0.1.4"
-__date__ = "20130319"
+__version__ = "0.1.5"
+__date__ = "20130321"
__copyright__ = "Copyright (c) 2013 Laszlo Szathmary"
__license__ = "GPL"
@@ -55,10 +55,10 @@
import termcolor
import re
from texttable import Texttable
-import datetime
import utils
STATIC_BUILD, OWN_COMPILATION = range(2) # enum
+FAILED = "failed"
# select which version you have:
#VERSION = STATIC_BUILD
@@ -118,24 +118,25 @@ def check_switches(args):
## end of config ##########################################################
#############################################################################
-
-def frame(fname):
- size = len(fname)
- horizontal = '+'+'-'*(size+2)+'+'
- print termcolor.colored(horizontal, "green")
- print termcolor.colored('| '+fname+' |', "green")
- print termcolor.colored(horizontal, "green")
-
-
class Result(object):
+ """
+ A record to hold information about a converted video file.
+ """
def __init__(self, status=True):
self.status = status
# status (True: OK, False: conversion failed)
# file_name (str)
# file_size (int)
+ # elapsed_time (float)
pass
+def frame(fname):
+ size = len(fname)
+ horizontal = '+'+'-'*(size+2)+'+'
+ print termcolor.colored(horizontal, "green")
+ print termcolor.colored('| '+fname+' |', "green")
+ print termcolor.colored(horizontal, "green")
def resize(fname):
@@ -188,8 +189,8 @@ def main(args):
process each argument
"""
table = Texttable()
- table.set_cols_align(["r", "r", "r", "r"])
- rows = [["Number", "File Name", "File Size", "Elapsed Time (sec.)"]]
+ table.set_cols_align(["r", "r", "r", "r", "r"])
+ rows = [["Number", "File Name", "File Size", "Video Duration (H:MM:SS)", "Conversion Time"]]
total_time = 0.0
total_file_size = 0
@@ -198,20 +199,21 @@ def main(args):
with timer:
result = resize(arg)
#
+ result.elapsed_time = timer.elapsed_time()
rows.append([index,
result.file_name,
utils.sizeof_fmt(result.file_size),
- timer.elapsed_time() if result.status else "failed"])
+ utils.sec_to_hh_mm_ss(utils.get_video_length(result.file_name)),
+ "{0:.1f} sec.".format(result.elapsed_time) if result.status else FAILED])
#
- t = rows[-1][-1]
- if isinstance(t, float):
- total_time += t
+ if rows[-1][-1] != FAILED:
+ total_time += result.elapsed_time
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)
+ print 'Total time: {0} (H:MM:SS)'.format(utils.sec_to_hh_mm_ss(total_time))
#############################################################################
View
@@ -1,11 +1,15 @@
#!/usr/bin/env python
+import re
import platform as p
import uuid
import hashlib
import time
import shlex
-from subprocess import Popen, PIPE
+from subprocess import Popen, PIPE, STDOUT
+from datetime import timedelta
+
+video_info = "/usr/bin/mplayer '{0}' -ao null -vo null -frames 1 -identify"
class Timer(object):
@@ -74,8 +78,57 @@ def sizeof_fmt(num):
return "{0:.2f} {1}".format(num, x)
num /= 1024.0
+
+def get_simple_cmd_output(cmd, stderr=STDOUT):
+ """
+ Execute a simple external command and get its output.
+
+ The command contains no pipes. Error messages are
+ redirected to the standard output by default.
+ """
+ args = shlex.split(cmd)
+ return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0]
+
+
+def get_video_info(video_file):
+ """
+ Get info about a video.
+
+ The info is returned by mplayer. The result is a
+ dictionary whose keys start with 'ID_'.
+ """
+ cmd = video_info.format(video_file)
+ output = get_simple_cmd_output(cmd)
+ return dict(re.findall('(ID_.*)=(.*)', output))
+
+
+def get_video_length(video_file):
+ """
+ Get the length of a video in seconds.
+
+ The length is extracted with mplayer.
+ The return value is a real number.
+ """
+ info = get_video_info(video_file)
+ return float(info['ID_LENGTH'])
+
+
+def sec_to_hh_mm_ss(seconds, as_str=True):
+ """
+ Convert a time given in seconds to H:MM:SS format.
+
+ If as_str is True, the return value is a string.
+ If as_str is False, the return value is a tuple (H:MM:SS).
+ """
+ s = str(timedelta(seconds=int(round(seconds))))
+ if as_str:
+ return s
+ else:
+ return tuple([int(x) for x in s.split(':')])
+
#############################################################################
if __name__ == "__main__":
print get_fingerprint(True)
print get_short_fingerprint()
+ print get_video_length("/opt/shared.folder.vbox/video/Keynote_ Guido Van Rossum.mp4")

0 comments on commit 8edd699

Please sign in to comment.