6

I'm trying to merge multiple ts chunk files to one single file, without any loss of quality or reencoding. The files are taken from a live stream, however I'm trying to merge them in a diffrent order and not the order they were streamed.

Example of files:

0000000033.ts
0000000034.ts
0000000039.ts
0000000044.ts

I tried:

cat 0000000033.ts 0000000034.ts 0000000039.ts 0000000044.ts >combined.ts

and

ffmpeg -i "concat:0000000033.ts|concat:0000000034.ts|concat:0000000039.ts|concat:0000000044.ts" -c copy -bsf:a aac_adtstoasc output.mp4

This kinda works, however I instead of beeing 4 seconds long it's around 15. It plays this way:

[first 2 clips]
[5 secs pause]
[39.ts]
[5 secs pause]
[44.ts]
[done]

This happens to both the cat and ffmpeg combined version. So it seems the ts chunks contain timestamps from the stream that are beeing used.

How can I fix that to make it one continous clip?

The chunks here are more of an example, the chunks will be dynamically selected.

Pete9119
  • 147
  • 1
  • 3
  • 7
  • Can you try analyzing the resulting file with DVBInspector? Check `Show packets` in the menu and post a screenshot of the PTS/DTS diagram. – aergistal Nov 10 '15 at 10:24
  • 2
    Are you using a recent version of ffmpeg? See [#4924](https://trac.ffmpeg.org/ticket/4924) which seems similar to your problem. – wimh Nov 15 '15 at 19:41

5 Answers5

7

If you have a long list of TS files, you can create a playlist, a file containing a list of the TS files in this line format:

    file 'seg-37-a.ts'

These commands produce such a file, with the TS files sorted numerically.

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

Then the creation of the single file can read the playlist using the -f concat modifier of ffmpeg's -i option.

    ffmpeg -f concat -i ts.files.txt -c copy tsw.014.ts.mp4
Douglas Daseeco
  • 3,475
  • 21
  • 27
5

Haven't checked whether this works with the concat protocol, but you need to generate a new set of timestamps.

ffmpeg -i "concat:0000000033.ts|0000000034.ts|0000000039.ts|0000000044.ts" \
       -c copy -bsf:a aac_adtstoasc -fflags +genpts output.mp4
Gyan
  • 85,394
  • 9
  • 169
  • 201
4

TS files can actually be merged with the Windows 'copy' command. The following will merge every TS in the current folder. Then, once you have a single ts, transmux to mp4 without re-encoding. I confirm the video duration will be correct unlike ffmpeg's concat.

copy /b *.ts all.ts

ffmpeg -i all.ts -c copy all.mp4
firebfm
  • 203
  • 1
  • 3
  • 9
  • I've tried and I'm very surprised that it works perfectly. I have no idea why a raw concat will have a better effect than just using the concat feature of ffmpeg You saved my day ! – AriaOS Jun 29 '22 at 08:41
  • Works well under Linux too with `cat *.ts > all.ts`. – Adam Barnes Jul 17 '23 at 03:20
0

In recent versions of ffmpeg, a bit stream filter setts has appeared that allows you to fix timestamps without transcoding. Try it, in my case it helped.

-bsf "setts=PTS-STARTPTS;DTS-STARTDTS"

I am combining a lot of mpeg ts chunks using unix cat, and then transmuxing to an mp4 container

ffmpeg -abort_on empty_output_stream -hide_banner -loglevel repeat+level+error -progress pipe:1 -i pipe:0 -map 0 -c copy -bsf:a aac_adtstoasc -bsf "setts=PTS-STARTPTS;DTS-STARTDTS" -f mp4 -movflags faststart -y out.mp4

enter image description here

jidckii
  • 139
  • 2
  • 5
-1

Are the examples accurate in file numbers? as 0033.ts and 0034.ts play together but it takes 5 secs to get to 0039.ts and then another 5 to 0044.ts so 0034 + 5 secs = 0039 and + 5 secs = 0044 so are you joining them in their proper order?

Sorry I misread the question but in regards to your problem once you have the 15 sec clip there is a program called flv editor lite from moyea which will take in and convert a .mp4 file to .flv and allow you to cut the excess time out of the file and export it as one file but then you need to reconvert back to .mp4 again

citizen
  • 1
  • 1