I need to get an image as preview of the videos loaded by the users, it doesn't need to be a great thumbnail because it's just for a chat application, so I had in mind that when a user sends a message that contains an mp4 I process it and save a random frame as well.
I googled a bit and everyone is using ffmpeg but that's an external software and you can only interact with it with java, my project needs to be stand alone I don't want it to rely on the fact that the server has ffmpeg installed, so I went back in time and found JavaFX, but all the snippets online don't specify the version they're using and chatGPT is useless as always, can you guys show me how to do it or at least explain to me how it works so that I can code it myself? I'm using this dependency
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>16</version> <!-- the version chatGPT wrote but you can change that -->
</dependency>
if you have a different solution altogether that's great too I only care about saving a random frame from the video with the specified name at the specified path, thanks in advance
I tried this method but it gives plenty of errors probably because of a version mismatch
private byte[] getFrameFromVideo(String videoFilePath, int targetTimeSeconds) throws IOException {
try {
Media media = new Media(new File(videoFilePath).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
CountDownLatch latch = new CountDownLatch(1);
mediaPlayer.setOnReady(() -> {
mediaPlayer.pause();
mediaPlayer.setStartTime(mediaPlayer.getTotalDuration().multiply(targetTimeSeconds * 1.0 / media.getDuration().toSeconds()));
mediaPlayer.setStopTime(mediaPlayer.getStartTime().add(mediaPlayer.getTotalDuration().multiply(1.0 / media.getDuration().toSeconds())));
latch.countDown();
});
mediaPlayer.setOnEndOfMedia(() -> {
BufferedImage bufferedImage = new BufferedImage(mediaPlayer.getMedia().getWidth(), mediaPlayer.getMedia().getHeight(), BufferedImage.TYPE_3BYTE_BGR);
FXImage fxImage = mediaPlayer.snapshot(null);
fxImage.pixelReaderProperty().get().getPixels(0, 0, (int) fxImage.getWidth(), (int) fxImage.getHeight(), javafx.scene.image.PixelFormat.getByteRgbInstance(), bufferedImage.getRaster().getDataBuffer());
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
mediaPlayer.dispose();
latch.countDown();
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
});
mediaPlayer.play();
latch.await();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}