CIS: torrent creation implemented
authorCalin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 8 Dec 2011 11:53:45 +0000 (13:53 +0200)
committerCalin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 8 Dec 2011 11:53:45 +0000 (13:53 +0200)
cis/api/api_exceptions.py
cis/api/avhandling.py
cis/api/base.py
cis/cisd.py

index fa03c33..a8e4f78 100644 (file)
@@ -17,5 +17,8 @@ class TranscodingException(Exception):
 class ThumbExtractionException(Exception):
     pass
 
+class AVInfoException(Exception):
+    pass
+
 class FileTransferException(Exception):
     pass
index 85cf908..058c9b7 100644 (file)
@@ -11,6 +11,7 @@ import api_exceptions
 import subprocess
 import re
 import os
+import math
 
 class FFmpegTranscoder(base.BaseTranscoder):
     """
@@ -149,7 +150,7 @@ class FFmpegAVInfo(base.BaseAVInfo):
     prog_bin = "ffprobe"
 
     @staticmethod
-    def get_video_duration(input_file):
+    def get_video_duration(input_file, formated=False):
         args = FFmpegAVInfo.prog_bin + ' -show_format "' \
                 + input_file + '"'
 
@@ -167,9 +168,22 @@ class FFmpegAVInfo(base.BaseAVInfo):
             # 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 api_exceptions.ThumbExtractionException( \
-                    'FFmpeg exited with code ' + str(exit_code) + '.')
+            raise api_exceptions.AVInfoException( \
+                    'ffprobe exited with code ' + str(exit_code) + '.')
index cff692f..dce1efb 100644 (file)
@@ -288,8 +288,9 @@ class BaseFileTransferer:
 
 class BaseAVInfo:
     @staticmethod
-    def get_video_duration(input_file):
+    def get_video_duration(input_file, formated=False):
         """
-        Returns the number of seconds of a video (int/float).
+        Returns the number of seconds of a video (int/float) if formated is
+        False and a string for duration formated as [HH:]:mm:ss otherwise.
         """
         pass
index e1acefd..3f0dc25 100755 (executable)
@@ -2,26 +2,31 @@
 
 import sys
 import config
+import os
 
-#
-# !! Imports required for create_torrent
-#
 from BaseLib.Core.API import *
-#
-#
-#
 
-def create_torrent(input_):
+def create_torrent(source):
+    """ Creates a torrent file for the video source file. """
+
+    if isinstance(source, unicode):
+        usource = source
+    else:
+        usource = source.decode(sys.getfilesystemencoding())
+
+    duration = config.AVINFO_CLASS.get_video_duration(source, True)
+
+    print config.BT_TRACKER, duration, source
+
     tdef = TorrentDef()
-    tdef.add_content(input_, config.AVINFO_CLASS.get_video_duration(input_))
+    tdef.add_content(usource, playtime=duration)
     tdef.set_tracker(config.BT_TRACKER)
 
     tdef.set_piece_length(32768)
 
     tdef.finalize()
-    tdef.save(input_ + ".tstream")
+    tdef.save(source + '.tstream')
 
-    print 'READY!', config.BT_TRACKER, config.AVINFO_CLASS.get_video_duration(input_)
 
 if __name__ == '__main__':
     pass