5

So I am working on a concept and while I understand I could use FFMPEG to do what I need to do I was hoping that there would be a way to use nodejs Request to join a video onto the end of another video.

The following code does work for audio, and the video file I am using is an MP4 (note I am trying to join the same video together so instead of 9seconds it should be 18 seconds.

the 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;
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • If you just want one video to play after the other without a gap you may be able to do this on the client side, The approach is to load and start and pause the next video while the current one is still playing and then switch This answer shows an example browser based approach: https://stackoverflow.com/a/58269415/334402 – Mick Feb 17 '20 at 18:11
  • @Mick the idea is more they are able to download the video with a 30 second video in front of the requested video – RussellHarrower Feb 19 '20 at 06:50
  • Request is now depreciated – RoylatGnail Feb 20 '20 at 13:43

4 Answers4

4

I've had a look and found something that may help, there is a package on NPM called video-stitch here's a link

with it you can use the following code to merge 2 videos

'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);
  });

Hope this helps!

mmoomocow
  • 1,173
  • 7
  • 27
1

By using FFMPEG, lets say you have 2 or more than 2 video files. Download all the videos. Create a text file like "all_videos.txt" and save the full path of all the videos directory location you want to merge one after another like this

enter image description here

Then execute this command in node js (take care of "all_videos.txt" location properly in command below)

let cmd = "ffmpeg -f concat -i all_videos.txt -c copy merged_video.mp4"
Exec(cmd, function(err, stdout, stderr) {
  if(err) console.log(err)
  else console.log("Done!")
})
wallgeek
  • 809
  • 1
  • 8
  • 10
0

Concatenating video files is not as simple as concatenating audio files. there are metadata ( tags, headers ) and other stuff ( like the resolution/dimension of both videos) that you need to take care of

Some very simple video format may support file level concatenation but mp4 does not support file level concatenation

and in case of mp3 yes you can simply concatenate the file but it will leave some invalid/corrupted headers but in most of the cases it won't effect the playback

Sure you can write a program ( or maybe someone already wrote a library which you can run in node) to correctly concatenate the video which will take some effort but it's not impossible ( also not simple ) to do it without other external tool on the other hand you can use one of the very mature and well working FFMPEG for this job to know more about how you can dot this with FFMPEG see this answer

Tripurari Shankar
  • 3,308
  • 1
  • 15
  • 24
0

There is a npm package called ffmpeg-concat, which is used to concatenate videos together using OpenGL transitions. This might serve your purpose.

WaughWaugh
  • 1,012
  • 10
  • 15