Skip to content

Instantly share code, notes, and snippets.

@rahul286
Created July 26, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul286/ac5ea06b959c0f95a53188c2f18ff50f to your computer and use it in GitHub Desktop.
Save rahul286/ac5ea06b959c0f95a53188c2f18ff50f to your computer and use it in GitHub Desktop.
Mosaic/collage creation for Fittr TC videos using FFMpeg

FFMpeg Script to create mosaic/collage from 12 TC videos

Please note, we will be using ffmpeg via command-line. If you are not comfortable with command-line, then better find another way.

Preparation

  1. Move all weekly videos into a folder (e.g. vidoes).
  2. Rename all videos according to week numbers such as 01.mp4, 02.mp4
  3. (Optional) since we are goint to mute the sound, you may trim earlier part where we speak secret code.

I am assuming you have them already in mp4 format. If you have them in mov format, it also works, just replace mp4 references to mov.

Labeling videos with week number

Run following command to add week-number label to each video

cd videos

for i in *.mp4; do
    ffmpeg -i $i -vf \
        "format=yuv444p, \
 drawbox=y=10:color=black@0.7:width=300:height=120:t=fill, \
 drawtext=text='W ${i%%.*}':fontcolor=white:fontsize=100:x=20:y=30, \
 format=yuv420p" \
        -c:v libx264 -c:a copy -movflags +faststart t-${i%%.*}.mp4
done

Creating mosaic

Now you have two options to create mosiac/collage from 12 videos. It can be 4x3 or 3x4. I prefer as 3x4 was becoming too "vertical".

Use following to create 3x4 mosaic/collage

ffmpeg \
    -i ./t-01.mp4 \
    -i ./t-02.mp4 \
    -i ./t-03.mp4 \
    -i ./t-04.mp4 \
    -i ./t-05.mp4 \
    -i ./t-06.mp4 \
    -i ./t-07.mp4 \
    -i ./t-08.mp4 \
    -i ./t-09.mp4 \
    -i ./t-10.mp4 \
    -i ./t-11.mp4 \
    -i ./t-12.mp4 \
    -filter_complex " \
      [0:v] setpts=PTS-STARTPTS, scale=qvga [a0]; \
      [1:v] setpts=PTS-STARTPTS, scale=qvga [a1]; \
      [2:v] setpts=PTS-STARTPTS, scale=qvga [a2]; \
      [3:v] setpts=PTS-STARTPTS, scale=qvga [a3]; \
      [4:v] setpts=PTS-STARTPTS, scale=qvga [a4]; \
      [5:v] setpts=PTS-STARTPTS, scale=qvga [a5]; \
      [6:v] setpts=PTS-STARTPTS, scale=qvga [a6]; \
      [7:v] setpts=PTS-STARTPTS, scale=qvga [a7]; \
      [8:v] setpts=PTS-STARTPTS, scale=qvga [a8]; \
      [9:v] setpts=PTS-STARTPTS, scale=qvga [a9]; \
      [10:v] setpts=PTS-STARTPTS, scale=qvga [a10]; \
      [11:v] setpts=PTS-STARTPTS, scale=qvga [a11]; \
      [a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][a10][a11]xstack=inputs=12:layout=0_0|w0_0|w0+w1_0|w0+w1+w2_0|0_h0|w0_h0|w0+w1_h0|w0+w1+w2_h0|0_h0+h1|w0_h0+h1|w0+w1_h0+h1|w0+w1+w2_h0+h1[out] \
      " \
    -map "[out]" \
    -vsync 2  new-3x4.mp4

Alternative

Use following to create 4x3 mosaic/collage

ffmpeg \
    -i t-01.mp4 \
    -i t-02.mp4 \
    -i t-03.mp4 \
    -i t-04.mp4 \
    -i t-05.mp4 \
    -i t-06.mp4 \
    -i t-07.mp4 \
    -i t-08.mp4 \
    -i t-09.mp4 \
    -i t-10.mp4 \
    -i t-11.mp4 \
    -i t-12.mp4 \
    -filter_complex " \
      [0:v] setpts=PTS-STARTPTS, scale=qvga [a0]; \
      [1:v] setpts=PTS-STARTPTS, scale=qvga [a1]; \
      [2:v] setpts=PTS-STARTPTS, scale=qvga [a2]; \
      [3:v] setpts=PTS-STARTPTS, scale=qvga [a3]; \
      [4:v] setpts=PTS-STARTPTS, scale=qvga [a4]; \
      [5:v] setpts=PTS-STARTPTS, scale=qvga [a5]; \
      [6:v] setpts=PTS-STARTPTS, scale=qvga [a6]; \
      [7:v] setpts=PTS-STARTPTS, scale=qvga [a7]; \
      [8:v] setpts=PTS-STARTPTS, scale=qvga [a8]; \
      [9:v] setpts=PTS-STARTPTS, scale=qvga [a9]; \
      [10:v] setpts=PTS-STARTPTS, scale=qvga [a10]; \
      [11:v] setpts=PTS-STARTPTS, scale=qvga [a11]; \
      [a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][a10][a11]xstack=inputs=12:layout=0_0|w0_0|w0+w1_0|0_h0|w0_h0|w0+w1_h0|0_h0+h1|w0_h0+h1|w0+w1_h0+h1|0_h0+h1+h2|w0_h0+h1+h2|w0+w1_h0+h1+h2[out] \
      " \
    -map "[out]" \
    -vsync 2 new-4x3.mp4

Helpful functions

In videos folder you an use following to convert all files from one video format to another format.

for i in *.mov; do
    ffmpeg -i $i ${i%%.*}.mp4
done

References

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