File size: 1,141 Bytes
c45b591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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)