toolkit / keyframe
k4d3's picture
Update
51b7ba7
raw
history blame
1.14 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
def extract_keyframes(video_file_path):
"""
Extract keyframes from the given video file.
Parameters:
video_file_path (str): The path to the video file.
"""
# Get the base name of the video file without extension
base_name = os.path.splitext(os.path.basename(video_file_path))[0]
# Create a directory with the same name as the video file
output_dir = os.path.join(os.path.dirname(video_file_path), base_name)
os.makedirs(output_dir, exist_ok=True)
# Command to extract keyframes using ffmpeg
ffmpeg_command = [
'ffmpeg',
'-i', video_file_path,
'-vf', 'select=eq(pict_type\\,I)',
'-vsync', 'vfr',
os.path.join(output_dir, 'frame_%03d.png')
]
# Run the ffmpeg command
subprocess.run(ffmpeg_command, check=True)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python keyframe.py <path_to_video>")
sys.exit(1)
video_path = sys.argv[1]
extract_keyframes(video_path)