import os
import subprocess
import shlex
import boto3
import json
import urllib.parse
from time import sleep
S3_DESTINATION_BUCKET = "s3-bucket-name"
SIGNED_URL_TIMEOUT = 600
def lambda_handler(event, context):
os.chdir('/tmp')
s3_source_bucket = event['Records'][0]['s3']['bucket']['name']
s3_source_key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
s3_source_basename = os.path.splitext(os.path.basename(s3_source_key))[0]
s3_source_key_new = os.path.basename(s3_source_key)
s3_destination_filename_xlarge = "xlarge"+s3_source_key_new
s3_destination_filename_large = "large"+s3_source_key_new
s3_destination_filename_medium = "medium"+s3_source_key_new
s3_destination_filename_small = "small"+s3_source_key_new
s3_client = boto3.client('s3')
s3_source_signed_url = s3_client.generate_presigned_url('get_object',
Params={'Bucket': s3_source_bucket, 'Key': s3_source_key},
ExpiresIn=SIGNED_URL_TIMEOUT)
s3_client.download_file(s3_source_bucket,s3_source_key,s3_source_key_new)
ffmpeg_cmd = f"/opt/bin/ffmpeg -i {s3_source_key_new} -vf scale=680:382 {s3_destination_filename_xlarge} "
os.system(ffmpeg_cmd)
resp = s3_client.put_object(Body=open(s3_destination_filename_xlarge,"rb"), Bucket=S3_DESTINATION_BUCKET, Key="converted/xlarge/"+s3_source_key_new)
ffmpeg_cmd = f"/opt/bin/ffmpeg -i {s3_source_key_new} -vf scale=540:304 {s3_destination_filename_large} "
os.system(ffmpeg_cmd)
resp = s3_client.put_object(Body=open(s3_destination_filename_large,"rb"), Bucket=S3_DESTINATION_BUCKET, Key="converted/large/"+s3_source_key_new)
ffmpeg_cmd = f"/opt/bin/ffmpeg -i {s3_source_key_new} -vf scale=440:248 {s3_destination_filename_medium} "
os.system(ffmpeg_cmd)
resp = s3_client.put_object(Body=open(s3_destination_filename_medium,"rb"), Bucket=S3_DESTINATION_BUCKET, Key="converted/medium/"+s3_source_key_new)
ffmpeg_cmd = f"/opt/bin/ffmpeg -i {s3_source_key_new} -vf scale=240:135 {s3_destination_filename_small} "
os.system(ffmpeg_cmd)
resp = s3_client.put_object(Body=open(s3_destination_filename_small,"rb"), Bucket=S3_DESTINATION_BUCKET, Key="converted/small/"+s3_source_key_new)
s3 = boto3.resource('s3')
return {
'statusCode': 200,
'body': json.dumps('Processing complete successfully')
}
Comments
Post a Comment