aboutsummaryrefslogtreecommitdiff
path: root/src/getFrames.java
blob: e17f3489a2ea520c2b920f8e90947da025b88d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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();
	}
	

}