0

I'm trying to use bash to stitch several images in multiple directories into one video. The script below works.

for f in {0..289}; do convert +append Viewer/$f.png Timeline_Board/$f.png Leaderboard/$f.png Montage/montage$f.png; done
cd Montage
ffmpeg -f image2 -r 7 -i montage%d.png -vcodec libx264 -crf 22 montage.mkv

This loops through frames 0.png through 289.png and stitches the components together, as expected. However, I don't always know how many frames I'll be converting. What I'd like to be able to do is use a line like this:

for f in {0..$1}; do convert +append Viewer/$f.png Timeline_Board/$f.png Leaderboard/$f.png Montage/montage$f.png; done

Where I can pass in a command line argument to select exactly how many frames I want. However, when I have that line in my script and run ./convert_images 289 I get an error saying convert: unable to open image 'Viewer/{0..289}.png': No such file or directory @ error/blob.c/OpenBlob/2675.

It seems as though bash is not interpreting the integer I passed in as a parameter for the loop. How can I make bash treat the variable as a number to loop up to?

ecapstone
  • 269
  • 5
  • 14

1 Answers1

0

If your shell is bash, use a C-style for loop:

for ((f=0; f<289; f++)); do
  ...
done

To explain the why of it -- brace expansion happens before paramater expansion, so the value of your variable isn't yet available when it occurs.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • This isn't per-se an answer to the question asked, but `for ((f=0; f<$1; f++));` worked, so I marked it as correct. Thanks for the help! – ecapstone May 26 '15 at 03:13