Concatenate TS files with correct timestamps

14,095

Solution 1

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

Solution 2

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
Share:
14,095
Pete9119
Author by

Pete9119

Updated on July 08, 2022

Comments

  • Pete9119
    Pete9119 almost 2 years

    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.