From aa1e24b4e3a9d99a72c8e440b0dafe2118a72816 Mon Sep 17 00:00:00 2001 From: Calin-Andrei Burloiu Date: Thu, 8 Dec 2011 13:53:45 +0200 Subject: [PATCH] CIS: torrent creation implemented --- cis/api/api_exceptions.py | 3 +++ cis/api/avhandling.py | 22 ++++++++++++++++++---- cis/api/base.py | 5 +++-- cis/cisd.py | 25 +++++++++++++++---------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cis/api/api_exceptions.py b/cis/api/api_exceptions.py index fa03c33..a8e4f78 100644 --- a/cis/api/api_exceptions.py +++ b/cis/api/api_exceptions.py @@ -17,5 +17,8 @@ class TranscodingException(Exception): class ThumbExtractionException(Exception): pass +class AVInfoException(Exception): + pass + class FileTransferException(Exception): pass diff --git a/cis/api/avhandling.py b/cis/api/avhandling.py index 85cf908..058c9b7 100644 --- a/cis/api/avhandling.py +++ b/cis/api/avhandling.py @@ -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) + '.') diff --git a/cis/api/base.py b/cis/api/base.py index cff692f..dce1efb 100644 --- a/cis/api/base.py +++ b/cis/api/base.py @@ -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 diff --git a/cis/cisd.py b/cis/cisd.py index e1acefd..3f0dc25 100755 --- a/cis/cisd.py +++ b/cis/cisd.py @@ -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 -- 2.20.1