Top

time_code module

class TimeCode(object):
    '''
    This class provides a time code for a video editing system. Each video
    consists of frames (single images). There are a given number of frames in a
    second and of course 60 seconds in a minute.
    TimeCode is immutable.

    @author: oseppala, santtu
    '''

    # Constant values
    FRAMES_PER_SECOND = 50
    SECONDS_PER_MINUTE = 60

    def __init__(self, minutes, seconds, frames):
        '''
        Constructs a TimeCode instance with the given parameters.

        @param minutes: minutes of this TimeCode object.

        @param seconds: seconds of this TimeCode object.

        @param frames: frames of this TimeCode object.
        '''
        self.minutes = minutes
        self.seconds = seconds
        self.frames = frames


    def get_minutes(self):
        '''
        Returns the minutes field of this TimeCode instance.

        @return: minutes
        '''
        return self.minutes


    def get_seconds(self):
        '''
        Returns the seconds field of this TimeCode instance.

        @return: seconds
        '''
        return self.seconds
    

    def get_frames(self):
        '''
        Returns the frames field of this TimeCode instance.

        @return: frames
        '''
        return self.frames

    def inclusive_after(self, event):
        ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __ge__-method'''
        return self >= event    

    def __ge__(self, event):
        '''
        Tests if this timecode is after or equal to the timecode given as the
        parameter.

        Using this method overwrites >=-operator, so you can compare a >= b.

        @param event: the timecode being compared to

        @return: True if and only if this timecode is after or equal to the
                parameter timecode.
        '''

        if self.minutes < event.minutes:
            return False
        if self.minutes > event.minutes:
            return True
        if self.seconds < event.seconds:
            return False
        if self.seconds > event.seconds:
            return True
        if self.frames < event.frames:
            return False
        return True

    def inclusive_before(self, event):
        ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __le__-method'''
        return self <= event    

    def __le__(self, event):
        '''
        Tests if this timecode is before or equal to the timecode given as the
        parameter.

        @param event: the timecode being compared to

        @return: True if and only if this timecode is before or equal to the
                parameter timecode.
        '''

        if self.minutes > event.minutes:
            return False
        if self.minutes < event.minutes:
            return True
        if self.seconds > event.seconds:
            return False
        if self.seconds < event.seconds:
            return True
        if self.frames > event.frames:
            return False
        return True
    

    def inclusive_between(self, event1, event2):
        '''
        Tests if this timecode is between (inclusive) the timecodes given as
        parameters.

        @param event1: the begin timecode being compared to

        @param event2: the end timecode being compared to

        @return: True if and only if this timecode is between (inclusive) the given timecodes.
        '''

        return event1 <= self <= event2
    

    def previous_frame(self):
        '''
        Returns a new TimeCode instance which is one frame later from this timecode.

        @return: a new timecode object.
        '''
        return self.transpose_frames(-1)
    

    def next_frame(self):
        '''
        Returns a new TimeCode instance which is one frame before from this timecode.

        @return: a new timecode object.
        '''
        return self.transpose_frames(1)
    

    def to_frames(self):
        '''
        Converts the minutes, seconds and frames to just frames. 

        @return: a frames equivalent to this timecode.
        '''
        return self.frames + TimeCode.FRAMES_PER_SECOND * self.seconds +\
                 TimeCode.SECONDS_PER_MINUTE * TimeCode.FRAMES_PER_SECOND * self.minutes
    

    def calculate_frame_distance(self, time):
        '''
        Calculates the distance in frames between two timecodes.

        @param time: The timecode to which the distance is calculated.

        @return: The difference between the timecode in frames.
        '''
        return time.to_frames() - self.to_frames()
    

    def transpose_frames(self, offset):
        '''
        Returns a new TimeCode instance at the given offset in frames.

        @param offset: offset in frames from this timecode.

        @return: the new timecode object.
        '''
        total_frames = self.to_frames() + offset

        new_frames = total_frames % TimeCode.FRAMES_PER_SECOND
        seconds_left = total_frames // TimeCode.FRAMES_PER_SECOND

        new_seconds = seconds_left % TimeCode.SECONDS_PER_MINUTE
        new_minutes = seconds_left // TimeCode.SECONDS_PER_MINUTE

        return TimeCode(new_minutes, new_seconds, new_frames)
    

    def equals(self, other):
        '''
        Compares two timecode objects for equality.

        Method for lecasy support only, consider using overrided ==-operator instead!

        @return: True if and only if all fields match.
        '''
        return self == other

    def __eq__(self, other):
        '''
        Compares two timecode objects for equality.

        Calling it __eq__ allows us to overrite the ==-operator.
        E.g. now a == b would call this method, if a and b are timecodes.

        @return: True if and only if all fields match.
        '''
        if isinstance(other, TimeCode):
            return other.minutes == self.minutes and\
                     other.seconds == self.seconds and\
                     other.frames == self.frames
        return False

    def __ne__(self, other):
        return not self == other

    def __str__(self):
        '''
        Returns a String representation of this TimeCode-Object.

        @return: a String in the form [MM:SS:FF] (field widths may vary).
        '''
        return "[{0:d}:{1:d}:{2:d}]".format(int(self.minutes), int(self.seconds), int(self.frames))

Classes

class TimeCode

This class provides a time code for a video editing system. Each video consists of frames (single images). There are a given number of frames in a second and of course 60 seconds in a minute. TimeCode is immutable.

@author: oseppala, santtu

class TimeCode(object):
    '''
    This class provides a time code for a video editing system. Each video
    consists of frames (single images). There are a given number of frames in a
    second and of course 60 seconds in a minute.
    TimeCode is immutable.

    @author: oseppala, santtu
    '''

    # Constant values
    FRAMES_PER_SECOND = 50
    SECONDS_PER_MINUTE = 60

    def __init__(self, minutes, seconds, frames):
        '''
        Constructs a TimeCode instance with the given parameters.

        @param minutes: minutes of this TimeCode object.

        @param seconds: seconds of this TimeCode object.

        @param frames: frames of this TimeCode object.
        '''
        self.minutes = minutes
        self.seconds = seconds
        self.frames = frames


    def get_minutes(self):
        '''
        Returns the minutes field of this TimeCode instance.

        @return: minutes
        '''
        return self.minutes


    def get_seconds(self):
        '''
        Returns the seconds field of this TimeCode instance.

        @return: seconds
        '''
        return self.seconds
    

    def get_frames(self):
        '''
        Returns the frames field of this TimeCode instance.

        @return: frames
        '''
        return self.frames

    def inclusive_after(self, event):
        ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __ge__-method'''
        return self >= event    

    def __ge__(self, event):
        '''
        Tests if this timecode is after or equal to the timecode given as the
        parameter.

        Using this method overwrites >=-operator, so you can compare a >= b.

        @param event: the timecode being compared to

        @return: True if and only if this timecode is after or equal to the
                parameter timecode.
        '''

        if self.minutes < event.minutes:
            return False
        if self.minutes > event.minutes:
            return True
        if self.seconds < event.seconds:
            return False
        if self.seconds > event.seconds:
            return True
        if self.frames < event.frames:
            return False
        return True

    def inclusive_before(self, event):
        ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __le__-method'''
        return self <= event    

    def __le__(self, event):
        '''
        Tests if this timecode is before or equal to the timecode given as the
        parameter.

        @param event: the timecode being compared to

        @return: True if and only if this timecode is before or equal to the
                parameter timecode.
        '''

        if self.minutes > event.minutes:
            return False
        if self.minutes < event.minutes:
            return True
        if self.seconds > event.seconds:
            return False
        if self.seconds < event.seconds:
            return True
        if self.frames > event.frames:
            return False
        return True
    

    def inclusive_between(self, event1, event2):
        '''
        Tests if this timecode is between (inclusive) the timecodes given as
        parameters.

        @param event1: the begin timecode being compared to

        @param event2: the end timecode being compared to

        @return: True if and only if this timecode is between (inclusive) the given timecodes.
        '''

        return event1 <= self <= event2
    

    def previous_frame(self):
        '''
        Returns a new TimeCode instance which is one frame later from this timecode.

        @return: a new timecode object.
        '''
        return self.transpose_frames(-1)
    

    def next_frame(self):
        '''
        Returns a new TimeCode instance which is one frame before from this timecode.

        @return: a new timecode object.
        '''
        return self.transpose_frames(1)
    

    def to_frames(self):
        '''
        Converts the minutes, seconds and frames to just frames. 

        @return: a frames equivalent to this timecode.
        '''
        return self.frames + TimeCode.FRAMES_PER_SECOND * self.seconds +\
                 TimeCode.SECONDS_PER_MINUTE * TimeCode.FRAMES_PER_SECOND * self.minutes
    

    def calculate_frame_distance(self, time):
        '''
        Calculates the distance in frames between two timecodes.

        @param time: The timecode to which the distance is calculated.

        @return: The difference between the timecode in frames.
        '''
        return time.to_frames() - self.to_frames()
    

    def transpose_frames(self, offset):
        '''
        Returns a new TimeCode instance at the given offset in frames.

        @param offset: offset in frames from this timecode.

        @return: the new timecode object.
        '''
        total_frames = self.to_frames() + offset

        new_frames = total_frames % TimeCode.FRAMES_PER_SECOND
        seconds_left = total_frames // TimeCode.FRAMES_PER_SECOND

        new_seconds = seconds_left % TimeCode.SECONDS_PER_MINUTE
        new_minutes = seconds_left // TimeCode.SECONDS_PER_MINUTE

        return TimeCode(new_minutes, new_seconds, new_frames)
    

    def equals(self, other):
        '''
        Compares two timecode objects for equality.

        Method for lecasy support only, consider using overrided ==-operator instead!

        @return: True if and only if all fields match.
        '''
        return self == other

    def __eq__(self, other):
        '''
        Compares two timecode objects for equality.

        Calling it __eq__ allows us to overrite the ==-operator.
        E.g. now a == b would call this method, if a and b are timecodes.

        @return: True if and only if all fields match.
        '''
        if isinstance(other, TimeCode):
            return other.minutes == self.minutes and\
                     other.seconds == self.seconds and\
                     other.frames == self.frames
        return False

    def __ne__(self, other):
        return not self == other

    def __str__(self):
        '''
        Returns a String representation of this TimeCode-Object.

        @return: a String in the form [MM:SS:FF] (field widths may vary).
        '''
        return "[{0:d}:{1:d}:{2:d}]".format(int(self.minutes), int(self.seconds), int(self.frames))

Ancestors (in MRO)

Class variables

var FRAMES_PER_SECOND

var SECONDS_PER_MINUTE

Instance variables

var frames

var minutes

var seconds

Methods

def __init__(

self, minutes, seconds, frames)

Constructs a TimeCode instance with the given parameters.

@param minutes: minutes of this TimeCode object.

@param seconds: seconds of this TimeCode object.

@param frames: frames of this TimeCode object.

def __init__(self, minutes, seconds, frames):
    '''
    Constructs a TimeCode instance with the given parameters.
    @param minutes: minutes of this TimeCode object.
    @param seconds: seconds of this TimeCode object.
    @param frames: frames of this TimeCode object.
    '''
    self.minutes = minutes
    self.seconds = seconds
    self.frames = frames

def calculate_frame_distance(

self, time)

Calculates the distance in frames between two timecodes.

@param time: The timecode to which the distance is calculated.

@return: The difference between the timecode in frames.

def calculate_frame_distance(self, time):
    '''
    Calculates the distance in frames between two timecodes.
    @param time: The timecode to which the distance is calculated.
    @return: The difference between the timecode in frames.
    '''
    return time.to_frames() - self.to_frames()

def equals(

self, other)

Compares two timecode objects for equality.

Method for lecasy support only, consider using overrided ==-operator instead!

@return: True if and only if all fields match.

def equals(self, other):
    '''
    Compares two timecode objects for equality.
    Method for lecasy support only, consider using overrided ==-operator instead!
    @return: True if and only if all fields match.
    '''
    return self == other

def get_frames(

self)

Returns the frames field of this TimeCode instance.

@return: frames

def get_frames(self):
    '''
    Returns the frames field of this TimeCode instance.
    @return: frames
    '''
    return self.frames

def get_minutes(

self)

Returns the minutes field of this TimeCode instance.

@return: minutes

def get_minutes(self):
    '''
    Returns the minutes field of this TimeCode instance.
    @return: minutes
    '''
    return self.minutes

def get_seconds(

self)

Returns the seconds field of this TimeCode instance.

@return: seconds

def get_seconds(self):
    '''
    Returns the seconds field of this TimeCode instance.
    @return: seconds
    '''
    return self.seconds

def inclusive_after(

self, event)

Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see ge-method

def inclusive_after(self, event):
    ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __ge__-method'''
    return self >= event    

def inclusive_before(

self, event)

Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see le-method

def inclusive_before(self, event):
    ''' Tests if this timecode is after or equal to the timecode given as the parameter. Legacy support, see __le__-method'''
    return self <= event    

def inclusive_between(

self, event1, event2)

Tests if this timecode is between (inclusive) the timecodes given as parameters.

@param event1: the begin timecode being compared to

@param event2: the end timecode being compared to

@return: True if and only if this timecode is between (inclusive) the given timecodes.

def inclusive_between(self, event1, event2):
    '''
    Tests if this timecode is between (inclusive) the timecodes given as
    parameters.
    @param event1: the begin timecode being compared to
    @param event2: the end timecode being compared to
    @return: True if and only if this timecode is between (inclusive) the given timecodes.
    '''
    return event1 <= self <= event2

def next_frame(

self)

Returns a new TimeCode instance which is one frame before from this timecode.

@return: a new timecode object.

def next_frame(self):
    '''
    Returns a new TimeCode instance which is one frame before from this timecode.
    @return: a new timecode object.
    '''
    return self.transpose_frames(1)

def previous_frame(

self)

Returns a new TimeCode instance which is one frame later from this timecode.

@return: a new timecode object.

def previous_frame(self):
    '''
    Returns a new TimeCode instance which is one frame later from this timecode.
    @return: a new timecode object.
    '''
    return self.transpose_frames(-1)

def to_frames(

self)

Converts the minutes, seconds and frames to just frames.

@return: a frames equivalent to this timecode.

def to_frames(self):
    '''
    Converts the minutes, seconds and frames to just frames. 
    @return: a frames equivalent to this timecode.
    '''
    return self.frames + TimeCode.FRAMES_PER_SECOND * self.seconds +\
             TimeCode.SECONDS_PER_MINUTE * TimeCode.FRAMES_PER_SECOND * self.minutes

def transpose_frames(

self, offset)

Returns a new TimeCode instance at the given offset in frames.

@param offset: offset in frames from this timecode.

@return: the new timecode object.

def transpose_frames(self, offset):
    '''
    Returns a new TimeCode instance at the given offset in frames.
    @param offset: offset in frames from this timecode.
    @return: the new timecode object.
    '''
    total_frames = self.to_frames() + offset
    new_frames = total_frames % TimeCode.FRAMES_PER_SECOND
    seconds_left = total_frames // TimeCode.FRAMES_PER_SECOND
    new_seconds = seconds_left % TimeCode.SECONDS_PER_MINUTE
    new_minutes = seconds_left // TimeCode.SECONDS_PER_MINUTE
    return TimeCode(new_minutes, new_seconds, new_frames)