-
Notifications
You must be signed in to change notification settings - Fork 13
/
split_video.py
66 lines (54 loc) · 1.93 KB
/
split_video.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
# This script takes an input video and saves a section of the individual frames
import cv2
import numpy as np
import os
def save_frames(video, loadpath, savepath):
'''
Saves every 5th frame of the video as a jpg at the savepath location
Arguments
video: the name of the mp4 video
loadpath: the location of the original video
savepath: the location where the frames will be saved
'''
cap = cv2.VideoCapture(loadpath+video)
# Check if camera opened successfully
if (cap.isOpened()==False):
print("Error opening video stream or file")
# Read until video is completed
count = 0
while(cap.isOpened()) and count < 250:
# capture frame by frame
ret, frame = cap.read()
if ret == True and count%5 == 0:
name = video[:-4]+'_'+str(count)+'.jpg'
print(name)
cv2.imwrite(savepath + name, frame)
count += 1
# When everything done, release the video capture object
cap.release()
# Close all frames
cv2.destroyAllWindows()
if __name__ == "__main__":
loadpath = 'D:/noam_/Cornell/CS7999/Macaulay Library Birds - Videos/'
savepath = 'D:/noam_/Cornell/CS7999/Macaulay Library Birds - Frames/'
# Create a map of video IDs to common names
id2name = {}
with open('vids_to_split.txt', 'r') as f:
for line in f:
splitline = line.strip().split('\t')
ID = int(splitline[0])
name = splitline[2]
id2name[ID] = name
all_vids = os.listdir(loadpath)
# Capture video frames of each video
for video in all_vids:
ID = int(video[:-4])
name = id2name[ID]
print(video)
# Create folder to save video frames
try:
os.mkdir(savepath + name)
except:
pass
os.mkdir(savepath + name + '/' + str(ID))
save_frames(video, loadpath, savepath + name + '/' + str(ID)+'/')