Giter Club home page Giter Club logo

aws-lambda-ffmpeg's Introduction

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.

Dependency Status devDependency Status

Setup

  1. 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.
  2. Clone this repo
  3. Run npm install
  4. Create a config.json file (see below), or modify the index.js file for your purposes
  5. Create your function code's S3 bucket (this will hold your zipped lambda function) (optional if you already have a bucket for this)
  6. Run Gulp (see below)
  7. Create and upload your Lambda function
  8. Invoke the lambda function by uploading a video to your source bucket or run it manually in the AWS console.

Gulp

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)

Lambda Function Process Overview

  1. User uploads a video file to the source bucket
  2. A notification event trigger the lambda function
  3. The lambda function downloads the video file from the source bucket
  4. Streams it through FFmpeg
  5. Outputs a scaled video file and a thumbnail image
  6. Uploads both files to the destination bucket

Configuration

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.

Testing

Edit test_event.json, by modifying the bucket name and object key, and run npm test if you want to run a test locally.

Updates/Modification to the lambda function or configuration

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.

Gotchas

  • 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!

Contributing

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!

aws-lambda-ffmpeg's People

Contributors

binoculars avatar crgwbr avatar

Watchers

 avatar  avatar  avatar

Forkers

kmuncie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.