#!/bin/bash
#Concatenate TS streams
#
#This script requires ffmpeg
#
#Version: 1.0
#
#Usage:
# catts streamout streamIn1 streamIn2 ...streamInN
#
#Example:
# catts out.ts in1.ts in2.ts in3.ts in4.ts

#Set global vars 
#----------------------------
ffmpeg="ffmpeg"

#Set in vars
#----------------------------
destTS=$1

#Set intermediate files 
#----------------------------
catlist=${destTS}_list.txt

#Clean
#----------------------------
rm -f $catlist
rm -f $destTS

#Create list of files to merge
#----------------------------
noout="0"

echo "#List of files to merge" > $catlist
for var in "$@"
do
	if [ $noout == "0" ]; then
		noout="1"
	else	
		echo "file '${var}'" >> $catlist
	fi
done

#Cat mpeg TS streams
#----------------------------
${ffmpeg} -f concat -i $catlist -codec copy -mpegts_copyts 1 -f mpegts ${destTS}

