How to get the output from fluent-ffmpeg screenshots as a buffer instead of writing it directly to the disk?
Asked Answered
S

1

9

I have a set of videos I want to take a screenshot from each of them, then do some processing on these generated images, and finally store them.

To be able to do the processing I need to get the screenshots as buffers.

this is my code

ffmpeg(videoFilePath)
.screenshots({
     count: 1,
     timestamps: ['5%'],
     folder: DestinationFolderPath,
     size: thumbnailWidth + 'x' + thumbnailHeight,
})
.on('err', function (error) {
     console.log(err)
});

as you see the output is being directly stored in the DestinationFolderPath. Instead of that I want to get the output as a buffer.

Serve answered 17/11, 2020 at 13:30 Comment(0)
G
-2

I'm not sure how to do that directly, but the screenshot is saved in a folder in your file system, so you could read the file from there and convert it into a buffer.

const thumbnailStream = createReadStream(thumbnailPath)
const thumbnailBuffer = stream2buffer(thumbnailStream)

There's a lot of ways of transforming a stream into a buffer, you can check it out in this question.

e.g. from this answer

function stream2buffer(stream) {
    return new Promise((resolve, reject) => {        
        const _buf = [];
        stream.on("data", (chunk) => _buf.push(chunk));
        stream.on("end", () => resolve(Buffer.concat(_buf)));
        stream.on("error", (err) => reject(err));
    });
} 

const thumbnailStream = createReadStream(thumbnailPath)
const thumbnailBuffer = await stream2buffer(thumbnailStream)

And createReadStream is imported from fs

Gilly answered 12/10, 2021 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.