-
Notifications
You must be signed in to change notification settings - Fork 9
/
vidGen.py
186 lines (150 loc) · 7.17 KB
/
vidGen.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import cv2
import random
import datetime
import argparse
import numpy as np
import uploadvideo as uv
from moviepy.editor import *
import videoVimeo
def compare_image(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images;
# NOTE: the two images must have the same dimension
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# return the MSE, the lower the error, the more "similar"
# the two images are
return err
def GivemMeARandomNumber(count):
return random.randrange(1,count)
def destroyAllFrames(pathOfFrames,count):
for i in range(1,count+1):
pathToremove = os.path.join(pathOfFrames,'frame%d.jpg' %i)
if os.path.isfile(pathToremove):
os.remove(pathToremove)
os.removedirs(pathOfFrames)
#Function to generate an array of frames from the video
def GenerateFrames(videoLocation,FrameDirectory):
vidcap = cv2.VideoCapture(videoLocation)
success,image = vidcap.read()
count = 1
vidDirectory = os.path.dirname(videoLocation)
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver) < 3 :
fps = vidcap.get(cv2.cv.CV_CAP_PROP_FPS)
else :
fps = vidcap.get(cv2.CAP_PROP_FPS)
while (count < 200) and (success):
success,image = vidcap.read()
frame_read = os.path.join(FrameDirectory,'frame%d.jpg' %count)
cv2.imwrite(frame_read, image) # save frame as JPEG file
if cv2.waitKey(10) == 27: # exit if Escape is hit
break
count += 1
cv2.destroyAllWindows()
vidcap.release()
return count,fps
#need for the preview window
def getFrameFromVideo( videoLocation, frameCount ) :
vidcap = cv2.VideoCapture(videoLocation)
success,image = vidcap.read()
count = 1
vidDirectory = os.path.dirname(videoLocation)
while (count < frameCount) and (success):
success,image = vidcap.read()
frame_read = os.path.join(vidDirectory,'frame%d.jpg' %count)
count += 1
cv2.imwrite(frame_read, image) # save frame as JPEG file
vidcap.release()
return frame_read
#Gives an array of numbers but those numbers corresponds to unidentical frames
def GiveUnidenticalFrames(numVideos,FrameCount,FrameDirectory):
RandomFrame = []
RandomFrameData = GivemMeARandomNumber(FrameCount)
RandomFrame.append(RandomFrameData)
i = 1
UNIDENTICAL_THRESHOLD = 2000
counter_for_loop = 300
if numVideos == 1:
return RandomFrame
else:
while (i<numVideos and counter_for_loop > 0 ):
Match = False
RandomFrameData = GivemMeARandomNumber(FrameCount)
for imgNum in range(0,len(RandomFrame)):
imgA = cv2.imread(os.path.join(FrameDirectory,'frame%d.jpg' %RandomFrame[imgNum]))
imgB = cv2.imread(os.path.join(FrameDirectory,'frame%d.jpg' %RandomFrameData))
mse = compare_image(imgA,imgB)
if mse < UNIDENTICAL_THRESHOLD:
Match = True
break
if(Match is False):
RandomFrame.append(RandomFrameData)
i = i+1
counter_for_loop = counter_for_loop - 1
if counter_for_loop <= 0 : #when loop is stopeed abruptly and we need to fill up the list
while len(RandomFrame) < numVideos :
RandomFrameData = GivemMeARandomNumber(FrameCount)
RandomFrame.append(RandomFrameData)
return RandomFrame
#Call upload to upload the videos
def upload(youtubeObj, VidLocation,titleVid,description,tags,category='22',privacy='private'): #keywords are string of tags separated by commas, description is also a string
if youtubeObj is None :
return
print "Uploading in Youtube"
#Look at the auth params if there is a trouble
args = argparse.Namespace(file=VidLocation,title=titleVid,description=description,keywords= ",".join(tags), category=category,privacyStatus=privacy,auth_host_name='localhost', auth_host_port=[8080, 8090],logging_level='ERROR', noauth_local_webserver=False)
# youtubeObj = uv.authenticateWithYoutube(args)
uv.uploadMyVideo( youtubeObj, args )
print "Upload completed for youtube"
def uploadVimeo(vimeoObj, VidLocation,titleVid,description,tags,category='22',privacy='private'): #keywords are string of tags separated by commas, description is also a string
if vimeoObj is None :
return
print "Uploading in Vimeo"
videoVimeo.upload(vimeoObj, VidLocation,titleVid, description, tags)
print "Upload completed for vimeo"
def GenerateTheVideo(caller, videoLocation, numVideos=1, tags = None, t1=0, t2=0, x=0, y=0, ImageLocation=None, callback = None ):
vidDirectory = os.path.dirname(videoLocation)
#Make directory where you put all images
FrameDirectory = os.path.join(vidDirectory,'Frames')
if not os.path.exists(FrameDirectory):
os.makedirs(FrameDirectory)
FrameCount,frame_rate = GenerateFrames(videoLocation,FrameDirectory)
RandomFrame = GiveUnidenticalFrames(numVideos,FrameCount,FrameDirectory)
durationOfImage = GivemMeARandomNumber(5)
for numVideo_i in range(0,numVideos):
if callback is not None:
callback( numVideo_i )
else :
print "No callback"
if ImageLocation is not None :
OverlayImage = ImageLocation #Location Of Image/Banner
ovrImgClip = ImageClip(OverlayImage,duration=t2-t1) #Create a clip for the duration start
frame_to_use = os.path.join(FrameDirectory,'frame%d.jpg' %RandomFrame[numVideo_i])
clip1 = ImageClip(frame_to_use,duration=durationOfImage)
clip2 = VideoFileClip(videoLocation)
if ImageLocation is not None :
Video = CompositeVideoClip([clip1,clip2.set_start(durationOfImage).crossfadein(1), \
ovrImgClip.set_start(t1+durationOfImage).set_pos((x,y))]) #Overlay the video
else :
Video = CompositeVideoClip([clip1,clip2.set_start(durationOfImage).crossfadein(1)]) #Overlay the video
if tags is not None :
video_name = tags[numVideo_i] + ".mp4"
else :
video_name = "video" + ".mp4"
newFileLocation = os.path.join(vidDirectory,video_name)
Video.write_videofile(newFileLocation,fps=frame_rate)
print "Video creation completed"
if caller is not None :
upload( caller.youtubeObj, VidLocation =newFileLocation ,titleVid = tags.split(",")[numVideo_i], description = video_name, tags=tags, category='22',privacy='public' )
uploadVimeo(caller.vimeoObj, VidLocation =newFileLocation ,titleVid = tags.split(",")[numVideo_i], description = video_name, tags=tags)
destroyAllFrames(FrameDirectory,FrameCount)
if __name__ == "__main__":
# main()
print datetime.datetime.now()
GenerateTheVideo(None, "C:/Users/pratika/Desktop/valve.avi", 2 ) #path where the video is located
print datetime.datetime.now()