0

Is there is a way we can add our pre-defined own video clip to existing video via some script and then save it as one video? We don’t want to use any video editor software’s Please let me know do you have any way to video edit and concatenation video?

1 Answers1

0

Welcome! ffmpeg is my favorite way of working with audio and video files through code and can be used with most any scripting language (I have done it with python and javascript). There is already an answer to the question here but I will give another example.

Since you tagged javascript and node.js here is a snippet of the code I have used to accomplish this programmatically (through code), without using a video editor. I have used this on linux, mac, and windows so the file paths depend on your system. It works with most major audio and video formats, but for the sake of this example I am using .mp4. I just tested this exact setup I posted below on Windows 10 and it worked with no errors.

First, create a new node project (if you have not done so already)

npm init

Next, install ffmpeg using npm for node from the command line (this will install it for any platform: linux, osx, windows) npm package

npm install ffmpeg-downloader

There are multiple ways to combine videos, but I like the following method. In order to combine the files you need to have the full file paths listed in a .txt file, one video file per line, that ffmpeg will read to combine (I call mine files.txt but it can be called anything). I am not sure of the limit of videos you can have, but I have combined up to 15 videos at once into a single final video with no issue.

The contents of files.txt would look something like this; you can either create it beforehand or through code, depending on your needs:

file 'C:\path\to\file1.mp4'
file 'C:\path\to\file2.mp4'
file 'C:\path\to\file3.mp4'
file 'C:\path\to\file4.mp4'

Now in a javascript file (I called mine combine.js) you can adapt the following to fit your file path structure.

const { exec } = require('child_process');
const ffmpeg = require('ffmpeg-downloader');

function combineVideos() {
    console.log('process starting');

    // executable for ffmpeg
    const exe = ffmpeg.path;

    // the files.txt that contains the list of files to combine
    const filesList = 'C:\\path\\to\\files.txt';

    // where you want the final video to be created
    const outFile = 'C:\\path\\to\\finalCombinedVideo.mp4';

    // build the command that ffmpeg will run. It will look something like this:
    // ffmpeg -y -f concat -safe 0 -i "path/to/files.txt" -c copy "path/to/finalCombinedVideo.mp4"

    const cmd = '"' + exe + '" -y'
        + ' -f concat'
        + ' -safe 0'
        + ' -i "' + filesList + '"'
        + ' -c copy "' + outFile + '"';

    exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(`exec error: ${err}`);
            return;
        }

        console.log(`process complete: ${outFile}`);
    });
}

// call the combineVideos function
combineVideos();

Now, you can use node to run the combine function and create the video

node combine.js
process starting
process complete:  C:\path\to\finalCombinedVideo.mp4

I hope this can be of some help to you. Best of luck!

chrisbyte
  • 1,408
  • 3
  • 11
  • 18
  • Thank you so much for explaining its really help us we need to add gif image as an overlay on video using FFMPEG. That image will need to show after specific time on given pinpoints. – Baljit Magento 2 Dec 11 '19 at 04:56
  • Adding a gif would require a few more options for the `ffmpeg` command, as described here https://video.stackexchange.com/questions/12105/add-an-image-overlay-in-front-of-video-using-ffmpeg and here https://superuser.com/questions/472557/animated-gif-as-an-overlay-in-ffmpeg. I have not done this before myself, but my example would still work for you as a starting point, and you would change the command to use the other options in the correct manner. – chrisbyte Dec 11 '19 at 14:40
  • Thank you so much @chrisbye its working fine we have achieved something. – Baljit Magento 2 Dec 13 '19 at 06:21