Skip to content

Initial commit for python conversion #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions chapter-3/python/3.4 - Transcode Video Lambda/lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#########################################
## Created by Sathiya Shunmugasundaram
## Serverless Architectures on AWS
## http://book.acloud.guru/
## Last Updated: Feb 28, 2017
#########################################

from __future__ import print_function

import json
import urllib
import boto3
import logging
import os

print('Loading function')

s3 = boto3.client('s3')
transcoder = boto3.client('elastictranscoder')
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
"""
This is the Lambda hander function
"""
logger.info('got event{}'.format(event))
# Get the bucket and key from the event
bucket = event['Records'][0]['s3']['bucket']['name']
logger.info("bucket is: %s", bucket)
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
#Replace whitespaces by +
sourceKey = ' '.join(key.split()).replace(" ","+")
logger.info("Formatted key is: %s", sourceKey)
#Remove extension for output key
outputKey = sourceKey.split(".")[0]
logger.info("outputKey is: %s", sourceKey)
#collect the pipeline id from Lambda environment variable
pipeline_id = os.environ['pipeline_id']
logger.info("pipeline_id is: %s", pipeline_id)
try:
job = transcoder.create_job(
PipelineId=pipeline_id,
Input={
'Key': sourceKey
},
Outputs=[
{
'Key': outputKey + '-1080p' + '.mp4',
'PresetId': '1351620000001-000001' #Generic 1080p
},
{
'Key': outputKey + '-720p' + '.mp4',
'PresetId': '1351620000001-000010' #Generic 720p
},
{
'Key': outputKey + '-web-720p' + '.mp4',
'PresetId': '1351620000001-100070' #Web Friendly 720p
}
],
)
except Exception as e:
print(e)
print('Error creating Transcoder JOb')
raise e