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.