Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Created July 5, 2023 17:36
Show Gist options
  • Save bahamas10/0c0b80666f22175a009cfd03c570f0a0 to your computer and use it in GitHub Desktop.
Save bahamas10/0c0b80666f22175a009cfd03c570f0a0 to your computer and use it in GitHub Desktop.
Convert a stream layout to vertical format

This script can be used to convert a stream layout to a vertical layout.

Before

Screenshot 2023-07-05 at 1 32 18 PM

After

Screenshot 2023-07-05 at 1 32 02 PM


The script was configured specifically for https://www.twitch.tv/videos/1861733765

$ ./thab streams/The\ Best\ SMW\ Hack\ this\ side\ of\ the\ Mississippi.mp4 out.mp4
$ open out.mp4
#!/usr/bin/env bash
#
# Convert a stream layout into a vertical layout.
#
# Author: Dave Eddy <[email protected]>
# Date: July 05, 2023
# License: MIT
input=$1
output=$2
# This script works by creating a blank video of size 1080x1920 at 60fps (of
# just the color black). Then, it reads data from an input video (presumed to
# be a 1080p video containing gameplay and a facecam) to crop out the gameplay
# and facecam and overlay it on the new 9:16 vertical video. The audio feed is
# copied directly from the source video to the destination.
#
# The _crop variables are in the format of:
#
# width:height:offsetX:offsetY
#
# So in the example of:
#
# gameplay_crop='1250:920:640:135'
#
# This means the area of the gameplay we want is 1250px by 920px, and starts
# 640px in from the left and 135px down from the top.
#
# ---
#
# The _scale variables are in the format of:
#
# width:height
#
# and this will force scale (resize) the video to fit the given dimensions. The
# number "-1" can be used for one of the variables to have it be automatically
# be calculated and maintain aspect ratio. For example, if a video is 250x500
# pixels in size, a scale of "-1:1000" will result in the video size being
# "500:1000".
#
# This script makes the assumption that the facecam will be at the top of the
# resulting video and the gameplay will be at the bottom of the resulting video,
# so the facecam video will "grow down" from the top when increased in size, and
# the gameplay will "grow up" from the bottom increased in size - with the
# facecam ultimately "winning" (being on top).
#
# NOTE: this script was specifically tested with
# https://www.twitch.tv/thabeast721/videos
gameplay_crop='1250:1070:720:10'
gameplay_scale='-1:1300'
facecam_crop='600:380:0:630'
facecam_scale='1200:-1'
# XXX NOTE: the `-t 5` argument will make it so only 5 seconds are rendered -
# this is useful for when you are adjusting variables and want to see the
# result quickly. Remove that line before running this for real.
ffmpeg \
-f lavfi -i 'color=c=black:s=1080x1920:r=60/1' \
-i "$input" \
-t 5 \
-filter_complex "
[1]crop = $gameplay_crop [gameplay];
[gameplay]scale = $gameplay_scale [gameplay];
[0][gameplay]overlay = (main_w / 2) - (overlay_w / 2) : main_h - overlay_h [canvas];
[1]crop = $facecam_crop [facecam];
[facecam]scale = $facecam_scale [facecam];
[canvas][facecam]overlay = 0:0
" \
-c:a copy \
-n "$output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment