Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_save_load_from_video(test_video_file):
""" Test generating and saving some frame metrics from TEST_VIDEO_FILE to a file on disk, and
loading the file back to ensure the loaded frame metrics agree with those that were saved.
"""
video_manager = VideoManager([test_video_file])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
base_timecode = video_manager.get_base_timecode()
scene_manager.add_detector(ContentDetector())
try:
video_fps = video_manager.get_framerate()
start_time = FrameTimecode('00:00:00', video_fps)
duration = FrameTimecode('00:00:20', video_fps)
video_manager.set_duration(start_time=start_time, end_time=duration)
video_manager.set_downscale_factor()
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
def test_detector_metrics(test_video_file):
""" Test passing StatsManager to a SceneManager and using it for storing the frame metrics
from a ContentDetector.
"""
video_manager = VideoManager([test_video_file])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
#base_timecode = video_manager.get_base_timecode()
assert not stats_manager._registered_metrics
scene_manager.add_detector(ContentDetector())
# add_detector should trigger register_metrics in the StatsManager.
assert stats_manager._registered_metrics
try:
video_fps = video_manager.get_framerate()
start_time = FrameTimecode('00:00:00', video_fps)
duration = FrameTimecode('00:00:20', video_fps)
video_manager.set_duration(start_time=start_time, end_time=duration)
video_manager.set_downscale_factor()
video_manager.start()
def test_load_hardcoded_file(test_video_file):
""" Test loading a stats file with some hard-coded data generated by this test case. """
from scenedetect.stats_manager import COLUMN_NAME_FPS
from scenedetect.stats_manager import COLUMN_NAME_FRAME_NUMBER
from scenedetect.stats_manager import COLUMN_NAME_TIMECODE
stats_manager = StatsManager()
stats_file = open(TEST_STATS_FILES[0], 'w')
try:
stats_writer = get_csv_writer(stats_file)
some_metric_key = 'some_metric'
some_metric_value = 1.2
some_frame_key = 100
base_timecode = FrameTimecode(0, 29.97)
some_frame_timecode = base_timecode + some_frame_key
# Write out a valid file.
stats_writer.writerow([COLUMN_NAME_FPS, '%.10f' % base_timecode.get_framerate()])
stats_writer.writerow(
[COLUMN_NAME_FRAME_NUMBER, COLUMN_NAME_TIMECODE, some_metric_key])
stats_writer.writerow(
def test_load_corrupt_stats(test_video_file):
""" Test loading a corrupted stats file created by outputting data in the wrong format. """
from scenedetect.stats_manager import COLUMN_NAME_FPS
from scenedetect.stats_manager import COLUMN_NAME_FRAME_NUMBER
from scenedetect.stats_manager import COLUMN_NAME_TIMECODE
stats_manager = StatsManager()
stats_files = [open(stats_file, 'wt') for stats_file in TEST_STATS_FILES]
try:
stats_writers = [get_csv_writer(stats_file) for stats_file in stats_files]
some_metric_key = 'some_metric'
some_metric_value = str(1.2)
some_frame_key = 100
base_timecode = FrameTimecode(0, 29.97)
some_frame_timecode = base_timecode + some_frame_key
# Write out some invalid files.
# File 0: Blank FPS [StatsFileCorrupt]
stats_writers[0].writerow([COLUMN_NAME_FPS])
stats_writers[0].writerow(
def test_metrics():
""" Test StatsManager metric registration/setting/getting with a set of pre-defined
key-value pairs (metric_dict).
"""
metric_dict = {'some_metric': 1.2345, 'another_metric': 6.7890}
metric_keys = list(metric_dict.keys())
stats = StatsManager()
frame_key = 100
assert not stats.is_save_required()
stats.register_metrics(metric_keys)
assert not stats.is_save_required()
with pytest.raises(FrameMetricRegistered):
stats.register_metrics(metric_keys)
assert not stats.metrics_exist(frame_key, metric_keys)
assert stats.get_metrics(frame_key, metric_keys) == [None] * len(metric_keys)
stats.set_metrics(frame_key, metric_dict)
assert stats.is_save_required()
def scene_detect(opt):
video_manager = VideoManager([os.path.join(opt.avi_dir,opt.reference,'video.avi')])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
# Add ContentDetector algorithm (constructor takes detector options like threshold).
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
video_manager.set_downscale_factor()
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
scene_list = scene_manager.get_scene_list(base_timecode)
savepath = os.path.join(opt.work_dir,opt.reference,'scene.pckl')
if scene_list == []:
def scene_detect(opt):
video_manager = VideoManager([os.path.join(opt.avi_dir,opt.reference,'video.avi')])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
# Add ContentDetector algorithm (constructor takes detector options like threshold).
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
video_manager.set_downscale_factor()
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
scene_list = scene_manager.get_scene_list(base_timecode)
savepath = os.path.join(opt.work_dir,'scene.pckl')
if scene_list == []:
def _open_stats_file(self):
if self.stats_manager is None:
self.stats_manager = StatsManager()
if self.stats_file_path is not None:
if os.path.exists(self.stats_file_path):
logging.info('Loading frame metrics from stats file: %s',
os.path.basename(self.stats_file_path))
try:
with open(self.stats_file_path, 'rt') as stats_file:
self.stats_manager.load_from_csv(stats_file, self.base_timecode)
except StatsFileCorrupt:
error_strs = [
'Could not load stats file.', 'Failed to parse stats file:',
'Could not load frame metrics from stats file - file is corrupt or not a'
' valid PySceneDetect stats file. If the file exists, ensure that it is'
' a valid stats file CSV, otherwise delete it and run PySceneDetect again'
' to re-generate the stats file.']
logging.error('\n'.join(error_strs))
def find_scenes(video_path):
start_time = time.time()
print("Analyzing video "+video_path)
# type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]
video_manager = VideoManager([video_path])
stats_manager = StatsManager()
# Pass StatsManager to SceneManager to accelerate computing time
scene_manager = SceneManager(stats_manager)
# Add ContentDetector algorithm (each detector's constructor
# takes detector options, e.g. threshold).
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
# We save our stats file to {VIDEO_PATH}.stats.csv.
stats_file_path = '%s.stats.csv' % (video_path)
scene_list = []
folder = os.path.splitext(video_path)[0]