Example #1
0
    def __init__(self, file: str, *, time_boundaries: Opt[List[Tuple[(TIME_FORMAT, TIME_FORMAT)]]] = None,
                 **kwargs):
        """
        Parameters
        ----------
        file
            video file to sample from

        time_boundaries
            the set of time ranges to sample from in the video.
            For supported formats, see :data:`~mugen.constants.TIME_FORMAT`.
        """
        super().__init__(**kwargs)
        self.segment = VideoSegment(file)
        self.time_boundaries = time_boundaries if time_boundaries else []
Example #2
0
class VideoSource(Source):
    """
    A video source for sampling video segments
    """
    time_boundaries: List[Tuple[(TIME_FORMAT, TIME_FORMAT)]]

    def __init__(self, file: str, *, time_boundaries: Opt[List[Tuple[(TIME_FORMAT, TIME_FORMAT)]]] = None,
                 **kwargs):
        """
        Parameters
        ----------
        file
            video file to sample from

        time_boundaries
            the set of time ranges to sample from in the video.
            For supported formats, see :data:`~mugen.constants.TIME_FORMAT`.
        """
        super().__init__(**kwargs)
        self.segment = VideoSegment(file)
        self.time_boundaries = time_boundaries if time_boundaries else []

    def __repr__(self):
        return f"<{self.__class__.__name__}: {self.name}, duration: {self.segment.duration_time_code}, " \
               f"weight: {self.weight}>"

    @property
    def file(self):
        return self.segment.file

    @property
    def name(self):
        return self.segment.name

    def sample(self, duration: float) -> VideoSegment:
        """
        Randomly samples a video segment with the specified duration.

        Parameters
        ----------
        duration
            duration of the video segment to sample
        """
        if self.time_boundaries:
            # Select a random time boundary to sample from, weighted by duration
            time_ranges = [TimeRange(*boundary) for boundary in self.time_boundaries]
            time_ranges = [time_range for time_range in time_ranges if time_range.duration >= duration]
            total_duration = sum([time_range.duration for time_range in time_ranges])
            time_range_weights = [time_range.duration / total_duration for time_range in time_ranges]
            time_range_to_sample = time_ranges[choice(len(time_ranges), p=time_range_weights)]
        else:
            time_range_to_sample = TimeRange(0, self.segment.duration)

        start_time = random.uniform(time_range_to_sample.start, time_range_to_sample.end - duration)
        sampled_clip = self.segment.subclip(start_time, start_time + duration)

        return sampled_clip
Example #3
0
def video_segment_is_repeat(video_segment: VideoSegment, video_segments_used: List[VideoSegment]) -> bool:
    """
    Returns
    -------
    True if a video segment is a repeat of a video segment already used, False otherwise
    """
    for used_segment in video_segments_used:
        if video_segment.overlaps_segment(used_segment):
            return True

    return False
Example #4
0
def shinsekai_segment() -> VideoSegment:
    return VideoSegment(f'{DATA_PATH}/video/shinsekai.mp4')