Example #1
0
 def to_time(self, pos):
     """Convert `pos` to time."""
     if aeidon.is_time(pos):
         return pos
     if aeidon.is_frame(pos):
         return self.frame_to_time(pos)
     if aeidon.is_seconds(pos):
         return self.seconds_to_time(pos)
     raise ValueError("Invalid type for pos: {}"
                      .format(repr(type(pos))))
Example #2
0
 def is_later(self, x, y):
     """Return ``True`` if `x` is later than `y`."""
     if aeidon.is_time(x):
         x = self.time_to_seconds(x)
         return self.is_later(x, y)
     if aeidon.is_frame(x):
         return (x > self.to_frame(y))
     if aeidon.is_seconds(x):
         return (x > self.to_seconds(y))
     raise ValueError("Invalid type for x: {}"
                      .format(repr(type(x))))
Example #3
0
 def add(self, x, y):
     """Add position `y` to `x`."""
     if aeidon.is_time(x):
         x = self.to_seconds(x)
         y = self.to_seconds(y)
         return self.seconds_to_time(x + y)
     if aeidon.is_frame(x):
         return x + self.to_frame(y)
     if aeidon.is_seconds(x):
         return x + self.to_seconds(y)
     raise ValueError("Invalid type for x: {}"
                      .format(repr(type(x))))
Example #4
0
 def get_middle(self, x, y):
     """Return time, frame or seconds halfway between `x` and `y`."""
     if aeidon.is_time(x):
         x = self.time_to_seconds(x)
         y = self.to_seconds(y)
         return self.seconds_to_time((x+y)/2)
     if aeidon.is_frame(x):
         y = self.to_frame(y)
         return aeidon.as_frame(round((x+y)/2, 0))
     if aeidon.is_seconds(x):
         y = self.to_seconds(y)
         return aeidon.as_seconds(((x+y)/2))
     raise ValueError("Invalid type for x: {}"
                      .format(repr(type(x))))
Example #5
0
 def _convert_position(self, value):
     """Return `value` of position in correct mode."""
     if aeidon.is_time(value):
         if self._mode == aeidon.modes.TIME:
             return value
         if self._mode == aeidon.modes.FRAME:
             return self.calc.time_to_frame(value)
     if aeidon.is_frame(value):
         if self._mode == aeidon.modes.TIME:
             return self.calc.frame_to_time(value)
         if self._mode == aeidon.modes.FRAME:
             return value
     if aeidon.is_seconds(value):
         if self._mode == aeidon.modes.TIME:
             return self.calc.seconds_to_time(value)
         if self._mode == aeidon.modes.FRAME:
             return self.calc.seconds_to_frame(value)
     raise ValueError("Invalid type for value: {}"
                      .format(repr(type(value))))
Example #6
0
    def round(self, pos, ndigits):
        """
        Round `pos` to given precision in decimal digits.

        `ndigits` may be negative. For frames zero will be used
        if given `ndigits` is greater than zero.
        """
        if aeidon.is_time(pos):
            pos = self.time_to_seconds(pos)
            pos = round(pos, ndigits)
            return self.seconds_to_time(pos)
        if aeidon.is_frame(pos):
            ndigits = min(0, ndigits)
            pos = round(pos, ndigits)
            return aeidon.as_frame(pos)
        if aeidon.is_seconds(pos):
            pos = round(pos, ndigits)
            return aeidon.as_seconds(pos)
        raise ValueError("Invalid type for pos: {}"
                         .format(repr(type(pos))))
Example #7
0
    def get_middle(self, x, y):
        """
        Return time, frame or seconds halfway between `x` and `y`.

        >>> calc = aeidon.Calculator()
        >>> calc.get_middle(0, 100)
        50
        >>> calc.get_middle("00:00:00.000", "00:00:10.000")
        '00:00:05.000'
        """
        if aeidon.is_time(x):
            x = self.time_to_seconds(x)
            y = self.to_seconds(y)
            return self.seconds_to_time((x+y)/2)
        if aeidon.is_frame(x):
            y = self.to_frame(y)
            return aeidon.as_frame(round((x+y)/2, 0))
        if aeidon.is_seconds(x):
            y = self.to_seconds(y)
            return aeidon.as_seconds(((x+y)/2))
        raise ValueError("Invalid type for x: {}"
                         .format(repr(type(x))))
Example #8
0
    def transform_positions(self, indices, p1, p2, register=-1):
        """
        Change positions by a linear two-point correction.

        `indices` can be ``None`` to process all subtitles. `p1` and `p2`
        should be tuples of index, position.
        """
        if aeidon.is_time(p1[1]):
            method = self._get_time_transform
        if aeidon.is_frame(p1[1]):
            method = self._get_frame_transform
        if aeidon.is_seconds(p1[1]):
            method = self._get_seconds_transform
        coefficient, constant = method(p1, p2)
        new_subtitles = []
        indices = indices or self.get_all_indices()
        for index in indices:
            subtitle = self.subtitles[index].copy()
            subtitle.scale_positions(coefficient)
            subtitle.shift_positions(constant)
            new_subtitles.append(subtitle)
        self.replace_positions(indices, new_subtitles, register=register)
        self.set_action_description(register, _("Transforming positions"))
Example #9
0
 def to_time_ensure(self, value, pos):
     assert aeidon.is_time(value)
Example #10
0
 def time_to_seconds_require(self, time):
     assert aeidon.is_time(time)
Example #11
0
 def time_to_frame_require(self, time):
     assert aeidon.is_time(time)
Example #12
0
 def seconds_to_time_ensure(self, value, seconds):
     assert aeidon.is_time(value)
Example #13
0
 def _get_transform(self, p1, p2):
     """Return a formula for linear correction of positions."""
     if aeidon.is_time(p1[1]): return self._get_time_transform(p1, p2)
     if aeidon.is_frame(p1[1]): return self._get_frame_transform(p1, p2)
     if aeidon.is_seconds(p1[1]): return self._get_seconds_transform(p1, p2)
     raise ValueError("Bad position argument: {}".format(repr(p1)))
Example #14
0
 def _get_first_point_ensure(self, value):
     assert aeidon.is_time(value[1])
Example #15
0
 def frame_to_time_ensure(self, value, frame):
     assert aeidon.is_time(value)
Example #16
0
 def _get_second_point_ensure(self, value):
     assert aeidon.is_time(value[1])
Example #17
0
 def test_is_time(self):
     assert aeidon.is_time(aeidon.as_time("12:34:56.789"))
     assert not aeidon.is_time(aeidon.as_frame(13))
     assert not aeidon.is_time(aeidon.as_seconds(13))