def dump_frames(video_path, output_directory, frames_per_second):
"""Dump frames at frames_per_second from a video to output_directory.
If frames_per_second is None, the clip's fps attribute is used instead."""
clip = VideoFileClip(video_path)
info_path = '{}/info.json'.format(output_directory)
name_format = '{}/frame%04d.png'.format(output_directory)
if frames_per_second is None:
frames_per_second = clip.fps
frames_already_dumped_helper = lambda: \
frames_already_dumped(video_path, output_directory,
frames_per_second, info_path,
name_format, clip.duration)
if frames_already_dumped_helper():
logging.info('Frames for {} exist, skipping...'.format(video_path))
return
successfully_wrote_images = False
try:
clip.write_images_sequence(
name_format.format(output_directory),
fps=frames_per_second)
successfully_wrote_images = True
except Exception as e:
logging.error("Failed to dump images for %s", video_path)
logging.error(e)
if successfully_wrote_images:
info = {'frames_per_second': frames_per_second,
'input_video_path': os.path.abspath(video_path)}
with open(info_path, 'wb') as info_file:
json.dump(info, info_file)
if not frames_already_dumped_helper():
logging.error(
"Images for {} don't seem to be dumped properly!".format(
video_path))
评论列表
文章目录