/
SequenceEncoderDemo.java
65 lines (58 loc) · 2.29 KB
/
SequenceEncoderDemo.java
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
package org.jcodec.samples.gen;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.jcodec.api.awt.AWTSequenceEncoder8Bit;
import org.jcodec.common.tools.MainUtils;
import org.jcodec.common.tools.MainUtils.Cmd;
import org.jcodec.common.tools.MainUtils.Flag;
/**
* This class is part of JCodec ( www.jcodec.org ) This software is distributed
* under FreeBSD License
*
* A demo of SequenceEncoder that generates a video with a bouncing ball
*
* @author Stan Vitvitskyy
*
*/
public class SequenceEncoderDemo {
private static final Flag FLAG_FRAMES = new Flag("n-frames", "frames", "Total frames to encode");
private static final Flag[] FLAGS = new MainUtils.Flag[] {FLAG_FRAMES};
public static void main(String[] args) throws IOException {
Cmd cmd = MainUtils.parseArguments(args, FLAGS);
if (cmd.argsLength() < 1) {
MainUtils.printHelpVarArgs(FLAGS, "output file");
return;
}
final int speed = 4;
final int ballSize = 40;
AWTSequenceEncoder8Bit enc = AWTSequenceEncoder8Bit.create25Fps(new File(cmd.getArg(0)));
enc.getEncoder().setKeyInterval(25);
int framesToEncode = cmd.getIntegerFlagD(FLAG_FRAMES, 100);
long totalNano = 0;
BufferedImage image = new BufferedImage(640, 480, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0, x = 0, y = 0, incX = speed, incY = speed; i < framesToEncode; i++, x += incX, y += incY) {
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.YELLOW);
if (x >= image.getWidth() - ballSize)
incX = -speed;
if (y >= image.getHeight() - ballSize)
incY = -speed;
if (x <= 0)
incX = speed;
if (y <= 0)
incY = speed;
g.fillOval(x, y, ballSize, ballSize);
long start = System.nanoTime();
enc.encodeImage(image);
totalNano += System.nanoTime() - start;
}
enc.finish();
System.out.println("FPS: " + ((1000000000L * framesToEncode) / totalNano));
}
}