I am trying to record screen in Java, using newest MonteMedia ScreenRecorder:
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.monte.media</artifactId>
<version>17.1</version>
</dependency>
My code is similar to many examples I could find of usage of the library - however older version as I found no examples for the new one - so packages are different:
import org.monte.media.math.Rational;
import org.monte.media.screenrecorder.ScreenRecorder;
import org.monte.media.av.Format;
import org.monte.media.av.FormatKeys.MediaType;
import static org.monte.media.av.codec.video.VideoFormatKeys.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class Screencorder {
private Screencorder() {}
public static ScreenRecorder startRecording(String fileName) throws IOException, AWTException {
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
Format fileFormat = new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI);
Format screenFormat = new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24, FrameRateKey,
Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (15 * 60));
Format mouseFormat = new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ScreenRecorder.ENCODING_BLACK_CURSOR, FrameRateKey, Rational.valueOf(30));
ScreenRecorder screenRecorder = new ScreenRecorder(gc, null,
fileFormat, screenFormat, mouseFormat,
null, new File(fileName));
screenRecorder.start();
return screenRecorder;
}
public static void stopRecording(ScreenRecorder screenRecorder) throws IOException {
screenRecorder.stop();
}
}
Running a test:
import org.junit.jupiter.api.Test;
import org.monte.media.screenrecorder.ScreenRecorder;
import java.time.Duration;
import static org.awaitility.Awaitility.await;
class ScreenRecordingTest {
@Test
void recordingTest() throws Exception {
ScreenRecorder recorder = Screencorder.startRecording("/tmp/");
Thread.sleep(2000)
Screencorder.stopRecording(recorder);
}
}
gives me:
java.io.IOException: Error no writer found for file format: Format{mimeType:video/avi,mediaType:FILE}.
I tried various JDKs - 17, 20 - without success.
Tried changing directory to a file, same error.
Tried a different MIME type - same error regardless.
Can someone help how to run this, so it records an .avi? Or other movie-like format.
I hope I am doing some obvious mistake.
Adding
module-info.javaand thoroughly cleaning up IntelliJ as described here got it working. A simple working example of this is shown here.Summarising:
you need Java >= 17 on your machine
maven module's
pom.xmlhas 3 main MonteMedia dependencies:src/main/javaneeds amodule.infowith those dependencies and some additional ones required by them + an export to use the module in a test:com.your.path.util.screenthere is a classScreencorderthat will record a movie with everything that's necessary to start a full screen recording instartandstopmethods:src/test/javaneeds amodule.infoas well - it will require the above module and test libraries + it will open itself for reflection tojunit:the test package needs to be different than for main code, for example
com.your.path.util.screen.test, the test class goes in hereIn case of problems with IntelliJ: after cleaning as described above, the module
com.your.path.util.screenneeds to have selected the MonteMedia dependencies in Module Settings > com.your.path.util.screen > Dependencies > chec 3 MonteMedia dependencies (same as inpom.xml)Now you can run
main()method in IntelliJ as well as the test.