bashloopscommand-lineffmpegmuxer

Loop with two variables at once in Bash


I have two file types with the same filenames in one directory:

file1.ts
file1.ec3
file2.ts
file2.ec3

and I need to make a loop which would process pair of files at once (file1.ts + file1.ec3). Then the loop needs to be restarted with second pair (file2.ts + file2.ec3).

Here is my code:

for i in *.ts; do
    for e in *.ec3; do
        ffmpeg -i "$i" -i "$e" -map 0:v -map 1:a -codec copy "${i%.ts}_DD+.ts";
    done;
done

But when the first cycle ends it tries to process file1.ts + file2.ec3 and brakes everything...

How can I make it work properly?


Solution

  • Try this:

    for file in *.ts; do
        [ -f "${file%.ts}.ec3" ] &&
            ffmpeg -i "$file" -i "${file%.ts}.ec3" -map 0:v -map 1:a \
                -codec copy "${file%.ts}_DD+.ts"
    done