I’m working in node js video streaming web application I would like to upload the video and get the buffer data using multer package and convert the buffer video data into m3u8 format and store it in server For converting m3u8 format am using ffmpeg Here I found the issue when handling the buffer data using ffmpeg It is possible to use input in ffmpeg as buffer data If it is possible kindly share reference link or code Thank you
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
FFmpeg can handle buffer data, but it requires a bit of setup when working within a Node.js environment. When you upload a video using Multer, you receive the video file as a buffer. To convert this buffer into an m3u8 format using FFmpeg, you can’t directly pass the buffer to FFmpeg. Instead, you need to handle the buffer in a way that FFmpeg can process it.
One common approach in such cases is to save the buffer to a temporary file and then use that file as the input for FFmpeg.
After the conversion, you can then delete the temporary file.
Here’s a basic outline of how you can achieve this:
fs module for this.const fs = require('fs');
const path = require('path');
const tempVideoPath = path.join(__dirname, 'tempVideo.mp4'); // Temp path for the video
// Assuming `videoBuffer` is the buffer you got from Multer
fs.writeFileSync(tempVideoPath, videoBuffer);
m3u8 format. Here’s how you might do it in Node.js:const { exec } = require('child_process');
const outputPath = path.join(__dirname, 'output.m3u8'); // Output path for the m3u8 file
const ffmpegCommand = `ffmpeg -i ${tempVideoPath} -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -f hls ${outputPath}`;
exec(ffmpegCommand, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
// After conversion, delete the temporary file
fs.unlinkSync(tempVideoPath);
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
This script first saves the buffer as a temporary file, then uses FFmpeg to convert that file to m3u8 format, and finally deletes the temporary file. That way you don’t store any temp files on the server after this is done.
Regarding direct buffer processing without a temporary file, FFmpeg doesn’t directly support buffers as input, so the above approach is a practical way to handle your use case in Node.js.
If you have any more questions or need further clarification, feel free to ask!
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.