Repository navigation
Expand file tree
/
Copy pathProduceGif.java
More file actions
87 lines (69 loc) · 2.47 KB
/
Copy pathProduceGif.java
File metadata and controls
87 lines (69 loc) · 2.47 KB
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
package examples.programmatic;
import com.github.kokorin.jaffree.ffmpeg.Frame;
import com.github.kokorin.jaffree.ffmpeg.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ProduceGif {
private final Path ffmpegBin;
public ProduceGif(Path ffmpegBin) {
this.ffmpegBin = ffmpegBin;
}
public void execute() {
Path output = Paths.get("test.gif");
FrameProducer producer = new FrameProducer() {
private long frameCounter = 0;
@Override
public List<Stream> produceStreams() {
return Collections.singletonList(new Stream()
.setType(Stream.Type.VIDEO)
.setTimebase(1000L)
.setWidth(320)
.setHeight(240)
);
}
@Override
public Frame produce() {
if (frameCounter > 30) {
return null;
}
System.out.println("Creating frame " + frameCounter);
BufferedImage image = new BufferedImage(320, 240, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = image.createGraphics();
graphics.setPaint(new Color(frameCounter * 1.0f / 30, 0, 0));
graphics.fillRect(0, 0, 320, 240);
Frame videoFrame = new Frame(0, frameCounter * 1000 / 10, image);
frameCounter++;
return videoFrame;
}
};
FFmpegResult result = FFmpeg.atPath(ffmpegBin)
.addInput(
FrameInput.withProducer(producer)
)
.addOutput(
UrlOutput.toPath(output)
)
.execute();
}
public static void main(String[] args) {
Iterator<String> argIter = Arrays.asList(args).iterator();
String ffmpegBin = null;
while (argIter.hasNext()) {
String arg = argIter.next();
if ("-ffmpeg_bin".equals(arg)) {
ffmpegBin = argIter.next();
}
}
if (ffmpegBin == null) {
System.err.println("Arguments: -ffmpeg_bin </path/to/ffmpeg/bin>");
return;
}
new ProduceGif(Paths.get(ffmpegBin)).execute();
}
}