Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[some_frame_key, some_frame_timecode.get_timecode(), some_metric_value])
# File 3: Wrong Header Names [StatsFileCorrupt]
stats_writers[3].writerow([COLUMN_NAME_FPS, '%.10f' % base_timecode.get_framerate()])
stats_writers[3].writerow(
[COLUMN_NAME_TIMECODE, COLUMN_NAME_FRAME_NUMBER, some_metric_key])
stats_writers[3].writerow(
[some_frame_key, some_frame_timecode.get_timecode(), some_metric_value])
for stats_file in stats_files: stats_file.close()
stats_files = [open(stats_file, 'rt') for stats_file in TEST_STATS_FILES]
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[0], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[1], base_timecode)
with pytest.raises(StatsFileFramerateMismatch):
stats_manager.load_from_csv(stats_files[2], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[3], base_timecode)
finally:
for stats_file in stats_files: stats_file.close()
for stats_file in TEST_STATS_FILES: os.remove(stats_file)
[COLUMN_NAME_FRAME_NUMBER, COLUMN_NAME_TIMECODE, some_metric_key])
stats_writers[2].writerow(
[some_frame_key, some_frame_timecode.get_timecode(), some_metric_value])
# File 3: Wrong Header Names [StatsFileCorrupt]
stats_writers[3].writerow([COLUMN_NAME_FPS, '%.10f' % base_timecode.get_framerate()])
stats_writers[3].writerow(
[COLUMN_NAME_TIMECODE, COLUMN_NAME_FRAME_NUMBER, some_metric_key])
stats_writers[3].writerow(
[some_frame_key, some_frame_timecode.get_timecode(), some_metric_value])
for stats_file in stats_files: stats_file.close()
stats_files = [open(stats_file, 'rt') for stats_file in TEST_STATS_FILES]
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[0], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[1], base_timecode)
with pytest.raises(StatsFileFramerateMismatch):
stats_manager.load_from_csv(stats_files[2], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[3], base_timecode)
finally:
for stats_file in stats_files: stats_file.close()
for stats_file in TEST_STATS_FILES: os.remove(stats_file)
# If the file is blank or we couldn't decode anything, assume the file was empty.
return num_frames
# First Row (FPS = [...]) and ensure framerate equals base_timecode if set.
if not len(row) == 2 or not row[0] == COLUMN_NAME_FPS:
raise StatsFileCorrupt()
stats_file_framerate = float(row[1])
if stats_file_framerate < MINIMUM_FRAMES_PER_SECOND_FLOAT:
raise StatsFileCorrupt("Invalid framerate detected in CSV stats file "
"(decoded FPS: %f)." % stats_file_framerate)
if base_timecode is not None and not base_timecode.equal_framerate(stats_file_framerate):
raise StatsFileFramerateMismatch(base_timecode.get_framerate(), stats_file_framerate)
# Second Row: Frame Num, Timecode, [metrics...]
try:
row = next(csv_reader)
except StopIteration:
raise StatsFileCorrupt("Header row(s) missing.")
if not row or not len(row) >= 2:
raise StatsFileCorrupt()
if row[0] != COLUMN_NAME_FRAME_NUMBER or row[1] != COLUMN_NAME_TIMECODE:
raise StatsFileCorrupt()
num_cols = len(row)
num_metrics = num_cols - 2
if not num_metrics > 0:
raise StatsFileCorrupt('No metrics defined in CSV file.')
metric_keys = row[2:]
num_frames = 0
for row in csv_reader:
metric_dict = {}
if not len(row) == num_cols:
raise StatsFileCorrupt('Wrong number of columns detected in stats file row.')
for i, metric_str in enumerate(row[2:]):
if metric_str and metric_str != 'None':
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))
raise click.BadParameter(
'\n Could not load given stats file, see above output for details.',
param_hint='input stats file')
except StatsFileFramerateMismatch as ex:
error_strs = [
'could not load stats file.', 'Failed to parse stats file:',
'Framerate differs between stats file (%.2f FPS) and input'
' video%s (%.2f FPS)' % (
ex.stats_file_fps,
except StopIteration:
raise StatsFileCorrupt("Header row(s) missing.")
if not row or not len(row) >= 2:
raise StatsFileCorrupt()
if row[0] != COLUMN_NAME_FRAME_NUMBER or row[1] != COLUMN_NAME_TIMECODE:
raise StatsFileCorrupt()
num_cols = len(row)
num_metrics = num_cols - 2
if not num_metrics > 0:
raise StatsFileCorrupt('No metrics defined in CSV file.')
metric_keys = row[2:]
num_frames = 0
for row in csv_reader:
metric_dict = {}
if not len(row) == num_cols:
raise StatsFileCorrupt('Wrong number of columns detected in stats file row.')
for i, metric_str in enumerate(row[2:]):
if metric_str and metric_str != 'None':
try:
metric_dict[metric_keys[i]] = float(metric_str)
except ValueError:
raise StatsFileCorrupt('Corrupted value in stats file: %s' % metric_str)
self.set_metrics(int(row[0]), metric_dict)
num_frames += 1
logging.info('Loaded %d metrics for %d frames.', num_metrics, num_frames)
if reset_save_required:
self._metrics_updated = False
return num_frames
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))
raise click.BadParameter(
'\n Could not load given stats file, see above output for details.',
param_hint='input stats file')
except StatsFileFramerateMismatch as ex:
error_strs = [
'could not load stats file.', 'Failed to parse stats file:',
'Framerate differs between stats file (%.2f FPS) and input'
' video%s (%.2f FPS)' % (
ex.stats_file_fps,
"(decoded FPS: %f)." % stats_file_framerate)
if base_timecode is not None and not base_timecode.equal_framerate(stats_file_framerate):
raise StatsFileFramerateMismatch(base_timecode.get_framerate(), stats_file_framerate)
# Second Row: Frame Num, Timecode, [metrics...]
try:
row = next(csv_reader)
except StopIteration:
raise StatsFileCorrupt("Header row(s) missing.")
if not row or not len(row) >= 2:
raise StatsFileCorrupt()
if row[0] != COLUMN_NAME_FRAME_NUMBER or row[1] != COLUMN_NAME_TIMECODE:
raise StatsFileCorrupt()
num_cols = len(row)
num_metrics = num_cols - 2
if not num_metrics > 0:
raise StatsFileCorrupt('No metrics defined in CSV file.')
metric_keys = row[2:]
num_frames = 0
for row in csv_reader:
metric_dict = {}
if not len(row) == num_cols:
raise StatsFileCorrupt('Wrong number of columns detected in stats file row.')
for i, metric_str in enumerate(row[2:]):
if metric_str and metric_str != 'None':
try:
metric_dict[metric_keys[i]] = float(metric_str)
except ValueError:
raise StatsFileCorrupt('Corrupted value in stats file: %s' % metric_str)
self.set_metrics(int(row[0]), metric_dict)
num_frames += 1
logging.info('Loaded %d metrics for %d frames.', num_metrics, num_frames)
if reset_save_required:
def __init__(self, message=
"Could not load frame metric data data from passed CSV file."):
# type: (str, str)
# Pass message string to base Exception class.
super(StatsFileCorrupt, self).__init__(message)
num_cols = len(row)
num_metrics = num_cols - 2
if not num_metrics > 0:
raise StatsFileCorrupt('No metrics defined in CSV file.')
metric_keys = row[2:]
num_frames = 0
for row in csv_reader:
metric_dict = {}
if not len(row) == num_cols:
raise StatsFileCorrupt('Wrong number of columns detected in stats file row.')
for i, metric_str in enumerate(row[2:]):
if metric_str and metric_str != 'None':
try:
metric_dict[metric_keys[i]] = float(metric_str)
except ValueError:
raise StatsFileCorrupt('Corrupted value in stats file: %s' % metric_str)
self.set_metrics(int(row[0]), metric_dict)
num_frames += 1
logging.info('Loaded %d metrics for %d frames.', num_metrics, num_frames)
if reset_save_required:
self._metrics_updated = False
return num_frames