Q:

FFmpeg performs every second task

Why does ffmpeg perform every second task that it is ordered to?
for i in *.MTS; do
     echo ffmpeg -i \"$i\" -c:a copy -c:v copy \"$i\" | sed 's/MTS/mp4/2'
done | sh

For example. if I have 9 MTS files like:
0001.MTS
0002.MTS
0003.MTS
0004.MTS
0005.MTS
0006.MTS
0007.MTS
0008.MTS
0009.MTS

This converts only 001 003 005 007 009. Any reason for that? The same command without | sh prints proper set of orders. It becomes really a headache when you have 64 video files.

A:

I don't know, why only every second ffmpeg command is executed, I could not reproduce this on my machine. However, you approach is too complicated. A cleaner solution would be:

for bash compatible shells:
for i in *.MTS; do
  ffmpeg -i $i -c:a copy -c:v copy ${i%MTS}mp4
done

Please read about the % and related operators in the section Parameter Expansion in bash's man page.
for csh compatible shells:
foreach i (*.MTS)
  ffmpeg -i $i -c:a copy -c:v copy $i:r.mp4
end

Here, the :r modifiers removes the file extension. Read more about some other modifiers in the (somewhat unintuitively) section History substitution of tcsh's man page.

