Where/How do I find missing ffmpeg dependencies?

I’m attempting to use a package which relies on the following ffmpeg libraries:

  • libavcodec
  • libavformat
  • libavutil
  • libswscale

but can’t seem to locate them either in ‘22.05’ or unstable. Given that ffmpeg of various versions is included, I’m fairly certain that these libs must also be available, albeit named differently?

I’ve also tried using ‘nix-locate’ but haven’t found a solution.

Any help or suggestions on how to find dependencies like this would be much appreciated.

As you’ve said they are just part of ffmpeg. It’s not clear how the libraries are missing, so maybe show us how you’re trying to use it and see what’s wrong?

3 Likes

We’re building dotnet 6.x applications and are attempting to use the OpenCVSharp nuget package which wraps the native OpenCV libraries.

Of course, ideally, we would build a specific version of these libraries for NixOS 22.05, but wanted to see if we could get a basic example working with the packages that are currently available.

More specifically, we’re using OpenCVSharp4/4.6.0.20220608 and OpenCVSharp4.runtime.ubuntu.18.04-x64/4.6.0.20220608 nuget packages.

To test, generate a console app w/ opencvsharp deps

dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.ubuntu.18.04-x64

Replace the contents of Program.cs with

using OpenCvSharp;

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

// Create a OpenCVSharp Mat type; See exception.
var mat = new Mat();

Run the app

dotnet run

Thrown exception

Hello, World!
Unhandled exception. System.TypeInitializationException: The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory
   at OpenCvSharp.Internal.NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
   at OpenCvSharp.Internal.ExceptionHandler.RegisterExceptionCallback()
   at OpenCvSharp.Internal.NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths)
   at OpenCvSharp.Internal.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at OpenCvSharp.Internal.NativeMethods.core_Mat_new1(IntPtr& returnValue)
   at OpenCvSharp.Mat..ctor()
   at Program.<Main>$(String[] args) in /home/talos/projects/opencvsharp-hello-world/ConsoleApp01/Program.cs:line 7

Check for missing dependencies

ldd   $HOME/.nuget/packages/opencvsharp4.runtime.ubuntu.18.04-x64/4.6.0.20220608/runtimes/ubuntu.18.04-x64/native/libOpenCvSharpExtern.so

libcairo.so.2 => not found
libgdk_pixbuf-2.0.so.0 => not found
libdc1394.so.22 => not found
libavcodec.so.57 => not found
libavformat.so.57 => not found
libavutil.so.55 => not found
libswscale.so.4 => not found
libjpeg.so.8 => not found
libpng16.so.16 => not found
libIlmImf-2_2.so.22 => not found

Flake.nix (note, the inclusion of the pkgs.ffmpeg_5 package).

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils}:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system}; in 
      rec {

        lib = pkgs.lib;
        
        devShell = pkgs.mkShell rec
        { 
            name ="OpenCV DevEnv";
            buildInputs = with pkgs; [ 
                # Tooling
                pkgs.dotnet-sdk

                # OpenCV dependencies
                pkgs.libgeotiff
                pkgs.xorg.libXt
                pkgs.libusb1
                pkgs.tesseract4
                pkgs.libtiff
                pkgs.stdenv.cc.cc.lib
                pkgs.gtk2
                pkgs.libpng12
                pkgs.ffmpeg_5
              ];
        };
      });
}

We’ve tried a variety of different ffmpeg packages, but got tripped up on the same issue.

Seems that you’re using binary libraries, which couldn’t work because there’s no way they’ll know the path that the ffmpeg libraries are put into. They’re instead trying to find the files from the ‘default’ paths which for NixOS is nothing. Ideally you can build all the .so stuff from source.

If you really want the binaries add LD_LIBRARY_PATH = lib.makeLibPath [ pkgs.ffmpef ];, though that would require you to be always in this nix-shell to use it.

There are probably ways to use patchelf to patch up the nuget package files to make them find the correct ffmpeg, though I have no experience with them.

1 Like
Hosted by Flying Circus.