Use ffmpeg or ffprobe with ffmpeg-aws-lambda-layer

3 minute read

Use ffmpeg or ffprobe with ffmpeg-aws-lambda-layer

Introduction

This section describes the procedure for building a ffmpeg execution environment that is convenient for formatting and resizing videos and generating thumbnails using AWS Lambda functions and layers.
Here, we will use ffmpeg-aws-lambda-layer. By creating an AWS Lambda layer that includes ffmpeg and ffprobe separately from the Lambda function, these The command is called under / opt in the Lambda function execution environment. It can be called like a library without including the binary for command execution in the deployment package.

With Amazon Elastic Transcoder, you can easily format and resize videos, but it is expensive and requires machine power. If it is not a conversion, I think it is easier and better to process it with Lambda.
Transfer the ATOM Cam video used in the previously written article (Set up smart home camera ATOM Cam) to S3 via NAS and use Amazon Elastic Transcoder. When I created a process to convert the video format, I quickly consumed thousands of yen. It seems better to use ffmpeg for this purpose.

What you can do with this sample

If you deploy the sample project provided in ffmpeg-aws-lambda-layer, ffmpeg-layer-example-uploadbucket-xxxx in S3 And ffmpeg-layer-example-resultbucket-xxxx (xxxx is a random string, the same applies below) will be generated, and the connection with the Lambda function and the IAM role setting will be done automatically.
When a video file is uploaded to ffmpeg-layer-example-uploadbucket-xxxx, a thumbnail image is automatically generated in ffmpeg-layer-example-resultbucket-xxxx.

Things to prepare

-What is written in ffmpeg-aws-lambda-layer

- Unix Make environment
- AWS command line utilities (just for deployment)

This time, we confirmed the operation in the following environment.

$ sw_vers 
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H2
$ aws --version
aws-cli/2.0.57 Python/3.7.4 Darwin/19.6.0 exe/x86_64

–AWS account
–You need a role that can operate Lambda, S3, CloudFormation. There is no problem if you have Administrator privileges.

Reference information

-Create thumbnails from videos using FFmpeg on AWS Lambda
-Analyze video with ffprobe
-Encode with AWS Lambda (Node.js) + ffmpeg
-Try to generate and convert audio files with custom AWS Lambda runtime

Preparation

git clone ffmpeg-aws-lambda-layer

git clone https://github.com/serverlesspub/ffmpeg-aws-lambda-layer.git

Preparing a temporary S3 bucket

Creates a bucket in the same region as the region specified in the default profile in ~ / .aws / credentials.

Deploy sample project

Specify the bucket name of the temporary S3 bucket for the value of the DEPLOYMENT_BUCKET option.

cd ffmpeg-aws-lambda-layer
make deploy-example DEPLOYMENT_BUCKET=<BUCKET NAME>

When you execute the make command, the following log will be displayed.

cd example && \
		make deploy DEPLOYMENT_BUCKET=<BUCKET NAME> LAYER_STACK_NAME=ffmpeg-lambda-layer
mkdir -p build
aws cloudformation package --template-file template.yaml --output-template-file output.yaml --s3-bucket net.rev-system.nas
Uploading to d5886731828e201e755325e950d28eab  1742 / 1742.0  (100.00%)
Successfully packaged artifacts and wrote output template to file output.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /tmp/ffmpeg-aws-lambda-layer/example/output.yaml --stack-name <YOUR STACK NAME>
aws cloudformation deploy --template-file output.yaml --stack-name ffmpeg-layer-example --capabilities CAPABILITY_IAM --parameter-overrides LambdaLayer=arn:aws:lambda:ap-northeast-1:************:layer:ffmpeg:1

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - ffmpeg-layer-example
aws cloudformation describe-stacks --stack-name ffmpeg-layer-example --query Stacks[].Outputs --output table
--------------------------------------------------------------------------------------------
|                                      DescribeStacks                                      |
+-------------------+----------------+-----------------------------------------------------+
|    Description    |   OutputKey    |                     OutputValue                     |
+-------------------+----------------+-----------------------------------------------------+
|  Upload S3 bucket |  UploadBucket  |  ffmpeg-layer-example-uploadbucket-xxxxxxxxxxx      |
|  Results S3 bucket|  ResultsBucket |  ffmpeg-layer-example-resultsbucket-yyyyyyyyyyyy    |
+-------------------+----------------+-----------------------------------------------------+

Here, the following resources are created.

Resource type Name example
Lambda function ffmpeg-layer-example-ConvertFileFunction-nnnnnnnnnnnn
Lambda layer ffmpeg
S3 bucket(For upload) ffmpeg-layer-example-uploadbucket-xxxxxxxxxxx
S3 bucket(Converted) ffmpeg-layer-example-resultsbucket-yyyyyyyyyyyy
CloudFront stack ffmpeg-layer-example
IAM roll ffmpeg-layer-example-ConvertFileFunctionRole-zzzzzzzzzzzz

Thumbnail generation

Upload the appropriate video file to the S3 bucket ffmpeg-layer-example-uploadbucket-xxxxxxxxxxx.
After a while, the thumbnail image will be output to ffmpeg-layer-example-resultsbucket-yyyyyyyyyyyy.

Output other than thumbnails

If you want to format the video in a different format or output the meta information of the video file with ffprobe, you can try various things by changing ffmpeg-aws-lambda-layer / example / src / index.js and redeploying.

Create only Lambda layer

If you want to deploy only the Lambda layer without deploying the uploaded / converted S3 bucket or Lambda function, execute the following command.
Specify the bucket name of the temporary S3 bucket for the value of the DEPLOYMENT_BUCKET option.

cd ffmpeg-aws-lambda-layer
make deploy DEPLOYMENT_BUCKET=<BUCKET NAME>

Only the Lambda layer is deployed, so you can use it by adding this layer to any Lambda function.