Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Concat / join .ts segment files into an mp4 file
# This script must be executed in the repo where
# the *.ts files are.
# It will concatenate the segments into one temp
# file which ffmpeg will reencode the audio track.
# By default the ouptup filename is output.mp4
# but can be changed by providing the name as parameter.
# ffmpeg is required
# Example:
# $ ./ trololo.mp4
# Get the output file name
if [ -z "$output" ]; then
# Get length of segment in the current repo
seglen=`ls -la segment-*.ts | wc -l`
if [ -z "$seglen" ]; then
echo "Not segment file found"
exit 1
# Clean temp files
bin=`rm -f all.ts`
# Concat segments into one
while [ "$a" -le $seglen ]
bin=`cat segment-$a.ts >> all.ts`
a=`expr $a + 1`
# Run ffmpeg to reencode the audio
bin=`ffmpeg -i all.ts -bsf:a aac_adtstoasc -vcodec copy $output`
echo $bin
# Delete temp files
bin=`rm -f all.ts`

This comment has been minimized.

Copy link

@mukunm mukunm commented Jul 29, 2018

bin=cat segment-$a.ts >> all.ts

This catting approach produces an all.ts fine.
But, when I ffprobe the all.ts it says the duration is only as long as one ts segment.
Thoughts on why this could be happening?

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