Skip to content

Instantly share code, notes, and snippets.

@uotw
Last active August 7, 2019 04:00
Show Gist options
  • Save uotw/878d04974566b78433759422af17368c to your computer and use it in GitHub Desktop.
Save uotw/878d04974566b78433759422af17368c to your computer and use it in GitHub Desktop.
combine 4 clips into one 2 x 2 grid video mosaic
#!/usr/bin/env bash
finalwidth=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 $1)
finalheight=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 $1)
halfwidth=$(echo "$finalwidth/2" | bc)
halfheight=$(echo "$finalheight/2" | bc)
x="x"
ffmpeg -i $1 -i $2 -i $3 -i $4 -filter_complex "nullsrc=size=$finalwidth$x$finalheight [base]; [0:v] setpts=PTS-STARTPTS, scale=$halfwidth$x$halfheight [upperleft]; [1:v] setpts=PTS-STARTPTS, scale=$halfwidth$x$halfheight [upperright]; [2:v] setpts=PTS-STARTPTS, scale=$halfwidth$x$halfheight [lowerleft]; [3:v] setpts=PTS-STARTPTS, scale=$halfwidth$x$halfheight [lowerright]; [base][upperleft] overlay=shortest=1 [tmp1]; [tmp1][upperright] overlay=shortest=1:x=$halfwidth [tmp2]; [tmp2][lowerleft] overlay=shortest=1:y=$halfheight [tmp3]; [tmp3][lowerright] overlay=shortest=1:x=$halfwidth:y=$halfheight" -c:v libx264 quad_vid.mp4
@uotw
Copy link
Author

uotw commented Aug 7, 2019

  • dependencies: must have ffmpeg/ffprobe installed for your OS, must have bash shell (OS X or Linux)
  • this script expects each video file to be the same dimensions, and makes the final video duration the length of the shortest input video, and final video dimensions the same as the input videos, final rendered video is mp4 format
  • usage: download this file quad.sh, and place in a directory containing your video files. To make executable open a terminal window, type cd <yourdirectory>, then chmod +x quad.sh Then, to run, type ./quad.sh <video1> <video2> <video3> <video4>

@uotw
Copy link
Author

uotw commented Aug 7, 2019

Here's an online version of the tool
https://sonoclipshare.com/quad_video/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment