Giter Site home page Giter Site logo

Comments (2)

Krassnig avatar Krassnig commented on June 19, 2024

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

from codedraw.

SingleKey avatar SingleKey commented on June 19, 2024

Thank you, this is very useful. I need to add that I am using version 1.1 of JavaCV.

from codedraw.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.