Skip to content

Commit 7629645

Browse files
VeldhoenSara Veldhoen
andauthored
Extend image_name_template to allow timestamp (#395)
* Added TIMESTAMP_MS to file_path substitutions * Added $TIMESTAMP to image_name_template in test * Improved variable name * Added $TIMECODE to image name template options (also added $FRAME_NUMBER to test) * Added TIMESTAMP_MS to file_path substitutions * Added $TIMESTAMP to image_name_template in test * Improved variable name * Added $TIMECODE to image name template options (also added $FRAME_NUMBER to test) * Use semicolon as timestamp separator in filename * Proper string concatenation * Updated docstring --------- Co-authored-by: Sara Veldhoen <s.veldhoen@beeldengeluid.nl>
1 parent dbd8455 commit 7629645

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

scenedetect/scene_manager.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ def save_images(scene_list: List[Tuple[FrameTimecode, FrameTimecode]],
383383
encoder_param: Quality/compression efficiency, based on type of image:
384384
'jpg' / 'webp': Quality 0-100, higher is better quality. 100 is lossless for webp.
385385
'png': Compression from 1-9, where 9 achieves best filesize but is slower to encode.
386-
image_name_template: Template to use when creating the images on disk. Can
387-
use the macros $VIDEO_NAME, $SCENE_NUMBER, and $IMAGE_NUMBER. The image
388-
extension is applied automatically as per the argument image_extension.
386+
image_name_template: Template to use when creating the images on disk. Can use the macros
387+
$VIDEO_NAME, $SCENE_NUMBER, $IMAGE_NUMBER, $FRAME_NUMBER, and $TIMESTAMP_MS.
388+
The image extension is applied automatically as per the argument image_extension.
389389
output_dir: Directory to output the images into. If not set, the output
390390
is created in the working directory.
391391
show_progress: If True, shows a progress bar if tqdm is installed.
@@ -489,11 +489,16 @@ def save_images(scene_list: List[Tuple[FrameTimecode, FrameTimecode]],
489489
frame_im = video.read()
490490
if frame_im is not None:
491491
# TODO: Allow NUM to be a valid suffix in addition to NUMBER.
492-
file_path = '%s.%s' % (filename_template.safe_substitute(
493-
VIDEO_NAME=video.name,
494-
SCENE_NUMBER=scene_num_format % (i + 1),
495-
IMAGE_NUMBER=image_num_format % (j + 1),
496-
FRAME_NUMBER=image_timecode.get_frames()), image_extension)
492+
file_path = '%s.%s' % (
493+
filename_template.safe_substitute(
494+
VIDEO_NAME=video.name,
495+
SCENE_NUMBER=scene_num_format % (i + 1),
496+
IMAGE_NUMBER=image_num_format % (j + 1),
497+
FRAME_NUMBER=image_timecode.get_frames(),
498+
TIMESTAMP_MS=int(image_timecode.get_seconds() * 1000),
499+
TIMECODE=image_timecode.get_timecode().replace(":", ";")),
500+
image_extension,
501+
)
497502
image_filenames[i].append(file_path)
498503
# TODO: Combine this resize with the ones below.
499504
if aspect_ratio is not None:

tests/test_scene_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def test_save_images(test_video_file):
9494
sm.add_detector(ContentDetector())
9595

9696
image_name_glob = 'scenedetect.tempfile.*.jpg'
97-
image_name_template = 'scenedetect.tempfile.$SCENE_NUMBER.$IMAGE_NUMBER'
97+
image_name_template = ('scenedetect.tempfile.'
98+
'$SCENE_NUMBER.$IMAGE_NUMBER.$FRAME_NUMBER.'
99+
'$TIMESTAMP_MS.$TIMECODE')
98100

99101
try:
100102
video_fps = video.frame_rate

0 commit comments

Comments
 (0)