def test_convert_deprecate_list_response(): with open(os.path.join(get_testfiles_path(), 'small.mp4'), 'rb') as file: video_base_64 = base64.b64encode(file.read()).decode() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") convert(video_base_64, max_frames=13, frame_rate=2, return_dict=False) assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "Returning a list instead of a list of dictionaries." in str( w[-1].message)
def test_convert_dictionary_return(): with open(os.path.join(get_testfiles_path(), 'small.mp4'), 'rb') as file: video_base_64 = base64.b64encode(file.read()).decode() frames = convert(video_base_64, max_frames=5, frame_rate=1, return_dict=True) assert len(frames) == 5 for frame in frames: assert isinstance(frame, dict) assert set(frame.keys()) == {'base64image', 'frameNumber'} assert frames[0] != frames[-1]
def test_convert_video_timestamp(): with open(os.path.join(get_testfiles_path(), 'small.mp4'), 'rb') as file: video_base_64 = base64.b64encode(file.read()).decode() frames = convert(video_base_64, max_frames=6, video_timestamp='2019-02-10 20:25:00') assert len(frames) == 6 for frame in frames: assert isinstance(frame, dict) assert set(frame.keys()) == {'base64image', 'frameNumber', 'timestamp'} assert [ '2019-02-10 20:25:00', '2019-02-10 20:25:00', '2019-02-10 20:25:00', '2019-02-10 20:25:00', '2019-02-10 20:25:00', '2019-02-10 20:25:00' ] == [frame['timestamp'] for frame in frames]
def video_to_frames(input_path, output_dir, max_frames, even): base_filename = input_path.split(os.path.sep)[-1].split('.')[0] with open(input_path, 'rb') as file: video_base_64 = base64.b64encode(file.read()) frames = convert(video_base_64=video_base_64, frame_rate=None, max_frames=max_frames, even=even, return_dict=True) for frame in frames: base_64_image = frame['base64image'] frame_number = frame['frameNumber'] output_path = os.path.join( output_dir, base_filename + '-frame{:03d}.jpg'.format(frame_number)) with open(output_path, 'wb') as file: file.write(base64.b64decode(base_64_image)) return True
def test_convert(): with open(os.path.join(get_testfiles_path(), 'small.mp4'), 'rb') as file: video_base_64 = base64.b64encode(file.read()).decode() frames = convert(video_base_64) assert frames[0] != frames[-1] assert len(frames) == 166
def test_convert_frame_rate_max_frames_higher_than_frame_count(): with open(os.path.join(get_testfiles_path(), 'small.mp4'), 'rb') as file: video_base_64 = base64.b64encode(file.read()).decode() frames = convert(video_base_64, max_frames=13, frame_rate=2) assert len(frames) == 12 assert frames[0] != frames[-1]