Skip to content

Instantly share code, notes, and snippets.

@mustafaileri
Created April 24, 2020 20:30
Show Gist options
  • Save mustafaileri/4814e919149de7ed8c2b7f4fc1d30562 to your computer and use it in GitHub Desktop.
Save mustafaileri/4814e919149de7ed8c2b7f4fc1d30562 to your computer and use it in GitHub Desktop.
sample serverless app with ffmpeg and python
import json
import os
import boto3
def lambda_handler(event, context):
try:
s3 = boto3.client('s3')
"""Downlod video from private S3"""
s3.download_file('serverless-test-2020', 'Big_Buck_Bunny_1080_10s_1MB.mp4', '/tmp/test-video.mp4')
"""Generate a thumbnail and save"""
command='/opt/bin/ffmpeg -i /tmp/test-video.mp4 -ss 00:00:01 -vframes 1 -filter:v scale="280:-1" /tmp/test-output.png -y'
os.system(command)
"""Upload the thumbnail to the public S3"""
target_bucket='serverless-test-public'
target_key='test-output.png'
response = s3.upload_file('/tmp/test-output.png', target_bucket, target_key, ExtraArgs={'ACL':'public-read'})
"""Generate public object URL"""
location = s3.get_bucket_location(Bucket=target_bucket)['LocationConstraint']
url = "https://s3-{}.amazonaws.com/{}/{}".format(location, target_bucket, target_key)
return {
'statusCode': 200,
'body': json.dumps({'thumbnail':url})
}
except Exception:
return {
'statusCode': 500
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment