Im able to upload an image to S3
. Now, if the file selected is .gif
, I want to be able to convert the .gif
file to .mp4
and upload the converted file to S3
. I am able to convert a .gif
to .mp4
with ffmpeg
only if I give the path of the file. How do I access the uploaded file from Multer
? Below is my code :
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var aws = require('aws-sdk');
var multer = require('multer');
var multerS3 = require('multer-s3');
var s3 = new aws.S3();
var ffmpeg = require('fluent-ffmpeg');
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'myBucket',
key: function (req, file, cb) {
console.log(file);
var extension = file.originalname.substring(file.originalname.lastIndexOf('.')+1).toLowerCase();
if(extension=="gif"){
console.log("Uploaded a .gif file");
ffmpeg(file) //THIS IS NOT WORKING
.setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe")
.output('./outputs/2.mp4') //TRYING TO UPLOAD LOCALLY, WHICH FAILS
.on('end', function() {
console.log('Finished processing');
})
.run();
}
cb(null, filename);
}
})
});
I'm trying to access the uploaded file like this: ffmpeg(file)
since file
is an argument passed in the multer
function.
My form :
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"> <br />
<input type="submit" value="Upload">
</form>
In which part of the process do I convert the file?
Please help. Many thanks.
You are trying to process a file locally that's on
s3
. The file needs to be your server's file system or at the very least be publicly available ons3
. So you have two options here.a) You could first upload all the files to the server where express is running (not on
s3
, first we store them temporarily). If the file is a.gif
, process it and upload the resulting.mp4
file, otherwise upload tos3
. Here's a working example:b) Alternatively if you're ok with making your
s3
files public you could upload them all usingmulter-s3
. Sinceffmpeg
also accepts network locations as input paths you can pass it thes3
location of your.gif
files and then upload the converted.mp4
files:For both examples, remember to create an
uploads/
folder and use youraws
configuration.