I was working on FFmpeg for a task related to audio files, and it worked fine on my local machine. Now, I wanted to bundle it up in AWS Lambda but struggled a lot. FFmpeg was not directly able to work out inside AWS Lambda. I started digging into a lot of articles but could not find the proper solution to it. Well, I didn’t give up and found the solution that worked great. With the help of some NPM, I could tackle this issue.
So today, we are going to learn how to implement FFmpeg package deployment inside AWS Lambda step by step.
OS Dependencies
AWS Lambda is working under Linux environment on the background so if you have developed and deployed the project from any other platform like Mac or Windows to lambda, it is not going to work for sure. It is 100% Linux platform dependent (I am a Windows user but still, only for this task's purpose, I installed Virtual Machine on my Windows & run Ubuntu inside it — FYI if need — link).
Prerequisites - (Install Ubuntu/Linux platform)
- NVM (Node Version Manager)
This is nothing but a simple version manager which allows you to manage multiple versions of Node.js at one place and you can write your Node.js code under any installed version, depending on your requirement.
- NodeJS -
Install it from NVM.
Warning: Do not install Node.js directly from the command line. You must install it only and only from NVM, otherwise while deploying the code, you will face a lot of issues which you could not even understand.
- NPM (Node Package Manager) - By default installed with NodeJS.
- AWS Console Account with Lambda and S3 Service enabled.
Required NPM Packages
@ffmpeg-installer/ffmpeg
npm install --save @ffmpeg-installer/ffmpeg
fluent-ffmpeg
npm install --save fluent-ffmpeg
CORS
The purpose of this package is described at this Link. (This package is optional and is useful only if you want to enable this AWS Lambda with AWS API Gateway for Endpoint trigger).
npm install --save cors
package.json
Business Logic
Here, I am trying to convert the audio bytes string into an audio .wav file by using FFmpeg package with the configuration of SampleRate 16Hz, Channel Mono, Quality 5, extension .wav.
The code snippet is available here.
Once you are done with the local development, it is time to deploy the Google Cloud function on the local environment.
Warning: Currently, AWS Lambda supports Node.js version 6 and 8. So, here, we need to use our NVM with the following command.
- nvm current
- nvm install 6
- nvm use 6
- nvm install 8
- nvm use 8
Once you set the targeted Node.js version and you are ready with your FFmpeg code, you are good to go on AWS Lambda. Just rebuild your Node Project so that your Node_Module will generate the targeted dependencies and bundle it at one place.
npm rebuild --force
- Just zip the root project.
- Upload zip inside AWS S3.
- Configure your AWS Lambda with your uploaded S3 bucket path.
- If needed, customise your configurations & parameters.
- Run your Lambda.
and, you are done.
Conclusion
In this write-up, we saw that FFmpeg deployment is possible inside AWS Lambda without any issues. So, go ahead guys!
If you have found this helpful, please hit the like button and share it on social media.