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))))
def to_seconds(self, pos): """Convert `pos` to seconds.""" if aeidon.is_time(pos): return self.time_to_seconds(pos) if aeidon.is_frame(pos): return self.frame_to_seconds(pos) if aeidon.is_seconds(pos): return pos raise ValueError("Invalid type for pos: {!r}".format(type(pos)))
def is_earlier(self, x, y): """Return ``True`` if `x` is earlier than `y`.""" if aeidon.is_time(x): x = self.time_to_seconds(x) return self.is_earlier(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))))
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))))
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))))
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))))
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))))
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))))
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))))
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: {!r}".format(type(value)))
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))))
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))))
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))))
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))))
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))))
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"))
def to_frame_ensure(self, value, pos): assert aeidon.is_frame(value)
def time_to_frame_ensure(self, value, time): assert aeidon.is_frame(value)
def seconds_to_frame_ensure(self, value, seconds): assert aeidon.is_frame(value)
def _get_frame_transform_ensure(self, value, p1, p2): assert aeidon.is_frame(value[1])
def _get_second_point_ensure(self, value): assert aeidon.is_frame(value[1])
def _get_first_point_ensure(self, value): assert aeidon.is_frame(value[1])
def _get_amount_ensure(self, value): assert aeidon.is_frame(value)
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)))
def test_is_frame(self): assert aeidon.is_frame(aeidon.as_frame(13)) assert not aeidon.is_frame(aeidon.as_seconds(13)) assert not aeidon.is_frame(aeidon.as_time("12:34:56.789"))