简体   繁体   中英

How to edit and concatenation video

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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