FFmpeg Split screen and combine multiple videos

Execution environment

Since it is supposed to be executed in Node.js, it will be JavaScript code. If you type the same command on the command line, you can execute it other than Node.js.

Split screen and combine multiple videos

This is an example of dividing the screen into four and synthesizing the video in the upper left, upper right, and lower left.

const { execSync } = require('child_process');

function overlay(input1, input2, input3, output)
{
    const stdout = execSync(`ffprobe -i ${input1} -show_streams -print_format json`);
    let streamInfo = JSON.parse(stdout);
    //Get width, height
    let width = streamInfo.streams[0].coded_width;
    let height = streamInfo.streams[0].coded_height;
    //Calculate width and height after 4 divisions
    let cutWidth = width/2;
    let cutHeight = height/2;
    //Command generation
    command = `ffmpeg -i ${input1} -i ${input2} -i ${input3} -filter_complex "\
        amix=inputs=3:duration=longest;\
        [0:v] setpts=PTS-STARTPTS, scale=${cutWidth}x${cutHeight} [upperleft];\
        [1:v] setpts=PTS-STARTPTS, scale=${cutWidth}x${cutHeight} [upperright];\
        [2:v] setpts=PTS-STARTPTS, scale=${cutWidth}x${cutHeight} [lowerleft];\
        [upperleft]pad=2*iw:2*ih[a];\
        [a][upperright]overlay=w:0[b];\
        [b][lowerleft]overlay=0:h\
        " \
        ${output}`;
    //Command execution
    execSync(command);
}

Command content

The first half of the function gets the width and height from the stream information of input1 and calculates the width and height of each video after dividing into four.

Audio ʻA mix = inputs = 3: duration = longest;is an audio composite. If ʻamix = inputs = 3: duration = longest;is omitted, only the video and audio in the upper left will be displayed.

Video

[0: v] setpts = PTS-STARTPTS, scale = $ {cutWidth} x $ {cutHeight} [upperleft]; [0: v]: Represents the Video of input1. scale = $ {cutWidth} x $ {cutHeight}: The scale is being changed. [upperleft]: Defined with the name upperleft. Since it is a name, it can be changed arbitrarily.

[1:v] setpts=PTS-STARTPTS, scale=${cutWidth}x${cutHeight} [upperright];[2:v] setpts=PTS-STARTPTS, scale=${cutWidth}x${cutHeight} [lowerleft]; Performs the same processing for input2 and input3.

[upperleft]pad=2*iw:2*ih[a]; Is adding a margin to the [upper left] video. ʻIw means input Width and ʻih means input height, adding a margin that is twice the width and height. We also define it as [a].

[a][upperright]overlay=w:0[b]; Combining [upperright] with [a] at the horizontal coordinate w (width of [upperright]) and the vertical coordinate 0. We also define it as [b].

[b][lowerleft]overlay=0:h Combining [lowerleft] with [b] at the position of horizontal coordinates 0 and vertical coordinates h (height of [lowerleft]).

Notes

Adding ; to the last option [b] [lowerleft] overlay = 0: h will result in an error (No such filter:'').

Execution result

Recommended Posts

FFmpeg Split screen and combine multiple videos
Extract face images from videos with ffmpeg and OpenVINO
Insert silent black screen before and after FFmpeg video