-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_serverless.py
More file actions
129 lines (105 loc) · 3.8 KB
/
process_serverless.py
File metadata and controls
129 lines (105 loc) · 3.8 KB
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
import moviepy.editor as mp
import moviepy.video as mp_vid
import boto3
import os
import json
import random
BUCKET_NAME = 'moviepy-video' # replace with your bucket name
KEY = 'ElephantsDream' # replace with your object key
LOGO = 'logo'
RESIZE = 128*2
CROP = 128*4
def read_from_s3(filename, ext):
session = boto3.Session()
s3 = session.client('s3')
s3_object = s3.get_object(Bucket=BUCKET_NAME, Key=filename + ext)
body = s3_object['Body'].read()
with open(filename + ext, "wb") as binary_file:
binary_file.write(body)
return filename
def write_to_s3(filename, ext):
with open(filename+ext, "rb") as f:
string = f.read()
encoded_string = string
s3 = boto3.resource("s3")
s3.Bucket(BUCKET_NAME).put_object(Key=filename+ext, Body=encoded_string)
def scaleDown(filename):
filename=read_from_s3(filename, ".mp4")
clip = mp.VideoFileClip(filename + ".mp4")
clip_resized = clip.resize(height=RESIZE) #(width/height ratio is conserved)
outputFilename = filename + "_resized"
clip_resized.write_videofile(outputFilename+".mp4")
write_to_s3(outputFilename, ".mp4")
return outputFilename
def crop(filename):
filename=read_from_s3(filename, ".mp4")
stream = mp.VideoFileClip(filename+".mp4")
outputFilename = filename + "_cropped"
mp_vid.fx.all.crop(stream, CROP, CROP, CROP//2, CROP//2)
# Stage IV: Saving
stream.write_videofile(outputFilename+".mp4")
write_to_s3(outputFilename, ".mp4")
return outputFilename
def mirror(filename):
filename=read_from_s3(filename, ".mp4")
stream = mp.VideoFileClip(filename+".mp4")
dirs=['X', 'Y']
ind=random.randint(0,1)
dir=dirs[ind]
if dir=='X':
stream=mp_vid.fx.all.mirror_x(stream)
else:
stream=mp_vid.fx.all.mirror_y(stream)
outputFilename = filename + "_mirror"
stream.write_videofile(outputFilename+".mp4")
write_to_s3(outputFilename, ".mp4")
return outputFilename
def watermark(filename, logoname):
logoname=read_from_s3(logoname, ".png")
filename = read_from_s3(filename, ".mp4")
video = mp.VideoFileClip(filename+".mp4")
logo = (mp.ImageClip(logoname+".png")
.set_duration(video.duration)
.resize(height=50) # if you need to resize...
.margin(right=8, top=8, opacity=0) # (optional) logo-border padding
.set_pos(("right","bottom")))
final = mp.CompositeVideoClip([video, logo])
outputFileName= filename+"_watermarked"
final.write_videofile(outputFileName+".mp4")
write_to_s3(outputFileName, ".mp4")
return outputFileName
def blackWhite(filename):
filename=read_from_s3(filename, ".mp4")
stream = mp.VideoFileClip(filename+".mp4")
outputFilename = filename + "_bw"
stream=mp_vid.fx.all.blackwhite(stream)
stream.write_videofile(outputFilename+".mp4")
write_to_s3(outputFilename, ".mp4")
return outputFilename
def rotate(filename):
filename=read_from_s3(filename, ".mp4")
stream = mp.VideoFileClip(filename+".mp4")
outputFilename = filename + "_rot"
angles=[0, 90, 180, 270]
ind=random.randint(0,3)
angle=angles[ind]
stream=mp_vid.fx.all.rotate(stream, angle)
stream.write_videofile(outputFilename+".mp4")
write_to_s3(outputFilename, ".mp4")
return outputFilename
def pipeline():
# Stage I: Cropping the Video
croppedFilename = crop(KEY)
# Stage II: Scaling Down the video
scaledDownFilename = scaleDown(croppedFilename)
# Stage III: Mirror on X/Y
mirrFilename = mirror(scaledDownFilename)
# Stage IV: Black and White
bwFilename = blackWhite(mirrFilename)
# Stage V: Rotate
rotateFilename = rotate(bwFilename)
# Stage VI: Adding the watermark
waterFilename = watermark(rotateFilename, LOGO)
if __name__=="__main__":
os.chdir("tmp/")
pipeline()