I think you are probably losing frames because the avi writer takes too long to create the new file or write an image, and the next image is lost. Since you didn't wire the input for which frame to extract, I am guessing that it extracts the most recent one.
The solution for this would be to create a fairly large buffer with as many images as your computer can handle, and use the index from the for loop to specify which image to extract. You can use the integer divide to get the remainder after dividing by the number of images in your buffer.
Bruce
Bruce Ammons
Ammons Engineering