Skip to content

Commit

Permalink
Added logging and VidStab calls
Browse files Browse the repository at this point in the history
- Added logging calls instead of print statements.
- Added the code to call VidStab to stabilize the video
- Still need to pipe stuff together so `in_path` and `out_path` actually
  exist.
  • Loading branch information
RobRoseKnows committed Sep 16, 2018
1 parent 25512dd commit 817ff94
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions stabVideo/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import os

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

OUTPUT_BUCKET = str(os.getenv('OUTPUT_BUCKET'))
KP_METHOD = str(os.getenv('KP_METHOD'))
BORDER_TYPE = str(os.getenv('BORDER_TYPE'))
Expand All @@ -18,11 +23,16 @@

def download_file(bucket, key):
name = get_tmp_file_from_key(key)
logger.info("Downloading s3://{}/{} to {}".format(bucket, key, name))
s3.download_file(bucket, key, name)
logger.info("Downloaded video file to {}".format(name))
return name

def upload_file(bucket, key, local_path):
logger.info("Uploading {} to s3://{}/{}".format(local_path, bucket, key))
response = s3.upload_file(local_path, bucket, key)
logger.info("Uploaded {} to s3://{}/{}".format(local_path, bucket, key))
logger.debug(response)
return response

# --------------- Other Helper Functions --------
Expand All @@ -35,26 +45,51 @@ def get_tmp_file_from_key(key):

# --------------- Main handler ------------------

VIDSTAB_ERROR = None

try:
from vidstab import VidStab
except ImportError as e:
logger.critical("Could not import VidStab")
VIDSTAB_ERROR = e

def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))

if VIDSTAB_ERROR is not None:
raise VIDSTAB_ERROR

try:

head_response = s3.head_object(Bucket=bucket, Key=key)
size = head_response['ContentLength']
content_type = head_response['ContentType']
if size <= MAX_SIZE:
# Do the stabilization
pass
if content_type[0:5] == "video":
stabilizer = VidStab(kp_method=KP_METHOD)
if BORDER_TYPE == "black":
stabilizer.stabilize(input_path=in_path,
output_path=out_path,
border_type=BORDER_TYPE,
border_size=BORDER_SIZE)
else:
stabilizer.stabilize(input_path=in_path,
output_path=out_path,
border_type=BORDER_TYPE)

else:
raise IOError("Object {} from bucket {} ".format(key, bucket) +
"is not a video.")
else:
raise IOError("Object {} from bucket {}".format(key, bucket) +
raise IOError("Object {} from bucket {} ".format(key, bucket) +
"exceeds max allowable size for stabilization.")

except IOError as e:
print(e)
logger.exception("IOError occurred during lambda handling")
raise e
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket) +
"Make sure your object and bucket exist and your bucket is in the same region as this function.")
logger.exception("Error processing object {} from bucket {}. ".format(key, bucket) +
"Make sure your object and bucket exist and your bucket is in the same region as this function.")
raise e

0 comments on commit 817ff94

Please sign in to comment.