k4d3 commited on
Commit
c45b591
·
1 Parent(s): 6c29921

add keyframe, a tool to extract keyframes from videos with ffmpeg

Browse files
Files changed (1) hide show
  1. keyframe +42 -0
keyframe ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ import os
5
+ import sys
6
+ import subprocess
7
+
8
+
9
+ def extract_keyframes(video_file_path):
10
+ """
11
+ Extract keyframes from the given video file.
12
+
13
+ Parameters:
14
+ video_file_path (str): The path to the video file.
15
+ """
16
+ # Get the base name of the video file without extension
17
+ base_name = os.path.splitext(os.path.basename(video_file_path))[0]
18
+
19
+ # Create a directory with the same name as the video file
20
+ output_dir = os.path.join(os.path.dirname(video_file_path), base_name)
21
+ os.makedirs(output_dir, exist_ok=True)
22
+
23
+ # Command to extract keyframes using ffmpeg
24
+ ffmpeg_command = [
25
+ 'ffmpeg',
26
+ '-i', video_file_path,
27
+ '-vf', 'select=eq(pict_type\\,I)',
28
+ '-vsync', 'vfr',
29
+ os.path.join(output_dir, 'frame_%03d.png')
30
+ ]
31
+
32
+ # Run the ffmpeg command
33
+ subprocess.run(ffmpeg_command, check=True)
34
+
35
+
36
+ if __name__ == "__main__":
37
+ if len(sys.argv) != 2:
38
+ print("Usage: python keyframe.py <path_to_video>")
39
+ sys.exit(1)
40
+
41
+ video_path = sys.argv[1]
42
+ extract_keyframes(video_path)