-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
45 lines (35 loc) · 1.21 KB
/
utils.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
import cv2
import moviepy.video.io.ImageSequenceClip
import os
import glob
def frames_to_video(out_folder, filename, fps):
image_files = []
for file in glob.glob(f'{out_folder}/*.png'):
image_files.append(file)
image_files.sort(key = lambda x: int(x.split('/')[-1].split('.')[0]))
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile(filename)
def video_to_frames(video, path_output_dir):
# extract frames from a video and save to directory as 'x.png' where
# x is the frame index
vidcap = cv2.VideoCapture(video)
count = 0
while vidcap.isOpened():
success, image = vidcap.read()
if success:
cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
count += 1
print(f'{count}.png')
else:
break
cv2.destroyAllWindows()
vidcap.release()
def video_framerate(video):
video = cv2.VideoCapture(video);
(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)
video.release()
return fps