Search code examples
javaffmpegjava-8

Can't fix this ffmpeg, NoClassDefFoundError org.bytedeco.ffmpeg.global.avutil


I am trying to get the video duration with this library import org.bytedeco.javacv.FFmpegFrameGrabber;

When I upload this mp4 video (https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4), I get this error message: java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.ffmpeg.global.avutil

It will break when trying to get the 'grabber':

private void videoInfo(MultipartFile file) {
    try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(file.getInputStream())) { // on this line it will break :(
        grabber.start();
        long durationMs = grabber.getLengthInTime();
    } catch (FrameGrabber.Exception e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

This is my build.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.9'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.nob234'
version = '0.0.1-SNAPSHOT'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.9'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'org.postgresql:postgresql'
    // for logging
    implementation 'org.slf4j:slf4j-api:1.7.30'
    implementation 'org.slf4j:jcl-over-slf4j:1.7.30'
    implementation 'org.slf4j:log4j-over-slf4j:1.7.30'
    implementation 'ch.qos.logback:logback-classic:1.2.3'
    implementation 'org.bytedeco:javacv:1.5.8'
}

tasks.named('test') {
    useJUnitPlatform()
}

This is my ffmpeg version:

ffmpeg version 6.0-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil      58.  2.100 / 58.  2.100
libavcodec     60.  3.100 / 60.  3.100
libavformat    60.  3.100 / 60.  3.100
libavdevice    60.  1.100 / 60.  1.100
libavfilter     9.  3.100 /  9.  3.100
libswscale      7.  1.100 /  7.  1.100
libswresample   4. 10.100 /  4. 10.100
libpostproc    57.  1.100 / 57.  1.100

Please keep in mind that I use Java 8 in this project and I hope this issue is reproducible. If you want more info please leave a comment.


Solution

  • You need a dependency to the jar containing org.bytedeco.ffmpeg.global.avutil

    Replace

    implementation 'org.bytedeco:javacv:1.5.8'
    

    With

    implementation 'org.bytedeco:javacv-platform:1.5.8'
    

    See bytedeco downloads page