Created
January 4, 2025 18:20
-
-
Save me-suzy/94400725afef8c73cce18d01de8d395e to your computer and use it in GitHub Desktop.
Extract PNG Screenshots from Video.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import os | |
from pathlib import Path | |
def extract_frames(video_path, interval_seconds=5): | |
# Obține directorul unde se află videoclipul | |
video_dir = os.path.dirname(video_path) | |
# Creează subfolderul 'images' în același director cu videoclipul | |
output_folder = os.path.join(video_dir, 'images') | |
Path(output_folder).mkdir(parents=True, exist_ok=True) | |
# Deschide videoclipul | |
video = cv2.VideoCapture(video_path) | |
# Obține FPS-ul și numărul total de cadre | |
fps = video.get(cv2.CAP_PROP_FPS) | |
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) | |
# Calculează intervalul de cadre | |
frame_interval = int(fps * interval_seconds) | |
count = 0 | |
frame_number = 0 | |
while True: | |
success, frame = video.read() | |
if not success: | |
break | |
if frame_number % frame_interval == 0: | |
output_path = os.path.join(output_folder, f'frame_{count:05d}.png') | |
cv2.imwrite(output_path, frame) | |
count += 1 | |
frame_number += 1 | |
video.release() | |
print(f'Au fost extrase {count} imagini în folderul {output_folder}') | |
# Folosire | |
video_path = 'd:/77/removed_001020.mp4' | |
extract_frames(video_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment