Frames from Video Using OpenCV in Ubuntu with Java 17

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.


import org.opencv.core.Core;
import org.opencv.videoio.VideoCapture;

public class Main {
  public static String videoPath = "";
  static {
    String osName = System.getProperty("").toLowerCase();
    if (osName.contains("win")) {
      videoPath = ClassLoader.getSystemResource("video.mp4").getPath();
    } else {
      videoPath = "/video.mp4";
//      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

  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.");


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
    <relativePath/> <!-- lookup parent from repository -->




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 /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.448572597Z General configuration for OpenCV 4.7.0 =====================================
2023-08-02T13:30:58.448587327Z   Version control:               v4.7.0
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
.......there is a lot of text......
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.448803525Z   Parallel framework:            pthreads
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.448874734Z   Install to:                    /usr/local

Couldn’t find a proper guide or example. Really hoping for some assistance.

does this mean, you’re using a prebuild opencv-java from there ?

the cmake output you show, clearly says, that this was built without any capabilities apart from v4l (usb/live webcam)

it’s no use installing any ffmpeg version after the libs got built, you need the dev versions present while building opencv

long story short, if you need ffmpeg or gstreamer support, you must build from src, not rely on maven

1 Like

Thank you for your response! Now I’ve figured it out. I generated everything I needed, following the advice from Docker + Java + OpenCV, no opencv_java342 in java.library.path - Stack Overflow. Then, I added the generated JAR to Maven as advised