-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo2img.py
More file actions
61 lines (52 loc) · 1.74 KB
/
Copy pathvideo2img.py
File metadata and controls
61 lines (52 loc) · 1.74 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Version: 1.0
@Author: Mr.WenC
@File: video2img.py
@Time: 2025-10-13 10:48:00
@License: (C)Copyright 2020-2030, Mr.WenC
@Desc:
"""
import os
import cv2
import argparse
from loguru import logger
from datetime import datetime
def video2img(video_path, save_path, fps=1):
"""
将视频文件转换为图片文件
:param video_path: 视频文件路径
:param save_path: 图片保存路径
:param fps: 图片帧率
:return: None
"""
if not os.path.exists(video_path):
print("Error: video file not exists!")
return
if not os.path.exists(save_path):
os.makedirs(save_path)
cap = cv2.VideoCapture(video_path)
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
logger.info("Reached end of video or Failed to read the video.")
break
now = datetime.now()
image_path = os.path.join(save_path, f"{now.strftime('%Y%m%d%H%M%S%f')}.jpg")
if frame_count % fps == 0:
cv2.imwrite(image_path, frame)
logger.info(f"Save image: {image_path}")
frame_count += 1
else:
logger.error("Video to image conversion failed!")
cap.release()
logger.info("Finished and Video to image conversion completed!, ")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert video to image')
parser.add_argument('--video_path', type=str, help='video file path')
parser.add_argument('--save_path', type=str, help='image save path')
parser.add_argument('--fps', type=int, default=1, help='image frame rate')
args = parser.parse_args()
video2img(args.video_path, args.save_path, args.fps)