-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
133 lines (109 loc) · 4.86 KB
/
lambda.py
File metadata and controls
133 lines (109 loc) · 4.86 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
130
131
132
133
import boto3
import json
import os
import subprocess
import uuid
from botocore.client import ClientError
# Converts a video file to a GIF using ffmpeg.
def convert_video_to_gif(source_video_path, gif_output_path):
# ffmpeg command for converting video to GIF with specified options.
ffmpeg_command = [
'ffmpeg', '-i', source_video_path, # Input file
'-t', '2.5', # Duration of output GIF
'-vf', 'fps=5,scale=200:-1', # Frame rate and scale
gif_output_path # Output file path
]
try:
subprocess.run(ffmpeg_command, check=True)
print("GIF conversion successful")
return True
except subprocess.CalledProcessError as e:
print(f"Error converting video to GIF: {e}")
return False
# Downloading file from S3 to local path.
def download_from_s3(bucket, key, download_path):
s3_client = boto3.client('s3')
try:
s3_client.download_file(bucket, key, download_path)
return True
except ClientError as e:
print(f"Error downloading video from S3: {e}")
return False
# Uploading file from local file system to the specified S3 location.
def upload_to_s3(file_path, bucket, key):
s3_client = boto3.client('s3')
try:
s3_client.upload_file(file_path, bucket, key)
print("Upload successful")
return True
except ClientError as e:
print(f"Error uploading GIF to S3: {e}")
return False
# Deletes local files to clean up the local file system.
def cleanup_local_files(*file_paths):
for file_path in file_paths:
try:
os.remove(file_path)
print(f"Deleted local file: {file_path}")
except Exception as e:
print(f"Error deleting local file {file_path}: {e}")
# Main entry point of lambda function
def lambda_handler(event, context):
assetID = str(uuid.uuid4())
sourceS3Bucket = event['Records'][0]['s3']['bucket']['name']
sourceS3Key = event['Records'][0]['s3']['object']['key']
sourceS3 = 's3://'+ sourceS3Bucket + '/' + sourceS3Key
sourceS3Basename = os.path.splitext(os.path.basename(sourceS3))[0]
destinationS3 = 's3://' + os.environ['DestinationBucket']
destinationS3basename = os.path.splitext(os.path.basename(destinationS3))[0]
mediaConvertRole = os.environ['MediaConvertRole']
region = os.environ['AWS_DEFAULT_REGION']
statusCode = 200
body = {}
# Use MediaConvert SDK UserMetadata to tag jobs with the assetID
# Events from MediaConvert will have the assetID in UserMedata
jobMetadata = {'assetID': assetID}
print (json.dumps(event))
local_video_path = f"/tmp/{os.path.basename(sourceS3Key)}"
local_gif_path = f"/tmp/{os.path.splitext(os.path.basename(sourceS3Key))[0]}.gif"
gif_s3_key = f"{os.path.splitext(sourceS3Key)[0]}.gif"
# Check and download video from S3.
if not download_from_s3(sourceS3Bucket, sourceS3Key, local_video_path):
return {'statusCode': 500, 'body': json.dumps('Failed to download video from S3')}
# Convert the downloaded video to GIF.
if not convert_video_to_gif(local_video_path, local_gif_path):
return {'statusCode': 500, 'body': json.dumps('Failed to convert video to GIF')}
# Upload the converted GIF to S3.
if not upload_to_s3(local_gif_path, destinationS3, gif_s3_key):
return {'statusCode': 500, 'body': json.dumps('Failed to upload GIF to S3')}
# Local storage clean-up
cleanup_local_files(local_video_path, local_gif_path)
try:
# Job settings are in the lambda zip file in the current working directory
with open('main.json') as json_data:
jobSettings = json.load(json_data)
print(jobSettings)
# get the account-specific mediaconvert endpoint for this region
mc_client = boto3.client('mediaconvert', region_name=region)
endpoints = mc_client.describe_endpoints()
# add the account-specific endpoint to the client session
client = boto3.client('mediaconvert', region_name=region, endpoint_url=endpoints['Endpoints'][0]['Url'], verify=False)
# Update the job settings with the source video from the S3 event and destination
# paths for converted videos
jobSettings['Inputs'][0]['FileInput'] = sourceS3
print("till here")
print('jobSettings:')
print(json.dumps(jobSettings))
# Convert the video using AWS Elemental MediaConvert
job = client.create_job(Role=mediaConvertRole, UserMetadata=jobMetadata, Settings=jobSettings)
print (json.dumps(job, default=str))
except Exception as e:
print ('Exception: %s' % e)
statusCode = 500
raise
finally:
return {
'statusCode': statusCode,
'body': json.dumps(body),
'headers': {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'}
}