Question

Ffmpeg can handle buffer data

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


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 5, 2024

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:

  1. Save Buffer to a Temporary File: When you receive the video buffer from Multer, write it to a temporary file on your server. You can use Node.js’s 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);
  1. Convert the Video Using FFmpeg: Once you have the temporary video file, you can use FFmpeg to convert it to the 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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.