所以我正在研究一个概念,虽然我知道我可以使用 FFMPEG 来做我需要做的事情,但我希望有一种方法可以使用 nodejs Request 将视频加入到另一个视频的末尾。
以下代码适用于音频,我使用的视频文件是 MP4(请注意,我正在尝试将同一视频连接在一起,因此应该是 18 秒而不是 9 秒。
URL = https://storage.googleapis.com/ad-system/testfolder/Video/10%20second%20video%20FAIL.mp4
const express = require('express');
request = require('request');
const router = new express.Router();
router.get(':url(*)', (req, res) =>{
var url = req.params.url.substr(1);
res.set({
"Content-Type": "video/mp4",
'Transfer-Encoding': 'chunked'
});
var clients = [];
ASystem();
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log;(req.params.url);
/* START A FUNCTION */
function ASystem(){
var remote = "https://storage.googleapis.com/ad-system/testfolder/Video/10%20second%20video%20FAIL.mp4";
var streama = request.get(remote);
streama.on("connect", function() {
console.error(" connected!");
console.error(streama.headers);
});
streama.on("end", function() {
console.error(" END");
console.log(url);
if(url !== ""){getVideo();}
console.error(streama.headers);
// test = true;
});
// Fired after the HTTP response headers have been received.
streama.on('response', function(res) {
console.error(" Completed");
console.error(res.headers);
});
// When a chunk of data is received on the stream, push it to all connected clients
streama.on("data", function (chunk) {
// console.log(clients.length);
if (clients.length > 0){
for (client in clients){
clients[client].write(chunk);
};
}
});
};
/* END A FUNCTION */
/* FETCHING URLVIDEO */
function getVideo(){
var remote = url;
var streama = request.get(remote);
streama.on("connect", function() {
console.error(" Stream connected!");
//console.error(stream.headers);
});
streama.on("end", function() {
console.error(" Stream END");
// setTimeout(function(){ test = true; console.error('yep yep yep')},29000);
//console.error(stream.headers);
// test = true;
//getVideo();
});
// Fired after the HTTP response headers have been received.
streama.on('response', function(res) {
// console.error("Stream response!");
console.error(res.headers);
});
// When a chunk of data is received on the stream, push it to all connected clients
streama.on("data", function (chunk) {
// console.log(clients.length);
if (clients.length > 0){
for (client in clients){
clients[client].write(chunk);
};
}
});
}
/* END VIDEO*/
/* CORE SYSTEM */
clients.push(res);
/* END CORE SYSTEM */
});
module.exports = router;
最佳答案
我查看了一下,发现了一些可能有帮助的东西,NPM 上有一个名为 video-stitch
的包。 here's a link
有了它,您可以使用以下代码合并 2 个视频
'use strict';
let videoStitch = require('video-stitch');
let videoMerge = videoStitch.merge;
videoMerge()
.original({
"fileName": "FILENAME",
"duration": "hh:mm:ss"
})
.clips([
{
"startTime": "hh:mm:ss",
"fileName": "FILENAME",
"duration": "hh:mm:ss"
},
{
"startTime": "hh:mm:ss",
"fileName": "FILENAME",
"duration": "hh:mm:ss"
},
{
"startTime": "hh:mm:ss",
"fileName": "FILENAME",
"duration": "hh:mm:ss"
}
])
.merge()
.then((outputFile) => {
console.log('path to output file', outputFile);
});
希望这可以帮助!
关于node.js - nodejs 将两个视频连接在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237687/