I’ve been trying to extract frames from a video using OpenCV in an Ubuntu environment with Java 17, but all my attempts have been unsuccessful so far. The main issue is that ffmpeg is not detected by the OpenCV library on ubuntu.
Main
import org.opencv.core.Core;
import org.opencv.videoio.VideoCapture;
public class Main {
public static String videoPath = "";
static {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
videoPath = ClassLoader.getSystemResource("video.mp4").getPath();
} else {
videoPath = "/video.mp4";
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("/usr/lib/opencv_java470.so");
}
}
public static void main(String[] args) throws InterruptedException {
VideoCapture videoCapture = new VideoCapture(videoPath);
if (!videoCapture.isOpened()) {
System.out.println("Error opening video file.");
} else {
System.out.println("Everything is fine.");
}
System.out.println(Core.getBuildInformation());
Thread.sleep(1000000000000000000L);
}
}
Maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv-platform</artifactId>
<version>4.7.0-1.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Docker
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y openjdk-17-jdk
RUN apt-get update && apt-get install -y pkg-config
RUN apt-get update && apt-get install -y ffmpeg libavformat-dev libavcodec-dev libswscale-dev libavresample-dev
ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64
COPY target/*.jar /app.jar
COPY src/main/resources/video.mp4 /
COPY opencv_java470.so /usr/lib
ENV JAVA_TOOL_OPTIONS="-Djava.library.path=/usr/lib:/lib:/usr/local/lib:/usr/local:/usr/lib/x86_64-linux-gnu"
ENV LD_LIBRARY_PATH=/usr/lib:/lib:/usr/local/lib:/usr/local:/usr/lib/x86_64-linux-gnu
ENTRYPOINT [ "sh", "-c", "java $JAVA_TOOL_OPTIONS -jar /app.jar" ]
The application displays the following text (video is not opened \ FFMPEG NO)
Error opening video file.
2023-08-02T13:30:58.448538297Z
2023-08-02T13:30:58.448572597Z General configuration for OpenCV 4.7.0 =====================================
2023-08-02T13:30:58.448587327Z Version control: v4.7.0
2023-08-02T13:30:58.448590607Z
2023-08-02T13:30:58.448593217Z Platform:
2023-08-02T13:30:58.448596057Z Timestamp: 2023-03-27T23:13:34Z
2023-08-02T13:30:58.448598747Z Host: Linux 5.4.0-1103-azure x86_64
2023-08-02T13:30:58.448601377Z CMake: 3.25.2
2023-08-02T13:30:58.448604277Z CMake generator: Unix Makefiles
2023-08-02T13:30:58.448606837Z CMake build tool: /usr/bin/make
2023-08-02T13:30:58.448609327Z Configuration: RELEASE
2023-08-02T13:30:58.448611867Z
.......there is a lot of text......
2023-08-02T13:30:58.448772865Z
2023-08-02T13:30:58.448775355Z Video I/O:
2023-08-02T13:30:58.448777845Z DC1394: NO
2023-08-02T13:30:58.448780535Z FFMPEG: NO
2023-08-02T13:30:58.448783105Z avcodec: NO
2023-08-02T13:30:58.448785725Z avformat: NO
2023-08-02T13:30:58.448788295Z avutil: NO
2023-08-02T13:30:58.448790755Z swscale: NO
2023-08-02T13:30:58.448793295Z avresample: NO
2023-08-02T13:30:58.448795845Z GStreamer: NO
2023-08-02T13:30:58.448798255Z v4l/v4l2: YES (linux/videodev2.h)
2023-08-02T13:30:58.448800885Z
2023-08-02T13:30:58.448803525Z Parallel framework: pthreads
2023-08-02T13:30:58.448806345Z
............
2023-08-02T13:30:58.448859654Z Java: export all functions
2023-08-02T13:30:58.448862024Z ant: /usr/bin/ant (ver 1.10.5)
2023-08-02T13:30:58.448864504Z JNI: /opt/hostedtoolcache/jdk/8.0.362/x64/include /opt/hostedtoolcache/jdk/8.0.362/x64/include/linux /opt/hostedtoolcache/jdk/8.0.362/x64/include
2023-08-02T13:30:58.448867224Z Java wrappers: YES
2023-08-02T13:30:58.448869804Z Java tests: NO
2023-08-02T13:30:58.448872314Z
2023-08-02T13:30:58.448874734Z Install to: /usr/local
Couldn’t find a proper guide or example. Really hoping for some assistance.