Search code examples
ffmpegterminalconcatenation

Merging video in a specific order using ffmpeg


I use the following to merge video in numeric order.

for f in *; do mv "$f" "${f: -17}"; done &&. find *.ts|. sed 's:\:\ :g'| sed 's/^/file /' > fraglist.txt && ffmpeg -f concat -safe 0 -i fraglist.txt -c copy output.ts; rm fraglist.txt

This works great for files named like the following...
000001
000002
000003
000004
000005
000006
000007
000008
000009
000010

But If I need something like the following merged the order is based on how many digits there are in the file name...

1708.ts
9803.ts
13798.ts
17815.ts
21804.ts
25819.ts
29832.ts

What command could I use to get the second group of files merged in that order? Thank you for your help!


Solution

  • Simplify your whole process by using printf alone to make the txt file contents, and use sort to provide natural/version sorting:

    printf "file '%s'\n" *.ts | sort -V > fraglist.txt
    ffmpeg -f concat -i fraglist.txt -c copy output.ts
    

    Result:

    file '1708.ts'
    file '9803.ts'
    file '13798.ts'
    file '17815.ts'
    file '21804.ts'
    file '25819.ts'
    file '29832.ts'
    
    • No need to rm fraglist.txt as the sort redirect (>) will overwrite fraglist.txt.

    • Note that I removed -safe 0 (an option specific to the concat demuxer) from the ffmpeg command because you don't need it in this exact example. But if you get the Unsafe file name error (such as due to special characters including spaces) then you will need to add it.