def automask(self): """ Automatically generate a mask image for alpha compositing using given height, width and angle. TODO: Don't limit to rectangle masks only. """ # grok overlay size from media itself if not self.options['width'] or not self.options['height']: overlay_info = VideoInfo(self.options['overlay']) self.options['width'], self.options['height'] = overlay_info.size # generate alpha mask image if not self.options['maskfile']: mask_size = (self.options['width'], self.options['height']) self.alphamask = AlphaMask(mask_size) self.alphamask.rectangle() self.alphamask.rotate(self.options['angle']) self.options['maskfile'] = \ self.alphamask.save(delete=not self.options['debug'])
class VideoTwisterOverlay(VideoTwisterBase): # http://stackoverflow.com/questions/1732619/packaging-resources-with-setuptools-distribute/1732741#1732741 vlm_template = resource_stream('zt.vlc', 'overlay_videos.vlm.tpl').read() def mungle_options(self): """Convert some variables to their proper types""" self.options['angle'] = int(self.options['angle']) self.options['segfaults'] = int(self.options['segfaults']) self.options['timeout'] = float(self.options['timeout']) self.options['width'] = int(self.options['width']) self.options['height'] = int(self.options['height']) self.options['watch'] = bool(self.options['watch']) self.options['debug'] = bool(self.options['debug']) self.options['verbose'] = bool(self.options['verbose']) def start(self): """Select VLM template for doing overlay stuff and run VLC.""" self.automask() self.expandvlm() self.run_vlc_with_vlm() def automask(self): """ Automatically generate a mask image for alpha compositing using given height, width and angle. TODO: Don't limit to rectangle masks only. """ # grok overlay size from media itself if not self.options['width'] or not self.options['height']: overlay_info = VideoInfo(self.options['overlay']) self.options['width'], self.options['height'] = overlay_info.size # generate alpha mask image if not self.options['maskfile']: mask_size = (self.options['width'], self.options['height']) self.alphamask = AlphaMask(mask_size) self.alphamask.rectangle() self.alphamask.rotate(self.options['angle']) self.options['maskfile'] = \ self.alphamask.save(delete=not self.options['debug']) def expandvlm(self): """Compute some additional options to be passed to VLM template. Here's all the comfort: - auto generate alpha mask image - detect background (still image vs. video) """ # real size of alpha mask image maskwidth_real, maskheight_real = self.mask_size masksize_real = '' masksize_real += 'width=' + str(maskwidth_real) + ',' masksize_real += 'height=' + str(maskheight_real) + ',' self.options['masksize_real'] = masksize_real # input channel (still image vs. video) background = self.options['background'] if BackgroundType.byfilename(background) == BackgroundType.IMAGE: overlay_info = VideoInfo(self.options['overlay']) self.options['overlay_duration'] = overlay_info.duration * 1000 input_channel = """ # input: still image setup background input fake:// setup background option fake-file="${background}" setup background option fake-duration=${overlay_duration} """ else: input_channel = """ # input: video setup background input "${background}" """ self.options['input_channel'] = input_channel # whether to display the result in vlc self.options['display'] = \ self.options['watch'] and ',dst=display' or '' @property def mask_size(self): """Compute width and height of alphamask image. Returns: (width, height) -- image size as tuple """ image = Image.open(self.options['maskfile']) return image.size