Closed SingleKey closed 11 months ago
Hello,
yes it is possible, but you need something that merges a bunch of images into a video. A quick Google search lead me to this blog post https://cooltrickshome.blogspot.com/2016/12/images-to-movie-converter-using-java.html
You have to setup a maven project and add bytedeco
.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.Krassnig</groupId>
<artifactId>CodeDraw</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Then use this class to capture the individual frames.
package org.example;
import codedraw.Image;
import codedraw.ImageFormat;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameRecorder;
import org.bytedeco.javacv.OpenCVFrameConverter;
import java.io.File;
import static org.bytedeco.javacpp.opencv_imgcodecs.*;
public class Recorder {
public Recorder(String filename, int width, int height, int frameRate) {
recorder = new FFmpegFrameRecorder(filename, width, height);
recorder.setFrameRate(frameRate);
recorder.setVideoBitrate(9000);
recorder.setFormat("mp4");
recorder.setVideoQuality(0);
try {
recorder.start();
} catch (FrameRecorder.Exception e) {
throw new RuntimeException(e);
}
}
private OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
private FFmpegFrameRecorder recorder;
private String temporaryFile = "./tmp";
public void captureFrame(Image codeDrawImage) {
Image.save(codeDrawImage, temporaryFile, ImageFormat.JPG);
try {
recorder.record(grabberConverter.convert(cvLoadImage(temporaryFile)));
} catch (FrameRecorder.Exception e) {
throw new RuntimeException(e);
}
}
public void stop() {
new File(temporaryFile).delete();
try {
recorder.stop();
} catch (FrameRecorder.Exception e) {
throw new RuntimeException(e);
}
}
}
Here is an example program that generates a video of the output produces by CodeDraw.
import codedraw.*;
public class Main {
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
cd.setColor(Palette.RED);
EventScanner es = cd.getEventScanner();
int FPS = 60;
Recorder recorder = new Recorder("./out.mp4", cd.getWidth(), cd.getHeight(), FPS);
int i = 0;
while (!cd.isClosed() && i < (FPS * 10)) {
while (es.hasEventNow()) {
if (es.hasMouseMoveEvent()) {
MouseMoveEvent mm = es.nextMouseMoveEvent();
cd.fillSquare(mm.getX() - 4, mm.getY() - 4, 8);
}
else {
es.nextEvent();
}
}
recorder.captureFrame(cd);
cd.show(1000 / FPS);
i++;
}
recorder.stop();
cd.close();
}
}
Then you can use ffmpeg https://ffmpeg.org/ to add audio to your video file or to convert that video file into a GIF. Note that GIF files cannot have audio as far as I know.
A drawback of this solution is that the framerate of normal CodeDraw programs is dependent on the time it takes execute that program, that means the framerate of the video will not exactly match the output you saw in your CodeDraw program. This could be avoided by using the Animation
interface which has a scheduler in the background that tries to match the given framerate.
As an alternative you can also use OBS https://obsproject.com/ to record the CodeDraw window.
Hope this helps
Thank you, this is very useful. I need to add that I am using version 1.1 of JavaCV.
Hello, may I ask if it is possible to export the results as a video file, then add audio, and export as a GIF file~ WYSIWYG