ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

How can add mosaic or stacking?

Open mousou2003 opened this issue 3 years ago • 2 comments

Any recommendation what would be the best way to add capability to do a mosaic of 4 videos? The orginial ffmpeg command would like this one ffmpeg
-i input0.mp4 -i input1.mp4 -i input2.mp4 -i input3.mp4
-filter_complex
"[0:v][1:v]hstack=inputs=2[top];
[2:v][3:v]hstack=inputs=2[bottom];
[top][bottom]vstack=inputs=2[v]"
-map "[v]"
finalOutput.mp4

mousou2003 avatar Feb 20 '22 08:02 mousou2003

yes I would also like to know this. The existing examples are great. I just wish there were more of them

ffmpeg -i input0 -i input1 -i input2 -i input3 \
  -filter_complex "[0:v][1:v][2:v][3:v]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" \
  -map "[v]" output

chapmanjacobd avatar Apr 17 '22 04:04 chapmanjacobd

oooo I think I figured it out just by monkey-on-the-typewriter-ing it

import os

import ffmpeg

in0 = ffmpeg.input(os.path.expanduser("~/.tmp/2022_0412_090814_F.mp4"))
in1 = ffmpeg.input(os.path.expanduser("~/.tmp/2022_0412_090814_R.mp4"))
aout = ffmpeg.filter(
    [
        in0.audio,
        in1.audio,
    ],
    "amultiply",
)
vout = ffmpeg.filter(
    [
        in0.video,
        in1.video,
        in1.video,
        in0.video,
    ],
    "xstack",
    inputs=4,
    layout="0_0|w0_0|0_h0|w0_h0",
)

(
    ffmpeg.output(
        aout,
        vout,
        "out.mkv",
    ).run()
)

if someone makes more examples there should also be a multiple output streams example:

aout = [
    in0.audio,
    in1.audio,
]

(
    ffmpeg.output(
        vout,
        in0.audio,
        in1.audio,
        "out.mkv"
    ).run()
)

it's confusing at first but then it makes sense !

chapmanjacobd avatar Apr 17 '22 04:04 chapmanjacobd