lambda-video v1.0.11
FFMPEG for AWS Lambda
Run FFMPEG on AWS Lambda.
What
lambda-video
is static binary of ffmpeg utility, built for AWS Lambda. It supports all ffmpeg
commands.
Why
lambda-video
allows you to manipulate video files using ffmpeg command in AWS Lambda.
Inspiration
It is completely based on lambda-audio package, we have gone through the package made some minor changes & make it workable for Video manipulation
How
Installation
You can install this package from NPM by running following command:
npm install lambda-video --save
Usage
After you require node module in your code by adding:
const lambdaVideo = require('lambda-video')
See below for the examples.
ffmpeg command
Let's say that you want to extract audio from video & need sample rate 8000, with shell you will do like this:
ffmpeg -i video.mp4 -vn -ar 8000 -ac 2 -ab 192k -f mp3 audio.mp3
With lambda-video
you can do that by passing the command as a string, just without ffmpeg
part, like this:
lambdaVideo.ffmpeg('-i video.mp4 -vn -ar 8000 -ac 2 -ab 192k -f mp3 audio.mp3')
.then(response => {
// Do something when the file was converted
})
.catch(errorResponse => {
console.log('Error from the ffmpeg command:', errorResponse)
})
Or by passing the arguments as an array:
Keep in mind that AWS Lambda is read only and that the output path needs to be in /tmp folder.
Deployment
same aslambda-audio
, lambda-video
is working perfectly with Claudia.js library.
With Claudia.js, simply save lambda-video
as a dependency and then you can deploy your AWS Lambda function using standard claudia create
command.
example : If any .mp4 upload on bucket, the lambda function should be called to extract audio from video
var AWS = require('aws-sdk');
var fs = require("fs");
var mktemp = require("mktemp");
const path = require('path');
const lambdaVideo = require('lambda-video');
exports.handler = function(event, context) {
if(event.Records) {
var bucketName = event.Records[0].s3.bucket.name;
var fileName = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var params = {
Bucket: bucketName,
Key: fileName
};
var s3 = new AWS.S3();
s3.getObject(params, function(err, data) {
var basename = path.parse(fileName).name,temp_file;
temp_file = mktemp.createFileSync("/tmp/video.mp4");
fs.writeFileSync(temp_file, data.Body);
//Command to create 3 audio files of 10 sec with 8000hz
var command = '-i /tmp/video.mp4 -vn -ar 8000 -ac 2 -ab 192k -f mp3 /tmp/testaudio.mp3';
lambdaVideo.ffmpeg(command)
.then(response => {
fs.readFile('/tmp/audio.mp3', function(err, data){
s3.putObject({
Bucket: 'YOUR-OUTPUT-BUCKET',
Key: basename+'audio.mp3',
Body: data
}, function(err, data) {
if (err) {
console.log(err)
} else {
fs.unlinkSync('/tmp/video.mp4');
fs.unlinkSync('/tmp/testaudio.mp3');
console.log("Successfully uploaded data to myBucket/myKey");
}
})
})
}).catch(errorResponse => {
console.log('Error from the ffmpeg command:', errorResponse)
})
});
}
};
Show Your Support
We are new in this field & we are trying to build our own organisation.We willing to provide such solutions & packages to the world.Your contribution can help us to build new team & solutions. You can donate through paypal from link below
DONATE NOW
Thanks
Notes
- You should increase Max Timeout in aws lambda (default is 3, you can make it 30, for large file you may increase further), otherwise it will give timeout error
Thanks
https://www.npmjs.com/package/lambda-audio
Keyword
lambda-FFMPEG, FFMPEG, AWS Lambda video manipulation
License
GNU GENERAL PUBLIC LICENSE, Version 2 -- see LICENSE