dopplesoldner
dopplesoldner

Reputation: 9469

How to convert Uint8Array video to frames in nodejs

I want to be able to extract jpegs from a Uint8 array containing the data for a mpeg or avi video.

The module ffmpeg has the function fnExtractFrameToJPG but it only accepts a filename pointing to the video file. I want to be able to extract the frames from the UInt8Array.

Upvotes: 1

Views: 1249

Answers (1)

christegho
christegho

Reputation: 304

One way to do it is to write the UInt8Array to a tmp file and then use the tmp file with ffmpeg to extract the frames:

const tmp = require("tmp");
const ffmpeg_ = require("ffmpeg");
function convert_images(video_bytes_array){
    var tmpobj = tmp.fileSync({ postfix: '.avi' }) 
    fs.writeFileSync(tmpobj.name, video_bytes_array);
    try {
        var process = new ffmpeg(tmpobj.name);
        console.log(tmpobj.name)
        process.then(function (video) {
            // Callback mode
            video.fnExtractFrameToJPG('./', { // make sure you defined the directory where you want to save the images
                frame_rate : 1,
                number : 10,
                file_name : 'my_frame_%t_%s'
            }, function (error, files) {
                if (!error)
                    tmpobj.removeCallback();
            });
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }
}

Another possibitlity is to use opencv after you save the UInt8Array to a tmp file. Another solution is to use stream and ffmpeg-fluent which would not require using tmp files.

Upvotes: 2

Related Questions