An AWS Lambda function that resizes videos and outputs thumbnails using FFmpeg. This function is meant for short-duration videos. If you need to transcode long videos, check out AWS Elastic Transcoder.
- Install node.js, preferably through nvm. Lambda uses a specific version of node, so it would be best use the same version--especially when installing dependencies via npm.
- Clone this repo
- Run
npm install
- Create a config.json file (see below), or modify the index.js file for your purposes
- Create your function code's S3 bucket (this will hold your zipped lambda function) (optional if you already have a bucket for this)
- Run Gulp (see below)
- Create and upload your Lambda function
- Invoke the lambda function by uploading a video to your source bucket or run it manually in the AWS console.
See Adam Neary's (@adamrneary) A Gulp Workflow for Amazon Lambda
There are a few more Gulp tasks to download, extract, and copy the 64-bit linux build of FFmpeg from John Van Sickle's FFmpeg builds site.
The default gulp task will run all of the tasks. This will result in the following actions:
- Cleans the build directory, dist directory, and dist.zip
- Downloads, extracts, and copies FFmpeg and FFprobe to the dist directory
- Copies the index.js and config.json file to the dist directory
- Runs npm install in the dist directory (excludes dev dependencies)
- Zips everything into dist.zip. This file is what you will upload to AWS Lambda for execution.
- Uploads the function to AWS (must create config.json first and have your AWS credentials set as environment variables).
- Creates or updates the CloudFormation stack which includes:
- The lambda function's execution role and policy
- The lambda function
- The source bucket (where videos are uploaded to), including the notification configuration
- The destination bucket (where videos and thumbnails go after they are processed)
- User uploads a video file to the source bucket
- A notification event trigger the lambda function
- The lambda function downloads the video file from the source bucket
- Streams it through FFmpeg
- Outputs a scaled video file and a thumbnail image
- Uploads both files to the destination bucket
See config_sample.json. config.json will be copied after npm install.
At minimum, you need to modify:
functionBucket
- The name of the bucket where your the lambda function code will be uploaded to. It's necessary for CloudFormation.sourceBucket
- The name of the bucket that will receive the videos and send them to the lambda for processing.destinationBucket
- The name of the bucket that will be used to store the output video and thumnail image.
Edit test_event.json, by modifying the bucket name and object key, and run npm test
if you want to run a test locally.
Run gulp update
after modifying anything in the Lambda function if you've already created the stack. This will rebuild dist.zip
, upload it to S3, and update the lambda function created during the CloudFormation stack creation.
- Gzipping videos will cause Safari errors in playback. Don't enable gzip unless you don't care about supporting Safari.
- The object key from the event is URL encoded. Spaces in the filenames might be replaced with
+
so be aware of this and handle errors appropriately. If you try to download the file with the AWS SDK for JavaScript like in this example, without handling this, it will throw an error. - Not handling errors with
context.fail(error)
will cause the function to run until the timeout is reached. - Pick the largest memory allocation. This is mostly CPU bound, but Lambda bundles memory and CPU allocation together. Memory size is 1536 by default. Testing with different videos and sizes should give you a good idea if it meets the requirements. Total execution time is limited!
Submit issues if you find bugs or something is unclear. Pull requests are even better, especially if you can make something more generalized.
If you use it, star it!