简体   繁体   English

使用 javascript 在客户端或后端合并两个视频

[英]Merge two videos on the client or backend side using javascript

I am working with React and Node.我正在使用 React 和 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?无论如何是可能的,我可以在我的 React 端使用一些画布或在后端端使用 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?因此,为此我一次播放所有 url,但我如何将它们播放成系列并保存下一个画布图像,直到前一个完成?

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).起初,用于视频播放器播放缓冲区的 .appendBytes() 方法是在 Flash 中发明的,以允许切换视频流(或不同质量的块)。 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).它还允许多个分辨率的视频块一个接一个地无缝播放,这在当时在任何其他播放器中都不起作用,包括 VLC(我认为 ffmpeg 也没有)。

HTML5 browsers have added an.appendBuffer() method to add video chunks to the currently playing video buffer. HTML5 浏览器添加了 .appendBuffer() 方法来将视频块添加到当前播放的视频缓冲区。

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.在服务器端,ffmpeg 无法像浏览器那样解决您的问题,因为您将不得不处理视频分辨率、编解码器、音频通道等可能不同的非常困难的极端情况。 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 MDN: https ://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer/appendBuffer

A quick search on the internet for some example use (pls check license yourself): https://github.com/joshuatz/mediasource-append-examples/blob/main/multi-file/segments/index.js#L48在互联网上快速搜索一些示例使用(请自己检查许可证): https ://github.com/joshuatz/mediasource-append-examples/blob/main/multi-file/segments/index.js#L48

This will solve all your problems related to video playback of multiple source files.这将解决与多个源文件的视频播放相关的所有问题。

If you want to do this on the backend, as stated in the comment, you will likely need to include ffmpg .如果您想在后端执行此操作,如评论中所述,您可能需要包含ffmpg There are some libraries though that make is simpler like fluent-ffmpeg有一些库虽然 make 更简单,比如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 .请注意,这是一个直接取自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.请务必阅读先决条件,因为它需要安装和配置 ffmpeg。 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.希望这能为您提供一个良好的起点。

Pretty sure most, if not all video manipulation libraries use ffmpeg under the hood.可以肯定的是,大多数(如果不是所有的话)视频处理库都在底层使用 ffmpeg。

Seeing as you're a react dev, you might appreciate Remotion to manipulate the videos as needed.鉴于您是 React 开发人员,您可能会喜欢Remotion根据需要操作视频。 It doesn't run in the frontend but does have some useful features.它不在前端运行,但确实有一些有用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM