1

Is it possible to store ffmpeg output directly to s3 without downloading it in local or any other storage? Below is my understanding of ffmpeg which converts format of video. I have done conversion part but i need to store it's output directly to s3 bucket so anyone have idea regarding this problem ?

const AWS = require('aws-sdk');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const axios = require('axios');


const s3 = new AWS.S3({
    endpoint: 's3-ap-south-1.amazonaws.com',   // Put you region
    accessKeyId: S3_ACCESS_KEY_ID,       // Put you accessKeyId
    secretAccessKey: S3_ACCESS_SECRET_KEY,   // Put you accessKeyId
    Bucket: S3_BUCKET_NAME,         // Put your bucket name
    signatureVersion: 'v4',
    region: 'ap-south-1'           // Put you region
});

var params = {
    Bucket: S3_BUCKET_NAME,
    Delimiter: '',
    Prefix: S3_STORE_PATH
};
s3.listObjects(params, function (err, data) {
    if (err) throw err;
    console.log(data);
    data.Contents.forEach(function (obj, index) {
        const file_name = obj.Key;
        const type = "mp4";
        console.log(obj.Key)
        const url = s3.getSignedUrl('getObject', {
            Bucket: S3_BUCKET_NAME,
            Key: obj.Key,
            Expires: signedUrlExpireSeconds
        });
        console.log("SIGNED URL= ", url);
        const filename = file_name.split('.').slice(0, -1).join('.');
        const localFileOutput = `${filename}.${type}`;
        // const localFileOutput = `${bucket_url}${filename}.${type}`;
        console.log(localFileOutput);
        const key = `${filename}.${type}`;
        const convert_video = async (req,res) => {
            await new Promise((resolve, reject) => {
                ffmpeg().input(url)
                    .toFormat('mp4')
                    .output(localFileOutput)
                    .on('end', async () => {
                        const params = {
                            Bucket: S3_BUCKET_NAME,
                            Key: key,
                            Body: localFileOutput
                        }
                        // const fileContent = await fs.readFileSync(localFileOutput);
                        await s3.putObject(params).promise();
                        resolve();
                    }).run();
            });

            // res.send("success")
        }
        convert_video();
    });
});
Parth Shah
  • 51
  • 7
  • Does this answer your question? [How can I pipe from fluent-ffmpeg to AWS s3?](https://stackoverflow.com/questions/61620261/how-can-i-pipe-from-fluent-ffmpeg-to-aws-s3) – Ervin Szilagyi Sep 27 '21 at 14:20
  • For the general case: No. Some containers require seeking during output, which isn't possible with a stream-to-S3 solution. If you can control the container and options for it, it might be possible. – Anon Coward Sep 27 '21 at 20:13
  • @ErvinSzilagyi Yes you're correct they did same thing but for mp3 format and in my case i need mp4 format and for that format it's not working as they mentioned – Parth Shah Sep 29 '21 at 10:14

0 Answers0