-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
151 lines (136 loc) · 4.98 KB
/
run.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import cv2
import os
import pprint
class VideoWriter:
"""docstring for VideoWriter"""
def __init__(self, arg):
super(VideoWriter, self).__init__()
self.arg = arg
def __init__(self, video_path, output_dir, clip_duration):
self.video_path = video_path
self.output_dir = output_dir
self.clip_duration = clip_duration
def GenerateClips(self):
cap = cv2.VideoCapture(self.video_path)
nframe = cap.get(cv2.CAP_PROP_FRAME_COUNT)
width = int(cap.get(3)) # float
height = int(cap.get(4)) # float
fps = cap.get(cv2.CAP_PROP_FPS)
frames_per_clip = fps * self.clip_duration
number_of_clips = int(nframe / frames_per_clip)
print("[INFO]...Frames:" + str(nframe))
print("[INFO]...Width: " + str(width) + ", height: " + str(height))
print("[INFO]...FPS: " + str(fps))
print("[INFO]...Clip Duration: " + str(self.clip_duration) + " seconds")
print("[INFO]...Frames Per Clip: " + str(frames_per_clip))
print("[INFO]...Number of Clips for Video "
+ self.video_path + " : " + str(number_of_clips))
# Reading the frames and writing as video
filename = self.GetFileNameFromPath()
output_file = self.output_dir + filename
print("[INFO]... Output Path " + output_file)
self.CheckDirectory()
clip_no = 1
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
writer = cv2.VideoWriter(output_file + "_" + str(clip_no) + ".mp4", fourcc, fps, (width,height))
print("[INFO]... Writing Clip: " + str(clip_no) + " of " + str(number_of_clips))
for x in range(1,int(nframe)):
flag, frame = cap.read()
if flag:
writer.write(frame)
# cv2.imshow('frame',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
if(x%int(frames_per_clip) == 0):
writer.release()
clip_no = clip_no + 1
if(clip_no == number_of_clips + 1):
break;
print("[INFO]... Writing Clip: " + str(clip_no) + " of " + str(number_of_clips))
writer = cv2.VideoWriter(output_file + "_" + str(clip_no) + ".mp4", fourcc, fps, (width,height))
cap.release()
def CheckDirectory(self):
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def GetFileNameFromPath(self):
## File Name without extension
file = os.path.basename(self.video_path)
return os.path.splitext(file)[0]
## Complete File name
#return os.path.basename(self.video_path)
class VideoCaption:
"""docstring for VideoCaption"""
def __init__(self, arg):
super(VideoCaption, self).__init__()
self.arg = arg
def __init__(self, data_dir):
self.videos_path = data_dir
self.videos_list = os.listdir(data_dir)
self.unique_videos = []
videos_dict = {}
for x in range(len(self.videos_list)):
file_name = os.path.splitext(self.videos_list[x])[0].split('_')[0]
if not file_name in self.unique_videos:
self.unique_videos.append(file_name)
videos_dict[file_name] = []
sub_clips = {}
sub_clips['name'] = os.path.splitext(self.videos_list[x])[0]
sub_clips['captions'] = []
sub_clips['captions'].append("Caption 1")
sub_clips['captions'].append("Caption 2")
sub_clips['captions'].append("Caption 3")
sub_clips['captions'].append("Caption 4")
sub_clips['captions'].append("Caption 5")
videos_dict[file_name].append(sub_clips)
# print(self.unique_videos)
# videos_dict = {}
# sub_clips = {}
# sub_clips['name'] = "19_1"
# sub_clips['captions'] = []
# sub_clips['captions'].append("Caption 1")
# sub_clips['captions'].append("Caption 2")
# sub_clips['captions'].append("Caption 3")
# sub_clips['captions'].append("Caption 4")
# sub_clips['captions'].append("Caption 5")
# sub_clips1 = {}
# sub_clips1['name'] = "19_2"
# sub_clips1['captions'] = []
# sub_clips1['captions'].append("Caption 1")
# sub_clips1['captions'].append("Caption 2")
# sub_clips1['captions'].append("Caption 3")
# sub_clips1['captions'].append("Caption 4")
# sub_clips1['captions'].append("Caption 5")
# for i in range(len(self.unique_videos)):
# videos_dict[self.unique_videos[i]] = []
# videos_dict[self.unique_videos[i]].append(sub_clips)
# videos_dict[self.unique_videos[i]].append(sub_clips1)
pprint.pprint(videos_dict, width=10)
def BatchProcessor(videos_directory, CLIP_DURATION):
OUTPUT_DIR = videos_directory + "/data/"
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
#CLIP_DURATION = 2 # in seconds
videos = os.listdir(videos_directory)
for i in range(len(videos)):
vw = VideoWriter(videos_directory + "\\" + videos[i], OUTPUT_DIR, CLIP_DURATION)
vw.GenerateClips()
#print(vw.GetFileNameFromPath())
return True
def OneVideo(video_path, clip_duration):
directory = os.path.dirname(os.path.abspath(video_path))
output_dir = directory + "/data/"
#clip_duration = 2 # in seconds
vw = VideoWriter(video_path, output_dir, clip_duration)
vw.GenerateClips()
return True
def main():
pass
#videos_directory = "Videos"
#data_dir = videos_directory + "\\data\\"
#data_dir = "Videos\\data\\"
#v_caption = VideoCaption(data_dir)
#BatchProcessor(videos_directory)
#OneVideo("58.mp4")
#print(vw.GetFileNameFromPath())
if __name__ == '__main__':
main()