#!/bin/sh trap cleanup EXIT ccf=/tmp/concatfile. cleanup() { rm ${ccf}*; } concat() { outfile="output$i.mkv" concatfile=$(mktemp ${ccf}XXXXXX) dir="$(pwd)" for video in $vidfiles; do echo "file '$dir/$video'" >> $concatfile done cat $concatfile | sort > ${concatfile}_ # Sort concatfile so videos are ordered correctly ffmpeg -v error -f concat -safe 0 -i ${concatfile}_ -c copy $outfile } for video in "$@"; do codechash=$(ffprobe -v quiet -show_format -show_streams "$video" | \grep 'width\|height\|codec' | md5sum | cut -d' ' -f1) [ -z "$prevhash" ] && prevhash=$codechash if [ $prevhash = $codechash ]; then vidfiles="$video${vidfiles:+ }$vidfiles" else [ -z "$i" ] && i=0 concat vidfiles="" i=$((i+1)) fi done concat