6

I have many small files (about 500). They are in right order. I would like to merge them. Linux commands are also welcome because I can compile them in my OS X. The command cat *.ts > masi.ts does not work well. The result stops at some points. I am investigating why this occurs. Some parts of the videos are not in order.

Names of parts of videos where each file has a prefix HRmasi453-27012016.mp4.ts

1.ts
2.ts
...
100.ts
101.ts
...
200.ts
...
300.ts

so the filename is HRmasi453-27012016.mp4-01.ts ... HRmasi453-27012016.mp4-300.ts. The command cat *.ts > masi.ts does not organise the video in the sequential order. I think I should make a list before of all items in order. Then, merge.

Pseudocode

  1. Make a list of videos in order
  2. Merge cat items in list
    • Do something like filename = prefix + itemInList without creating a new list in the while loop.
    • cat filename >> result.ts

How can you join the many video files, .ts, files?

4
  • 1
    Not at all sure if this would work however you could try first with just a few files, have you tried concatenating then with cat? Jan 27, 2016 at 13:57
  • @user3439894 Thank you for the proposal! There are some bugs. Some gaps in some stages, which I do not understand. I will record the flash show once again and then test again. Sometimes, the order of videos are mixed, probably because of naming. Jan 27, 2016 at 14:09
  • Are the only numbers in itemInList with a leading 0 (zero) e.g., 01...09 and what it the actual file number total count? Jan 27, 2016 at 16:38
  • See my updated answer. Jan 27, 2016 at 17:06

5 Answers 5

8

I need to preface this by saying having not worked with .ts files I do not know whether or not they can be simply concatenated using cat and as you saw because the are numerically named they get mixed up when using cat *.ts > masi.ts because it sorts lexicographically, so using a ordered list is in order. With that in mind, here is what I did to create a list of 500 numerically named .ts files and then concatenate them using the list.

I first created 500 numerically named .ts files containing the number of the .ts filename as a control. I used for i in {1..500}; do echo $i > $i.ts; done These weren't actually valid .ts files however it allowed me to have 500 files to work with and then see that they were concatenated in proper order when opening the combined.ts file to see an ordered numerical list within the file.

Since you already have the 500 .ts files, in Terminal do the following:

cd ts_files_directory
echo {1..500}.ts | tr " " "\n" > tslist
while read line; do cat $line >> combined.ts; done < tslist


Updated to include actual filename per your comment to my answer and your updated question.

The following assumes that the only numbers in itemInList with a leading 0 (zero) are e.g., 01...09 and that you change 500 in the {100..500} portion of the command to the actual count.

Change directory to that of which contains the .ts files.

cd ts_files_directory

The following two commands create the tslist file which is a numerically ordered list of the target filenames to be used with cat in order to avoid the lexicographical sorting issue.

echo 'HRmasi453-27012016.mp4-'{01..99}.ts | tr " " "\n" > tslist
echo 'HRmasi453-27012016.mp4-'{100..500}.ts | tr " " "\n" >> tslist

With the numerically ordered list created, use the following command line to concatenate based on the contents of the tslist file.

while read line; do cat $line >> combined.ts; done < tslist
6
  • The filename actually contains also HRmasi453-27012016.mp4-. So concatenate is required. I run unsuccessfully do cat "HRmasi453-27012016.mp4-" $line". Also failure in echo "HRmasi453-27012016.mp4-" {1...500}. What do you think where it is best to concatenate the prefix to the items of list? I think it is not smart to include the string in the list items, taking much space in reality. I think it is better to concatenate then lastly in do cat somehow. Jan 27, 2016 at 15:51
  • Great! It seems to work. I need to add some globs by echo 'HRmasi453-2701201?.mp4-'{01..99}.ts | tr " " "\n" > tslist. Jan 27, 2016 at 21:07
  • I extended the thread to the quality assurance. I am studying the quality assurance of the video merging here unix.stackexchange.com/q/258113/16920 Jan 27, 2016 at 21:13
  • 1
    @Masi, If the filenames were as you showed and stated up to the last -, e.g. HRmasi453-27012016.mp4-, then I fail to see why you needed to change it to 'HRmasi453-2701201?.mp4-. That said, nonetheless I'm glad you were able to use what I presented. Additionally I'd like to add that if you'd give more accurate and more complete information when asking a question then it would be much easier to give accurate to the situation answers. Just a thought. :) Jan 27, 2016 at 21:35
  • 1
    As a side note, if you're want to read in the list of ts files by their timestamp, you can change line 2 above to ls -tr *.ts | tr " " "\n" > tsList
    – kingliam
    Jan 21, 2017 at 22:56
1

Very easy way based in ffmpeg docs; the accepted answer is more complicated.

  1. first print all the list of files in a file: mylist.txt , give the extension for your file, here is .ts

    printf "file '%s'\n" ./*.ts > mylist.txt
    
  2. finally join the file using the txt list, last parameter name file joined, use same extension that the sources files.

    ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.ts
    

more info about concatenate:

https://trac.ffmpeg.org/wiki/Concatenate

5
  • Can you please compare your answer to user3439894's one? Nov 7, 2017 at 18:08
  • what do you mean? my answer is simple base in ffmpeg docs
    – stackdave
    Nov 7, 2017 at 18:23
  • printf "file '%s'\n" ./*.ts > mylist.txt fails for the exact same reason the OP was posted in the first place. mylist.txt will not contain a properly ordered list of .ts files because of how they are named and the video will be corrupted because the .ts files in the list are out of order. See this link for the contents of mylist.txt: paste.ee/p/ufu0S Nov 7, 2017 at 20:39
  • @Léo Léopold Hertz 준영, See my comment to stackdave's answer. Nov 7, 2017 at 20:40
  • just let the other people to judge with upvote or downvote, don't lost your time
    – stackdave
    Nov 8, 2017 at 4:26
0

Though I have not done this myself I have seen discussions where people have combined multiple video files using the command line version of FFMPEG.

I believe there is a Mac version so you don't have to compile it from source on ffmpeg.org but beyond that someone else will have to verify that and help you with the proper command syntax.

-1

cat ?.ts ??.ts ???.ts > all.ts

1
  • According to the question, all files have a prefix of HRmasi453-27012016.mp4.ts, so the wildcards used may need some refinement here.
    – nohillside
    Aug 30, 2016 at 6:47
-1

I understand you are looking for a elegant command line solution. However, if you use MKVToolnix, you can load the first TS file and append (merge) an unlimited number of ts files to be muxed subsequently in order into an mkv (for example 10, 5 second ts videos will mux into a 50 second mkv). You can then mux the mkv back to a ts in tsmuxergui.

You must log in to answer this question.

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