def render(self): # FACE DETECTION if 'IS_LOCAL' in os.environ: start_y_percent = 0.50 else: roi, dimensions, focus = FaceDetect.detect(self.path) start_y_percent = 0.00 if dimensions[1] > dimensions[0]: start_y_percent = roi[1] / float(dimensions[1]) # Animate Photo vf = [] if self.data["animation"] == "panup": f = filters.photoPanUp(start_y_percent, self.data["duration"]) self.vf.append("[0:v]%s[animated]" % f) elif self.data["animation"] == "pandown": f = filters.photoPanDown(start_y_percent, self.data["duration"]) self.vf.append("[0:v]%s[animated]" % f) self.addEffects() video_path = '/tmp/video-' + common.randomString(10) + '.mp4' cmd = [ common.FFMPEG_BIN, "-framerate 25 -i %s -t %.2f" % (self.path, self.data["duration"]), "-filter_complex \"%s\"" % ";".join(self.vf), "-map \"[final]\"", "-pix_fmt yuv420p -s 1280x720 -y %s" % video_path ] res = common.executeCmd(" ".join(cmd)) if res["error"] is True: return {'statusCode': 400, 'error': res["body"]} else: print res['result'] return {'statusCode': 200, 'video_path': video_path}
def create(self): # DOWNLOAD FONT f = Font(self.data['resourceUrl']) # RENDER TEXT JPG WITH IMAGICK self.path = '/tmp/text-' + common.randomString(10) + '.png' label_cmd = "./render_label.sh \"%s\" \"%s\" \"%s\" \"%s\" %i \"%s\"" % ( self.data['text'], f.ttf, self.data['color'], self.data['fontSize'] * self.comp.scale_factor, self.data['kerning'], self.path) res = common.executeCmd(label_cmd) if res["error"] is True: print('Error running IMAGEMAGIK') print( json.dumps({ 'command': res["body"].cmd, "code": res["body"].returncode, "error_output": res["body"].output })) # return { # 'statusCode': 400, # 'error': res["body"] # } else: if self.data['transitionIn'] == 'wipeLeftToRight': self.new_path = '/tmp/resource-' + common.randomString( 10) + '.mp4' resource_cmd = "./render_wipe.sh \"%s\" \"%.2f\" \"%.2f\" \"%s\"" % ( self.path, self.data['transitionInDuration'], self.comp.duration, self.new_path) common.executeCmd(resource_cmd) self.path = self.new_path self.data["transitionIn"] = "immediate" self.data["transitionInDuration"] = 0 self.shouldLoop = False
def render(self): self.addEffects() video_path = '/tmp/video-' + common.randomString(10) + '.mp4' # cmd = FFMPEG_BIN + " %s -filter_complex \"%s\" -map \"[v%i]\" -pix_fmt yuv420p -s 1280x720 -y %s" % (" ".join(self.inputs), ";".join(self.filters), len(self.inputs)-1, video_path) cmd = [ common.FFMPEG_BIN, " ".join(self.inputs), "-filter_complex \"%s\"" % ";".join(self.filters), "-map \"[comp]\"", "-pix_fmt yuv420p -s 1280x720 -y %s" % video_path ] res = common.executeCmd(" ".join(cmd)) if res["error"] is True: return {'statusCode': 400, 'error': res["body"]} else: return {'statusCode': 200, 'video_path': video_path}
def create(self): # DOWNLOAD IMAGE self.path = '/tmp/photo-' + common.randomString(10) # Download urllib.urlretrieve(self.data["resourceUrl"], self.path) # Trying another way to download - does not seem to make a difference # print "downloading photo file" # f = urllib2.urlopen(self.data["resourceUrl"]) # data = f.read() # with open(self.path, "wb") as code: # code.write(data) # print "photo saved to disk" # convert to jpg - need this step to make sure the images are encoded correctrly # Without this step getting glitches in the rendered videos # convert="%s -i %s -pix_fmt yuvj420p -y %s" % (common.FFMPEG_BIN, self.path, self.path+'.jpg') convert = "./tojpeg.sh %s" % self.path res = common.executeCmd(convert) self.path = self.path + '.jpg'
def PhotoPanDown(img, d): # FACE DETECTION roi, dimensions, focus = FaceDetect.detect(img) start_y_percent = 0.00 if dimensions[1] > dimensions[0]: start_y_percent = roi[1] / float(dimensions[1]) video_path = '/tmp/video-' + common.randomString(10) + '.mp4' cmd = common.FFMPEG_BIN + " -y -loop 1 -loglevel panic -i %s \ -c:v libx264 -pix_fmt yuv420p \ -filter_complex \ \"[0:v]crop=h=ih:w='if(gt(a,16/9),ih*16/9,iw)':y=0:x='if(gt(a,16/9),(ow-iw)/2,0)'[v01]; \ [v01]scale=-1:4000,crop=w=iw:h='min(iw*9/16,ih)':x=0:y='max((ih-oh)/6,%.2f*ih-((ih-oh)/6))+((t/%.2f)*(ih-oh)/6)',trim=duration=%.2f[v02]; \ [v02]zoompan=z='min(pzoom+0.0005,1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=1,setsar=sar=1:1[v]\" \ -map \"[v]\" -s \"1280x720\" %s" % (img, start_y_percent, d, d, video_path) res = common.executeCmd(cmd) if res["error"] is True: return {'statusCode': 400, 'error': res["body"]} else: return {'statusCode': 200, 'video_path': video_path}
# print("initiate font") self.url = url # default to packaged font # self.ttf = os.environ['LAMBDA_TASK_ROOT'] + "/assets/Bakerville.ttf" # try to download font self.download() def download(self): fontUrl = None try: data = urllib2.urlopen(self.url) for line in data: # files are iterable # print line m = re.search('url\((.+?)\)', line) if m: fontUrl = m.group(1) except urllib2.HTTPError, e: print('HTTPError = ' + str(e.code)) except urllib2.URLError, e: print('URLError = ' + str(e.reason)) except Exception: print('generic exception downloading font: ' + traceback.format_exc()) # Download font file if fontUrl is not None: font_file_path = '/tmp/font-' + common.randomString(10) + '.ttf' response = urllib2.urlopen(fontUrl) f = open(font_file_path, 'w') f.write(response.read()) f.close() self.ttf = font_file_path
def download(self): self.path = '/tmp/graphic-' + common.randomString(10) urllib.urlretrieve(self.data['resourceUrl'], self.path)