Search code examples
node.jsamazon-web-servicesamazon-s3multerfluent-ffmpeg

How can I store thumbnails for videos I upload on AWS at the same time in nodejs without lambda function?


I am working on thumbnail creation with ffmpeg. I have successfully created thumbnail for uploaded video on AWS.

For upload video/videos I have flow like:

  • I have one API used for multiple type file uploading. Also I have used upload.array to pass multiple file at the same time.

  • This API is called by Front-end developers to get the Uploaded files detail: Response of that API is like: `

    {
     "success": true,
     "message": "Files uploaded successfully",
     "data": [
         {
             "fileName": "1628503061631.mp4",
             "mimeType": "video/mp4"
         }
     ]
    }
    
  • This file name I receive in request.body whenever Edit/Add Post media API will be used also it will be stored in DB table too.

  • I have configured ffmpeg as per this tutorial: https://www.youtube.com/watch?v=8vue0_vs2AI

  • The problem I am facing is, I am not able to upload thumbnail along with video upload

  • After video is uploaded in S3, that time only I am able to get its location and I create thumbnail from that.

Is there any way to give AWS S3 path direct to the second argument of the Thumbnail function?

const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
var ffprobe = require('ffprobe-static');
ffmpeg.setFfprobePath(ffprobe.path);

...
 ffmpeg({ source: req.files[k].location })

 .on('filenames', (filenames) => {
     console.log("Created file name", filenames);
  })
  .on('end', () => {
  return res.status(200).json({ success: true, message: "Files uploaded successfully",   data: uploadedMedia })
  })
  .on('error', (err) => {
     console.log("Error", err);
  })
  .thumbnail({
    filename: 'thumbnail_' + req.files[k].key.split('.')[0],count: 1
    }, './uploads/thumbnails/<<HERE I WANT TO ADD S3 path');

Solution

  • This solution works perfect for making thumbnail from videos and upload it to S3.

    ffmpeg({ source: 'location of the file' })
    .on('filenames', (filenames) => {
       // created an array of thumbnail files as I have multiple media upload
    })
    .on('end', () => {
       for (let i in arrayofthumbnailfiles) {
           const uploadFile = () => {
               filePath = 'path where I have stored my thumbnails before s3 upload'
               const params = {
                  Bucket:// pass your bucket name
                  Key: // pass your key name,
                  Body: fs.createReadStream(filePath),
                  ContentType: // media content i.e. png,
                  ACL:// add ACL value here
               };
               // here I have uploaded thumbnails in s3
               s3.upload(params, function (s3Err, data) {
               if (s3Err) throw s3Err
               if (data) {
                  // Unlink file - removed from local storage
               }
             });
           };
        uploadFile();
       }
      })
     .on('error', (err) => {
         console.log("Error", err);
      })
    .thumbnail({filename: //set your filename here,count: 1}, 'localstorage path you want to save thumbnails temporary');