import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.jcodec.api.FrameGrab; import org.jcodec.api.JCodecException; import org.jcodec.api.awt.AWTSequenceEncoder; import org.jcodec.common.io.*; import org.jcodec.common.io.NIOUtils; import org.jcodec.common.io.SeekableByteChannel; import org.jcodec.common.model.Picture; import org.jcodec.common.model.Rational; import org.jcodec.common.model.*; import org.jcodec.scale.*; import org.jcodec.scale.AWTUtil; public class getFrames { //Splits the video into separate frames and dumps them in the frame-dump folder. //Function is currently unused in the program public static void splitVideo(File video) throws IOException, JCodecException{ double startSec = 0; int frameCount = 1292; FrameGrab grab = FrameGrab.createFrameGrab(NIOUtils.readableChannel(video)); grab.seekToSecondPrecise(startSec); for (int i = 0; i < frameCount; i++) { Picture picture = grab.getNativeFrame(); System.out.println(picture.getWidth() + "x" + picture.getHeight() + " " + picture.getColor()); BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture); ImageIO.write(bufferedImage, "png", new File("frame-dump/frame"+i+".png")); } } public static String encoded; public static void editVideo(File video) throws IOException, JCodecException{ SeekableByteChannel sbc = NIOUtils.readableChannel(video); FrameGrab grab = FrameGrab.createFrameGrab(sbc); AWTSequenceEncoder encoder = AWTSequenceEncoder.create30Fps(new File("output.mp4")); Picture picture; int i = 0; while ((picture = grab.getNativeFrame()) != null) { BufferedImage image = AWTUtil.toBufferedImage(picture); int width = image.getWidth(); int height = image.getHeight(); for(int y = 0; y < height; y++){ for(int x = 0; x < width; x++){ int p = image.getRGB(x,y); int a = (p>>24)&0xff; int r = (p>>16)&0xff; int g = (p>>8)&0xff; int b = p&0xff; int avg = (r+g+b)/3; //apply a different alpha value to each pixel p = (a<<20) | (avg<<16) | (avg<<8) | avg; image.setRGB(x, y, p); } } encoder.encodeImage(image); i++; encoded = "Encoded frame "+i; System.out.println("Encoded frame "+i); } encoder.finish(); } }