Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Converting GIF buffer to MP4 buffer without writing to file firstΒ #567

@GordoRank

Description

@GordoRank

I am trying to convert animated GIF's to MP4 videos, on the fly, as the user requests them, after resizing/processing the GIF's first as requested.

After all processing on the GIF is complete I have its contents stored in a Buffer, and I need to finally output the resulting MP4 as a Buffer also.

Currently, I have to write the Buffer to a temporary file, perform the conversion, then read the converted file into a Buffer and finally pass it into the callback.

This is terribly inefficient and I would like to avoid writing to disk. How can I simply convert the GIF buffer into an MP4 buffer?

I tried using streamifier and passing in the GIF Buffer in order to use a readableStream as the source but I ended up getting an End of File Error.

Here is a stripped down version of the current method I am using which involves writing the files to disk before processing:

function makeMP4(gifBuffer, callback){
     fs.writeFile(input.gif,  gifBuffer, function(err) {
         ffmpeg(input.gif).outputOptions([
                        '-movflags faststart',
                        '-pix_fmt yuv420p',
                        '-vf scale=trunc(iw/2)*2:trunc(ih/2)*2'
        ])
        .inputFormat('gif')
        .on('end', function() {         
                        fs.readFile(output.mp4, function(err, mp4Buffer){
                fs.unlink('/tmp/' + src);
                fs.unlink('/tmp/' + dst);
                callback(null, mp4Buffer);                          
            });     
        }).save(output.mp4)         
    });
};

Any help would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions