package com.pyrohail.AverageFrame;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;

public class AverageFrame {
	private static int totalFrames = 0;
	private static int[][][] imageRGB;

	public static void main(String[] args) {
		IMediaReader mediaReader = ToolFactory.makeReader(args[0]);
		mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
		mediaReader.addListener(new ImageSnapListener());

		while (mediaReader.readPacket() == null);

		BufferedImage image = new BufferedImage(imageRGB.length, imageRGB[0].length, BufferedImage.TYPE_INT_RGB);

		try {
			for (int x = 0; x < imageRGB.length; x++) {
				for (int y = 0; y < imageRGB[0].length; y++) {
					int redPixel = imageRGB[x][y][0] / totalFrames;
					int greenPixel = imageRGB[x][y][1] / totalFrames;
					int bluePixel = imageRGB[x][y][2] / totalFrames;

					image.setRGB(x, y, new Color(redPixel, greenPixel, bluePixel).getRGB());
				}
			}

			File outputfile = new File("AverageFrame.png");
			ImageIO.write(image, "png", outputfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static class ImageSnapListener extends MediaListenerAdapter {

		@Override
		public void onVideoPicture(IVideoPictureEvent event) {
			BufferedImage image = event.getImage();

			if (totalFrames == 0) {
				imageRGB = new int[image.getWidth()][image.getHeight()][3];
			}

			for (int x = 0; x < image.getWidth(); x++) {
				for (int y = 0; y < image.getHeight(); y++) {
					int pixelsRGB = image.getRGB(x, y);

					imageRGB[x][y][0] += (pixelsRGB >> 16) & 0XFF;
					imageRGB[x][y][1] += (pixelsRGB >> 8) & 0XFF;
					imageRGB[x][y][2] += (pixelsRGB) & 0XFF;
				}
			}

			totalFrames++;
		}
	}
}

@@ -361,36 +361,112 @@
 nt;%0A
-%0Apublic class AverageFrame %7B
+import com.xuggle.xuggler.IContainer;%0A%0Apublic class AverageFrame %7B%0A%09private static int currentFrame = 0;
 %0A%09pr
@@ -634,19 +634,59 @@
 der(args
-%5B0%5D
+.length %3E= 1 ? args%5B0%5D : %22AverageFrame.mp4%22
 );%0A%09%09med
@@ -809,16 +809,84 @@
 er());%0A%0A
+%09%09setTotalFrames(args.length %3E= 1 ? args%5B0%5D : %22AverageFrame.mp4%22);%0A%0A
 %09%09while 
@@ -1545,16 +1545,259 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09public static void setTotalFrames(String videoFile) %7B%0A%09%09IContainer container = IContainer.make();%0A%09%09container.open(videoFile, IContainer.Type.READ, null);%0A%0A%09%09totalFrames = (int)container.getStream(1).getNumFrames();%0A%0A%09%09container.close();%0A%09%7D%0A%0A
 %09private
@@ -1980,28 +1980,48 @@
 %0A%09%09%09
-if (total
+currentFrame++;%0A%09%09%09if (current
 Frame
-s
  == 
-0
+1
 ) %7B%0A
@@ -2394,16 +2394,70 @@
 %09%09%7D%0A%0A%09%09%09
+System.out.printf(%22%5Cr %25.2f%25%25%22, (double)currentFrame / 
 totalFra
@@ -2459,18 +2459,23 @@
 alFrames
-++
+ * 100)
 ;%0A%09%09%7D%0A%09%7D

Add default video, and show percentage done.