def getChangeInFrames(self, edge, meta_i, meta_o, source, target, expectedType='video'): if meta_i is None or meta_o is None: result = get_meta_data_change_from_edge(edge, expectedType=expectedType) if result is not None: return result else: change = getValue(meta_i, 'duration', None) != getValue(meta_o, 'duration', None) or \ getValue(meta_i, 'nb_frames', None) != getValue(meta_o, 'nb_frames', None) or \ getValue(meta_i, 'sample_rate', None) != getValue(meta_o, 'sample_rate', None) or \ getValue(meta_i, 'avg_frame_rate', None) != getValue(meta_o, 'avg_frame_rate', None) or \ getValue(meta_i, 'duration_ts', None) != getValue(meta_o, 'duration_ts', None) if not change: return None maskSource = self.getMetaDataLocator( source).getMaskSetForEntireVideoForTuples( media_types=[expectedType]) maskTarget = self.getMetaDataLocator( target).getMaskSetForEntireVideoForTuples( media_types=[expectedType]) return get_frames_from_segment(maskSource[0]), get_end_time_from_segment(maskSource[0]), \ get_frames_from_segment(maskTarget[0]), get_end_time_from_segment(maskTarget[0]), \ get_rate_from_segment(maskSource[0]), get_rate_from_segment(maskTarget[0])
def test_Audio_to_Video(self): source = self.locateFile('tests/videos/sample1.mov') extractor = MetaDataExtractor(GraphProxy(source, 'b')) masks = [video_tools.create_segment(endframe= 2618367, rate= 44100, starttime=0.0, frames= 2618367, startframe=1, endtime=59373.424, type='audio')] newMasks = extractor.create_video_for_audio(source, masks=masks) self.assertTrue(len(newMasks) > len(masks)) self.assertTrue(video_tools.get_start_frame_from_segment(newMasks[1]) == 1) self.assertTrue(video_tools.get_end_frame_from_segment(newMasks[1]) == 803) self.assertTrue(video_tools.get_rate_from_segment(newMasks[1]) == 28.25) self.assertTrue(video_tools.get_end_time_from_segment(newMasks[1]) == 59348.333) source = self.locateFile('tests/videos/Sample1_slow.mov') masks = [video_tools.create_segment(endframe= 441000, rate= 44100, starttime=1000.0, frames= 396901, startframe=44100, endtime=10000.0, type='audio')] newMasks = extractor.create_video_for_audio(source, masks=masks) self.assertTrue(len(newMasks) > len(masks)) self.assertTrue(video_tools.get_rate_from_segment(newMasks[1]) == 10.0) self.assertTrue(video_tools.get_start_frame_from_segment(newMasks[1]) == 11) self.assertTrue(video_tools.get_end_frame_from_segment(newMasks[1]) == 100)
def run_warp(source, target, start_time, end_time): source_set = video_tools.FileMetaDataLocator( source).getMaskSetForEntireVideo(start_time=start_time, end_time=end_time) self.create_masks(source_set) extractor = MetaDataExtractor(GraphProxy(source, target)) target_set = video_tools.FileMetaDataLocator( target).getMaskSetForEntireVideoForTuples( start_time_tuple=(video_tools.get_start_time_from_segment( source_set[0]), 0), end_time_tuple=(video_tools.get_end_time_from_segment( source_set[0]), 0)) new_mask_set = extractor.warpMask(source_set, source, target) self.assertTrue( video_tools.get_frames_from_segment(new_mask_set[0]) == video_tools.get_frames_from_segment(target_set[0])) self.assertTrue( video_tools.get_end_time_from_segment(new_mask_set[0]) == video_tools.get_end_time_from_segment(target_set[0])) self.assertTrue( video_tools.get_rate_from_segment(new_mask_set[0]) == video_tools.get_rate_from_segment(target_set[0])) self.assertTrue( video_tools.get_start_frame_from_segment(new_mask_set[0]) == video_tools.get_start_frame_from_segment(target_set[0])) self.assertTrue( video_tools.get_start_time_from_segment(new_mask_set[0]) == video_tools.get_start_time_from_segment(target_set[0])) file_data = self.read_masks(new_mask_set) self.assertEqual( video_tools.get_frames_from_segment(new_mask_set[0]), file_data[0]['frames'])
def create_masks(self, mask_set): from maskgen import tool_set import numpy as np for segment in mask_set: writer = tool_set.GrayBlockWriter( 'test_warp', video_tools.get_rate_from_segment(segment)) for i in range(video_tools.get_frames_from_segment(segment)): f = i + video_tools.get_start_frame_from_segment(segment) writer.write( np.random.randint(0, 1, (1000, 1000), 'uint8') * 255, (f - 1) * video_tools.get_rate_from_segment(segment), frame_number=f) writer.close() video_tools.update_segment(segment, videosegment=writer.filename) self.addFileToRemove(writer.filename)
def create(self, arguments={}, invert=False): from maskgen.tool_set import getMilliSecondsAndFrameCount media_types = ['video', 'audio'] if getValue( arguments, 'include audio', 'no') == 'yes' else ['video'] from maskgen.video_tools import FileMetaDataLocator end_time_tuple = getMilliSecondsAndFrameCount( getValue(arguments, 'End Time', "00:00:00")) start_time_tuple = getMilliSecondsAndFrameCount( getValue(arguments, 'Start Time', '00:00:00')) video_set = FileMetaDataLocator( self.startFileName).getMaskSetForEntireVideoForTuples( start_time_tuple=start_time_tuple, end_time_tuple=end_time_tuple if end_time_tuple[1] > start_time_tuple[1] else None, media_types=media_types) audio_segments = [ x for x in video_set if get_type_of_segment(x) == 'audio' ] video_segments = [ x for x in video_set if get_type_of_segment(x) == 'video' ] if getValue(arguments, 'include audio', 'no') == 'yes': for audio_segment in audio_segments: video_segment = video_segments[0] if len( video_segments) > 0 else audio_segment update_segment( audio_segment, type='audio', starttime=get_start_time_from_segment(video_segment), endtime=get_end_time_from_segment(video_segment), startframe=int( get_start_time_from_segment(video_segment) * get_rate_from_segment(audio_segment) / 1000.0), endframe=int( get_end_time_from_segment(video_segment) * get_rate_from_segment(audio_segment) / 1000.0) + 1) return video_set
def create_video_for_audio(self, source, masks): """ make a mask in video time for each audio mask in masks. in VFR case, uses ffmpeg frames to get nearest frame to timestamp. :param source: video file :param masks: :return: new set of masks """ def _get_frame_time(frame): if 'pkt_pts_time' in frame.keys( ) and frame['pkt_pts_time'] != 'N/A': return float(frame['pkt_pts_time']) * 1000 else: return float(frame['pkt_dts_time']) * 1000 def _frame_distance(time_a, time_b): dist = time_a - time_b return abs(dist) if dist <= 0 else float('inf') meta_and_frames = self.getVideoMeta(source, show_streams=True, with_frames=False, media_types=['video']) indices = ffmpeg_api.get_stream_indices_of_type( meta_and_frames[0], 'video') meta = meta_and_frames[indices[0]][0] isVFR = ffmpeg_api.is_vfr(meta) video_masks = [ mask for mask in masks if get_type_of_segment(mask) == 'video' ] audio_masks = [ mask for mask in masks if get_type_of_segment(mask) == 'audio' ] if len(video_masks) == 0 and indices: entire_mask = self.getMetaDataLocator( source).getMaskSetForEntireVideoForTuples( media_types=['video'])[0] upper_bounds = (get_end_frame_from_segment(entire_mask), get_end_time_from_segment(entire_mask)) new_masks = list(audio_masks) for mask in audio_masks: end_time = min(get_end_time_from_segment(mask), upper_bounds[1]) new_mask = mask.copy() rate = get_rate_from_segment(entire_mask) if not isVFR: start_frame = int( get_start_time_from_segment(mask) * rate / 1000.0) + 1 end_frame = int(end_time * rate / 1000.0) else: video_frames = \ self.getVideoMeta(source, show_streams=True, with_frames=True, media_types=['video'])[1][0] start_frame = video_frames.index( min(video_frames, key=lambda x: _frame_distance( _get_frame_time(x), get_start_time_from_segment(mask)))) + 1 end_frame = video_frames.index( min(video_frames, key=lambda x: _frame_distance( _get_frame_time(x), end_time))) + 1 end_frame = min(end_frame, upper_bounds[0]) update_segment(new_mask, type='video-associate', rate=rate, endtime=end_time, startframe=start_frame, endframe=end_frame) new_masks.append(new_mask) return new_masks else: return masks
def testWarp(self): source = self.locateFile('tests/videos/sample1.mov') target = 'sample1_ffr_ex.mov' source_set = video_tools.getMaskSetForEntireVideo(video_tools.FileMetaDataLocator(source), start_time='29', end_time='55') target_set = video_tools.getMaskSetForEntireVideoForTuples(video_tools.FileMetaDataLocator(target), start_time_tuple=(video_tools.get_start_time_from_segment(source_set[0]), 0), end_time_tuple=(video_tools.get_end_time_from_segment(source_set[0]), 0)) print(source_set[0]) extractor = MetaDataExtractor(GraphProxy(source,target)) new_mask_set = extractor.warpMask(source_set, source, source) print(new_mask_set[0]) self.assertTrue(video_tools.get_frames_from_segment(new_mask_set[0]) == video_tools.get_frames_from_segment(source_set[0])) self.assertTrue(video_tools.get_end_time_from_segment(new_mask_set[0]) == video_tools.get_end_time_from_segment(source_set[0])) self.assertTrue(video_tools.get_rate_from_segment(new_mask_set[0]) == video_tools.get_rate_from_segment(source_set[0])) self.assertTrue(video_tools.get_start_frame_from_segment(new_mask_set[0]) == video_tools.get_start_frame_from_segment(source_set[0])) self.assertTrue(video_tools.get_start_time_from_segment(new_mask_set[0]) == video_tools.get_start_time_from_segment(source_set[0])) self._add_mask_files_to_kill(source_set) new_mask_set = extractor.warpMask(source_set, source, target) self.assertTrue(video_tools.get_frames_from_segment(new_mask_set[0]) == video_tools.get_frames_from_segment(target_set[0])) self.assertTrue(video_tools.get_end_time_from_segment(new_mask_set[0]) == video_tools.get_end_time_from_segment(target_set[0])) self.assertTrue(video_tools.get_rate_from_segment(new_mask_set[0]) == video_tools.get_rate_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_frame_from_segment(new_mask_set[0]) == video_tools.get_start_frame_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_time_from_segment(new_mask_set[0]) == video_tools.get_start_time_from_segment(target_set[0])) source_mask_set = extractor.warpMask(new_mask_set, source, target, inverse=True) self.assertTrue(abs(video_tools.get_frames_from_segment(source_mask_set[0]) - video_tools.get_frames_from_segment(source_set[0])) < 2) self.assertTrue(abs(video_tools.get_end_time_from_segment(source_mask_set[0]) - video_tools.get_end_time_from_segment(source_set[0])) < video_tools.get_error_from_segment(source_mask_set[0]) * 2) self.assertTrue(abs(video_tools.get_rate_from_segment(source_mask_set[0]) - video_tools.get_rate_from_segment(source_set[0])) < 0.1) self.assertTrue(abs(video_tools.get_start_frame_from_segment(source_mask_set[0]) - video_tools.get_start_frame_from_segment(source_set[0])) < 2) self.assertTrue( abs(video_tools.get_start_time_from_segment(source_mask_set[0]) - video_tools.get_start_time_from_segment(source_set[0])) < video_tools.get_error_from_segment(source_mask_set[0]) * 2) new_mask_set = extractor.warpMask(source_set, source, target, useFFMPEG=True) self.assertTrue(video_tools.get_frames_from_segment(new_mask_set[0]) == video_tools.get_frames_from_segment(target_set[0])) self.assertTrue(video_tools.get_end_time_from_segment(new_mask_set[0]) == video_tools.get_end_time_from_segment(target_set[0])) self.assertTrue(video_tools.get_rate_from_segment(new_mask_set[0]) == video_tools.get_rate_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_frame_from_segment(new_mask_set[0]) == video_tools.get_start_frame_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_time_from_segment(new_mask_set[0]) == video_tools.get_start_time_from_segment(target_set[0])) source_mask_set = extractor.warpMask(new_mask_set, source, target, inverse=True, useFFMPEG=True) self.assertTrue(abs(video_tools.get_frames_from_segment(source_mask_set[0]) - video_tools.get_frames_from_segment(source_set[0])) < 2) self.assertTrue(abs(video_tools.get_end_time_from_segment(source_mask_set[0]) - video_tools.get_end_time_from_segment(source_set[0])) < video_tools.get_error_from_segment(source_mask_set[0]) * 2) self.assertTrue(abs(video_tools.get_rate_from_segment(source_mask_set[0]) - video_tools.get_rate_from_segment(source_set[0])) < 0.1) self.assertTrue(abs(video_tools.get_start_frame_from_segment(source_mask_set[0]) - video_tools.get_start_frame_from_segment(source_set[0])) < 2) self.assertTrue( abs(video_tools.get_start_time_from_segment(source_mask_set[0]) - video_tools.get_start_time_from_segment(source_set[0])) < video_tools.get_error_from_segment(source_mask_set[0]) * 2) source_set = target_set source = target target = 'sample1_ffr_2_ex.mov' target_set = video_tools.getMaskSetForEntireVideoForTuples(video_tools.FileMetaDataLocator(target), start_time_tuple=(video_tools.get_start_time_from_segment(source_set[0]), 0), end_time_tuple=(video_tools.get_end_time_from_segment(source_set[0]), 0)) new_mask_set = extractor.warpMask(new_mask_set, source, target) self.assertTrue(video_tools.get_frames_from_segment(new_mask_set[0]) == video_tools.get_frames_from_segment(target_set[0])) self.assertTrue(video_tools.get_end_time_from_segment(new_mask_set[0]) == video_tools.get_end_time_from_segment(target_set[0])) self.assertTrue(video_tools.get_rate_from_segment(new_mask_set[0]) == video_tools.get_rate_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_frame_from_segment(new_mask_set[0]) == video_tools.get_start_frame_from_segment(target_set[0])) self.assertTrue(video_tools.get_start_time_from_segment(new_mask_set[0]) == video_tools.get_start_time_from_segment(target_set[0]))