I have a function streamtogif() with ffmpeg that converts a stream to a gif and returns a buffer. It works perfectly fine, but here I am, I'm using busboy, and i get the entire file as buffer. Finally, I'd like to pass this one to the ffmpeg to turn for ex. a mp4 to a gif. Here is my code: streamtogif():
function streamtogif(stream, begintime = 0, duration){ //Return promise buffer
return new Promise((resolve, reject)=>{
buffer = [] //prepare creation of the buffer for the gif
function addChunk(chunk){
this.buffer.push(chunk)
}
function getBuffer(cb){ //get buffer array
cb(this.buffer);
}
ffmpegstream = ffmpeg()
.outputOptions('-metadata', 'title=test')
.input(stream)
.fps(20)
.setStartTime(begintime)
.noAudio()
.videoCodec('gif')
.format('gif')
if(duration){ffmpegstream.duration(duration)} //only define duration if defined in function's parameters
ffmpegstream.on('start',()=>{
//console.log("starting")
this.buffer = []
})
.on('end', ()=>{
getBuffer((buff)=>{
finalBuffer = Buffer.concat(buff);
resolve(finalBuffer);
});
})
var ffstream = ffmpegstream.pipe(); //handle data
ffstream.on('data', function(chunk) {
addChunk(chunk);
})
ffmpegstream.run()
});
}
The code using this function : //i dont put everything, but the essential
finalBuffer = Buffer.concat(this.fileRead)
const bufferStream = new Stream.PassThrough();
bufferStream.end(finalBuffer);
streamtogif(bufferStream).then((buffer)=>{
upload = uploadpicture(buffer, "source/sportifeed").then((response)=>{ //success request
res.status(200).json({success: true, message: "Successfully uploaded !", url: response.data.link});
},(err)=>{ //error
console.log(err)
res.status(500).json({success: false, message: "Error happenned while uploading !"});
}).catch((err)=>{
console.log(err)
res.status(500).json({success: false, message: "Error happenned while uploading !"});
});
},(err)=>{
console.log(err);
})
When i pass a stream coming from ytdl-core for example, everything works fine, but in this case, I get an error :
An error occurred: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input Error: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input
at ChildProcess.<anonymous> (C:\Users\famil\Desktop\sportifeed\node_modules\fluent-ffmpeg\lib\processor.js:182:22) at ChildProcess.emit (events.js:198:13) at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12) ffmpeg version N-92722-gf22fcd4483
Copyright (c) 2000-2018 the FFmpeg developers built with gcc 8.2.1 (GCC) 20181201
When i print the stream from streamtogif function, I get this https://pastebin.com/NHmySWdm It do not seems empty
Thanks for your help
Ps: I tried with bufferstream too and tried :https://gist.github.com/chris9753/86121a07c4aa27fcd654d16133799d30 I tried to pass the file from busboy ? Didn't work, litterally didn't raise error