cis: bug fixes
[living-lab-site.git] / cis / api / avhandling.py
index a64f7f2..27c9a5d 100644 (file)
@@ -7,10 +7,11 @@ videos and thumbnail extraction from videos using FFmpeg CLI program.
 """
 
 import base
-import cis_exceptions
+import api_exceptions
 import subprocess
 import re
 import os
+import math
 
 class FFmpegTranscoder(base.BaseTranscoder):
     """
@@ -90,11 +91,13 @@ class FFmpegTranscoder(base.BaseTranscoder):
 
         exit_code = p.wait()
         if exit_code > 0:
-            raise cis_exceptions.TranscodingException( \
+            raise api_exceptions.TranscodingException( \
                     'FFmpeg exited with code ' + str(exit_code) + '.')
 
         log.close()
 
+        return self.output_file
+
 
 class FFmpegThumbExtractor(base.BaseThumbExtractor):
     """
@@ -102,7 +105,6 @@ class FFmpegThumbExtractor(base.BaseThumbExtractor):
     """
 
     prog_bin = "ffmpeg"
-    info_prog_bin = "ffprobe"
 
     log_file = 'log/FFmpegThumbExtractor.log'
 
@@ -131,38 +133,66 @@ class FFmpegThumbExtractor(base.BaseThumbExtractor):
 
         exit_code = p.wait()
         if exit_code > 0:
-            raise cis_exceptions.ThumbExtractionException( \
+            raise api_exceptions.ThumbExtractionException( \
                     'FFmpeg exited with code ' + str(exit_code) + '.')
 
         # FFmpeg bug: when no key frame is found from seek_pos to the
         # end of file an empty image file is created.
         if os.path.getsize(output_file) == 0L:
             os.unlink(output_file)
-            raise cis_exceptions.ThumbExtractionException( \
+            raise api_exceptions.ThumbExtractionException( \
                     'FFmpeg created an empty file.')
 
     def get_video_duration(self):
-        args = self.info_prog_bin + ' -show_format "' \
-                + self.input_file + '"'
+        return FFprobeAVInfo.get_video_duration(self.input_file)
+
+
+class FFprobeAVInfo(base.BaseAVInfo):
+    
+    prog_bin = "ffprobe"
+
+    log_file = 'log/FFprobeAVInfo.log'
+
+    @staticmethod
+    def get_video_duration(input_file, formated=False):
+        args = FFprobeAVInfo.prog_bin + ' -show_format "' \
+                + input_file + '"'
 
         # READ handler for process's output.
         p = subprocess.Popen(args, shell=True,
-                stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
+                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
         pipe = p.stdout
 
+        # WRITE handler for logging.
+        log = open(FFprobeAVInfo.log_file, 'w')
+        log.write(args + '\n')
+
         # Parse ffprobe's output.
         while True:
             line = pipe.readline()
             if len(line) == 0:
                 break
+            log.write(line)
             
             # Search for the line which contains duration information.
             m = re.match(r"duration=([\d\.]+)", line)
             if m is not None:
-                return float(m.group(1))
+                seconds = float(m.group(1))
+                if not formated:
+                    return seconds
+                else:
+                    seconds = math.floor(seconds)
+                    minutes = math.floor(seconds / 60)
+                    seconds = seconds % 60
+                    if minutes >= 60:
+                        hours = math.floor(minutes / 60)
+                        minutes = minutes % 60
+                        
+                        return "%02d:%02d:%02d" % (hours, minutes, seconds)
+                    else:
+                        return "%02d:%02d" % (minutes, seconds)
 
         exit_code = p.wait()
         if exit_code > 0:
-            raise cis_exceptions.ThumbExtractionException( \
-                    'FFmpeg exited with code ' + str(exit_code) + '.')
-
+            raise api_exceptions.AVInfoException( \
+                    'ffprobe exited with code ' + str(exit_code) + '.')