3

I am working with React and Node. My project is having requirement to merge videos and play it on the player. Is possible anyhow, I can do it either on my React side using some canvas or on the back end side using some module other than ffmpeg?

Just want to show preview nothing else is it possible?

Any help would be much appriciated

Right now what I am doing is playing the videos one by one on the player

{vidoes.map((url) => <ReactPlayer src={url}/>)}

Now what I want to do is to draw the images on the canvas. So for that I am playing all the urls at once but how can I play them into series and save the next canvas images until the before one completes?

Profer
  • 553
  • 8
  • 40
  • 81
  • 2
    Well if you're just playing it on the front end in the browser, you don't need to actually merge the files. Just start playing one video as soon as the other is finished. If they need to be able to download one single file containing both videos combined, then yes you'll need to do that on the backend with some sort of media tool like ffmpeg – Jayce444 Dec 09 '22 at 06:14
  • can you share the git repo so that we can test if it works or not? – Yilmaz Dec 17 '22 at 16:13
  • Why do you want to avoid using ffmpeg? – I0_ol Dec 19 '22 at 20:11
  • Also it would help to know more about the videos you're wanting to merge. Are they mp4, webm, ts, etc... Certain types of files are easier to merge than others. – I0_ol Dec 19 '22 at 20:22

3 Answers3

4

To achieve continous playing in browsers for multiple input video files there’s no need for server side processing. Static file serving for multiple video files (we’ll call them chunks from now on) is enough.

At first the .appendBytes() method for the playing buffer of a video player was invented in Flash to allow for switching video streams (or different quality chunks). It was also particularly useful for live video where the next video file doesn’t exist when the playing starts. It also allowed multiple resolution video chunks to play one after the other seamelessly, which, at the time, didn’t work in any other player, including VLC (and I think ffmpeg didn’t have it either).

HTML5 browsers have added an .appendBuffer() method to add video chunks to the currently playing video buffer.

It allows you to hands-on pre-load whatever content you need with full control of what gets played and when (you are in control of buffering and what comes next at all times).

You can start dumping video chunks into the playing buffer at any time.

On the server side ffmpeg cannot solve your problem in the same way the browser can as you will have to handle very difficult corner cases where the video resolution, codecs, audio channels, etc. might differ. Also, a browser only solution is vastly superior to doing remuxing or video encoding on the server.

MDN: https://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer/appendBuffer

This will solve all your problems related to video playback of multiple source files.

oxygen
  • 5,891
  • 6
  • 37
  • 69
3

If you want to do this on the backend, as stated in the comment, you will likely need to include ffmpg. There are some libraries though that make is simpler like fluent-ffmpeg

assuming you already have the files on your node server, you can use something like:

const ffmpeg = require('fluent-ffmpeg');

ffmpeg('/path/to/input1.avi')
  .input('/path/to/input2.avi')
  .on('error', function(err) {
    console.log('An error occurred: ' + err.message);
  })
  .on('end', function() {
    console.log('Merging finished !');
  })
  .mergeToFile('/path/to/merged.avi', '/path/to/tempDir');

note that this is a simple example taken directly from https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#mergetofilefilename-tmpdir-concatenate-multiple-inputs.

Make sure you read through the prerequisites since it requires ffmpeg to be installed and configured. There are other aspects that you might need to consider, such as differing codecs and resolutions. Hopefully this gives you a good starting point though.

Daniel
  • 34,125
  • 17
  • 102
  • 150
  • Hi Daniel thanks for the answer... How much it will take to just merge the three clips having 10 mb each? – Profer Dec 14 '22 at 06:04
  • 1
    I don't understand your question _"How much it will take to just merge the three clips having 10 mb each?"_. But whether you mean the total time or resources needed in the end the answer is _it depends_. It will depend on the amount and type of compression, resolution and hardware used to process the videos. Also, there's the time to upload to server that may need to be included. In the end you'll get best idea if you run a test. – Daniel Dec 14 '22 at 16:19
  • If you're only interested in time to merge videos, you can quickly do a test by just running the ffmpeg without the server component on the hardware you'd be using. You can see, as an example, https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg for the code to do so – Daniel Dec 14 '22 at 16:20
2

Pretty sure most, if not all video manipulation libraries use ffmpeg under the hood.

Seeing as you're a react dev, you might appreciate Remotion to manipulate the videos as needed. It doesn't run in the frontend but does have some useful features.

Andrew Gillis
  • 3,250
  • 2
  • 13
  • 15