X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=cis%2Fapi%2Fbase.py;h=cff692fa6cfeb6ffb39e2d100763bdcc7f1521f8;hb=1ab12b6c509b6a6be95f79dc50089649a32872da;hp=96006f803ba0d83e7bbda2ccd3bc2425eb85ef91;hpb=42cb85273f6ef35a40c183f1184313bbc06afa05;p=living-lab-site.git diff --git a/cis/api/base.py b/cis/api/base.py index 96006f8..cff692f 100644 --- a/cis/api/base.py +++ b/cis/api/base.py @@ -4,8 +4,10 @@ Base classes for the external programs API. """ -import cis_exceptions +import api_exceptions import re +import cis_util +import random class BaseTranscoder: """ @@ -53,12 +55,7 @@ class BaseTranscoder: self.prog_bin = prog_bin if name is None: - if input_file.find('/') is not -1: - name = input_file[(input_file.rindex('/')+1):] - else: - name = input_file - if name.find('.') is not -1: - name = name[:name.rindex('.')] + name = cis_util.get_name(input_file) self.name = name @@ -134,7 +131,7 @@ class BaseTranscoder: """ Translates container API name into external program identifier.""" if not self.containers.has_key(name) or self.containers[name] is None: - raise cis_exceptions.NotImplementedException("Container " + name) + raise api_exceptions.NotImplementedException("Container " + name) return self.containers[name] @@ -155,7 +152,7 @@ class BaseTranscoder: """ Translates audio codec API name into external program identifier.""" if not self.a_codecs.has_key(name) or self.a_codecs[name] is None: - raise cis_exceptions.NotImplementedException("Audio Codec " + name) + raise api_exceptions.NotImplementedException("Audio Codec " + name) return self.a_codecs[name] @@ -163,6 +160,136 @@ class BaseTranscoder: """ Translates video codec API name into external program identifier.""" if not self.v_codecs.has_key(name) or self.v_codecs[name] is None: - raise cis_exceptions.NotImplementedException("Video Codec " + name) + raise api_exceptions.NotImplementedException("Video Codec " + name) return self.v_codecs[name] + + +class BaseThumbExtractor: + """ + Abstraction of the API class for the thumbnail extraction program. + + Thumbnail extracted are in JPEG format. + """ + + prog_bin = None + input_file = None + dest_path = '' + name = None + + def __init__(self, input_file, name=None, prog_bin=None): + self.input_file = input_file + if prog_bin is not None: + self.prog_bin = prog_bin + + if name is None: + name = cis_util.get_name(input_file) + + self.name = name + + def extract_thumb(self, seek_pos, resolution="120x90", index=0): + """ + Extracts a thumbnail from the video from a specified position + expressed in seconds (int/float). + + index: an index appended to the image name in order to avoid + overwriting. + """ + pass + + def extract_random_thumb(self, resolution="120x90", index=0): + """ + Extracts a thumbnail from the video from a random position. + """ + duration = self.get_video_duration() + seek_pos = random.random() * duration + self.extract_thumb(seek_pos, resolution, index) + + def extract_summary_thumbs(self, count, resolution="120x90"): + """ + Extracts a series of thumbnails from a video by taking several + snapshots. + + The snapshots are taken from equally spaced positions such that + `count` thumbs are extracted. + """ + duration = self.get_video_duration() + interval = duration / (count + 1) + + n_thumbs_extracted = 0 + seek_pos = interval + for index in range (0, count): + thumb_extracted = True + try: + self.extract_thumb(seek_pos, resolution, index) + except api_exceptions.ThumbExtractionException as e: + thumb_extracted = False + + if thumb_extracted: + n_thumbs_extracted += 1 + + seek_pos += interval + + return n_thumbs_extracted + + def get_output_file_name(self, index): + """ Returns the name required as output file name based on index. """ + output_file_name = self.dest_path + self.name \ + + '_t' + ("%02d" % index) + '.jpg' + return output_file_name + + def get_video_duration(self): + pass + + +class BaseFileTransferer: + """ + Ensures file transfer from the Web Server to the CIS (here). + + Several implementations can be done by extending this class for + file transfer protocol such as FTP, SCP, RSYNC, HTTP etc. + """ + + local_path = '' + remote_path = '' + + def __init__(self, local_path='', remote_path=''): + """ Initialize by setting local and remote paths for file transfer. """ + self.local_path = local_path + self.remote_path = remote_path + + def __del__(self): + self.close() + + def get(self, files): + """ + Transfers files locally from the Web Server. + + files: a list of file name strings + """ + pass + + def put(self, files): + """ + Transfers files from the Web Server locally. + + files: a list of file name strings + """ + pass + + def close(self): + """ + This method should be called when the instance is no longer required. + + Class's destructor calls this method. + """ + pass + + +class BaseAVInfo: + @staticmethod + def get_video_duration(input_file): + """ + Returns the number of seconds of a video (int/float). + """ + pass