Skip to content

Instantly share code, notes, and snippets.

@jerieljan
Last active Aug 7, 2016
Embed
What would you like to do?
This simple script splices a video file "trimmed.mkv" into multiple videos--defined by X-count and Y-count of screens. Requires ffmpeg.
#!/bin/bash
# This script splices a movie file into multiple parts.
# First parameter for the number of X screens
# Second parameter for the number of Y screens
X_COUNT=$1
Y_COUNT=$2
FILENAME="trimmed.mkv"
# Get the resolution of the file...
X_RES=`ffmpeg -i $FILENAME 2>&1 | grep Stream | grep -oP ', \K[0-9]+x[0-9]+' | cut -f 1 -dx`
Y_RES=`ffmpeg -i $FILENAME 2>&1 | grep Stream | grep -oP ', \K[0-9]+x[0-9]+' | cut -f 2 -dx`
echo $X_RES
echo $Y_RES
# Then divide it per number of X and Y screens
X_RES_EACH=$(($X_RES / $X_COUNT))
Y_RES_EACH=$(($Y_RES / $Y_COUNT))
echo Splicing videos to $X_RES_EACH x $Y_RES_EACH
# For each X, for each Y, assemble a video using the EACH resolution in steps.
for i in `seq 0 $(($X_COUNT - 1))`
do
for j in `seq 0 $(($Y_COUNT - 1))`
do
X_STEP=$(($i * $X_RES_EACH))
Y_STEP=$(($j * $Y_RES_EACH))
echo "Encoding video position $i,$j..."
ffmpeg -loglevel panic -i ${FILENAME} -filter:v "crop=${X_RES_EACH}:${Y_RES_EACH}:${X_STEP}:${Y_STEP}" trimmed_$i-$j.mkv
done
done
echo "Operation complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment