7

Downloading a video stream with curl, I ended up with ~400 *.ts files, each about 1MB in size. They are sequentially numbered video1.ts, video2.ts, ...video400.ts. I now need to concatenate them into one file, obviously in the right order (so video10.ts should be followed by video11.ts and not video110.ts).

I've tried to come up with something like "for i in *.ts; do ...." but I just can't figure it out. Also ffmepg and avconv are too complicated for me.

Who knows how to join these 400 files in the right oreder, into a new file? Thx!

1

7 Answers 7

7

Shortest way to concat all .ts into one .ts file : on linux

cat *.ts > all.ts

Shortest way to concatenate all .ts into one .ts file : on Windows (using cmd)

copy /b *.ts all.ts
5

filenames="`ls -rt1 $input | tr '\n' '|' | sed '$ s/.$//'`"

ffmpeg -i "concat:$filenames" -c copy out.ts,

where $input is the filename(s) or escaped regexp (e.g., \*.ts).

1
  • Quickest way to concat .ts files with ffmpeg, thanks.
    – Avamander
    Sep 19, 2019 at 12:13
2

All the answers to this question that mislead readers to concatenate the TS files before running ffmpeg are incorrect. To ensure the audio and video do not fall out of sync during the assembly of the mp4 stream, the poorly documented but important "-f concat" feature of ffmpeg should be used.

    delimiterBeforeFileNumber="-"
    ls |egrep '[.]ts$' \
        |sort "-t$delimiterBeforeFileNumber" -k2,2n \
        |sed -r "s/(.*)/file '\1'/" >ts.files.txt

    ffmpeg -f concat -i ts.files.txt -c copy tsw.014.ts.mp4

The two preparatory lines of code just create a file containing a list of TS files in this line format, which is used by ffmpeg like a playlist.

    file 'seg-37-a.ts'
1

What sort of does the trick:

for i in `seq 1 400`; do cat "video$i.ts" >> newvideo.ts; done

but now the audio is out of sync by ~0.5s and there are ~0.5s silences every few seconds (presumably when fragments are glued together).

1

This is an old question but I hope the answer may add value for others.

Based on this reference, the following script will do the job, assuming ffmpeg 1.1 and later.

#!/bin/bash

for i in `seq 0 $totalNumberOfTsFiles`; do echo  file "'${i}.ts'" >> Input.txt ; done
/home/hq6/bin/ffmpeg-2.3.3/ffmpeg -f concat -i Input.txt  -c copy output.ts
2
  • 1
    The entire for loop can probably be replaced by something like seq -f '%g.ts' 0 $totalNumberOfTsFiles for a similar result with a much cleaner command line.
    – user
    Aug 28, 2015 at 19:12
  • James Ivey: If you have a new idea for a different way to solve this problem, don’t try to post it in a comment (and don’t edit it into somebody else’s answer); post it as a new answer (in the “Your Answer” box at the bottom of the page). In case you’ve forgotten what you wanted to submit, you can review it here. Notes: (1) When the output of ls is a pipe, it uses -1 mode automatically. You can see this by running plain ls and then ls | cat. (2) ls sorts its output by name unless you tell it not to, so you don’t need ls | sort. Oct 29, 2017 at 18:09
1

Try the following command:

cat video?.ts video??.ts video???.ts  > out.ts
2
  • The duration of out.ts will likely be that of the first video.
    – Geremia
    Feb 4, 2018 at 1:16
  • You must use >> instead of >. Otherwise only the last file will be in out.ts.
    – Smeterlink
    Oct 7, 2023 at 20:24
1

Best Solution:

Download TSSplitter, click "JOIN" tab and drag all files into the window!

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .