forked from baowenbo/DAIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideotopng.py
77 lines (62 loc) · 1.82 KB
/
videotopng.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import cv2
import sys
import os
from spinner import Spinner
IMG_TYPE = 'png'
# Set video to be converted
try:
vid_str = sys.argv[1]
vidcap = cv2.VideoCapture(vid_str)
except IndexError:
vid_str = 'test.mp4'
vidcap = cv2.VideoCapture(vid_str)
# Set output directory
try:
save_dir = sys.argv[2]
except IndexError:
# save_dir = vid_str+'_frames'
save_dir = 'test_export_png'
print('Attempting to convert '+vid_str+' to '+save_dir)
if not os.path.exists(save_dir):
os.mkdir(save_dir)
def find_fps(video):
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver) < 3:
fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
else:
fps = video.get(cv2.CAP_PROP_FPS)
print("Frames per second: {0}".format(fps))
return fps
# useful to capture at a slower frame rate
def get_frame(video, sec):
video.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
has_frames, image = video.read()
if has_frames:
cv2.imwrite(os.path.join(save_dir, str(count)+"."+IMG_TYPE), image)
return has_frames
def print_all_frames(video, save_dir):
count = 1
while (video.isOpened()):
has_frames, frame = video.read()
if has_frames:
cv2.imwrite(os.path.join(save_dir, str(count) + "." + IMG_TYPE), frame)
else:
break
# keyboard break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
count += 1
fps = find_fps(vidcap)
with Spinner():
print_all_frames(vidcap, save_dir)
# sec = 0
# count = 1
# success = get_frame(vidcap, sec)
# while success:
# count = count + 1
# sec = sec + fps
# sec = round(sec, 2)
# success = get_frame(vidcap, sec)
vidcap.release()
print('Conversion completed for '+vid_str+'. All frames in '+save_dir+' with <num>.'+IMG_TYPE+' format')