Upload process_video_to_14frames.py
Browse files- process_video_to_14frames.py +70 -0
process_video_to_14frames.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import shutil
|
| 5 |
+
import argparse
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
def process_video(input_path: str, output_folder: str) -> str:
|
| 9 |
+
"""Process a single video file to 14 frames"""
|
| 10 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
video = cv2.VideoCapture(input_path)
|
| 13 |
+
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 14 |
+
fps = video.get(cv2.CAP_PROP_FPS)
|
| 15 |
+
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 16 |
+
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 17 |
+
|
| 18 |
+
frame_indices = np.linspace(0, frame_count - 1, num=14, dtype=int)
|
| 19 |
+
sampled_frames = []
|
| 20 |
+
current_frame = 0
|
| 21 |
+
frames_read = 0
|
| 22 |
+
|
| 23 |
+
while True:
|
| 24 |
+
ret, frame = video.read()
|
| 25 |
+
if not ret:
|
| 26 |
+
break
|
| 27 |
+
if current_frame in frame_indices:
|
| 28 |
+
sampled_frames.append(frame)
|
| 29 |
+
frames_read += 1
|
| 30 |
+
if frames_read == len(frame_indices):
|
| 31 |
+
break
|
| 32 |
+
current_frame += 1
|
| 33 |
+
|
| 34 |
+
video.release()
|
| 35 |
+
|
| 36 |
+
output_video_path = os.path.join(output_folder, os.path.basename(input_path))
|
| 37 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 38 |
+
out_video = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 39 |
+
|
| 40 |
+
for frame in sampled_frames:
|
| 41 |
+
out_video.write(frame)
|
| 42 |
+
|
| 43 |
+
out_video.release()
|
| 44 |
+
|
| 45 |
+
# Save individual frames
|
| 46 |
+
frame_folder = os.path.join(output_folder, os.path.splitext(os.path.basename(input_path))[0])
|
| 47 |
+
os.makedirs(frame_folder, exist_ok=True)
|
| 48 |
+
|
| 49 |
+
for idx, frame in enumerate(sampled_frames):
|
| 50 |
+
frame_image_path = os.path.join(frame_folder, f'frame_{idx:03d}.png')
|
| 51 |
+
cv2.imwrite(frame_image_path, frame)
|
| 52 |
+
|
| 53 |
+
return output_video_path
|
| 54 |
+
|
| 55 |
+
def main():
|
| 56 |
+
parser = argparse.ArgumentParser(description='Process video to 14 frames')
|
| 57 |
+
parser.add_argument('--input', type=str, required=True, help='Input video file')
|
| 58 |
+
parser.add_argument('--output', type=str, required=True, help='Output folder path')
|
| 59 |
+
|
| 60 |
+
args = parser.parse_args()
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
output_path = process_video(args.input, args.output)
|
| 64 |
+
print(f"Successfully processed video to: {output_path}")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error processing video: {str(e)}", file=sys.stderr)
|
| 67 |
+
sys.exit(1)
|
| 68 |
+
|
| 69 |
+
if __name__ == '__main__':
|
| 70 |
+
main()
|