Skip to content

FFmpeg Runtime

WangBin edited this page Sep 11, 2024 · 25 revisions

FFmpeg is heavily used in some mdk modules, for example demuxer, muxer and software decoder. mdk main library does not depends on ffmpeg, and all modules can be implemented by platform dependent APIs. A single ffmpeg share library is included in sdk, and will be loaded at runtime(except iOS).

Select FFmpeg Runtime

You can use the bundled libffmpeg if no special demand. To use system ffmpeg(4.0 or later) libraries or the ones in your app:

  • delete ffmpeg runtime in sdk
  • If existing ffmpeg libraries are in library loading search dirs, and their names are standard, then they will be loaded automatically.
  • Otherwise, you can choose one of
    • set environment vars AVUTIL_LIB, AVCODEC_LIB, AVFORMAT_LIB, AVFILTER_LIB, SWRESAMPLE_LIB and SWSCALE_LIB with ffmpeg module path.
    • call SetGlobalOption(key, value) once(usually before other mdk calls), where key is avutil_lib, avcodec_lib, avformat_lib, avfilter_lib, swresample_lib and swscale_lib, value is corresponding path/filename. For example SetGlobalOption("avutil_lib", "/opt/lib/libavutil.so.56"). The key can also have no _lib suffix since 0.16.1.
    • call SetGlobalOption(key, value) once(usually before other mdk calls), where key is avutil, avcodec, avformat, avfilter, swresample and swscale, value is corresponding module hande(from dlopen(), GetModuleHandle() or LoadLibrary()) of type void*, or library path/filename(since 0.16.1). For example SetGlobalOption("avutil", (void*)avutil_handle). You may need RTLD_NOLOAD for macOS 10.15+ and link your own ffmpeg libs.

Once ffmpeg is loaded, SetGlobalOption(key, value) can be called again if you need.

Runtime Lookup

If ffmpeg any module is not set manually by environment vars or SetGlobalOption(key, value), it's searched in the following order

  • dir: current mdk module dir > mdk framework dir(apple) > system default search dir
  • file name(since 0.12.0): in each dir load single ffmpeg library > ffmpeg module library, the closest and highest version(compared with build version, usually latest release) > no version. iterate dirs first.

For example, on windows, mdk will search avutil like this

  • if build against ffmpeg 6.x headers: ffmpeg-6.dll in mdk.dll dir=>avutil-58.dll in mdk.dll dir=>ffmpeg-7.dll in mdk.dll dir=>avutil-59.dll in mdk.dll dir=>ffmpeg-5.dll in mdk.dll dir=>avutil-57.dll in mdk.dll dir=>ffmpeg-4.dll in mdk.dll dir=>avutil-56.dll in mdk.dll dir=>ffmpeg.dll in mdk.dll dir=>avutil.dll in mdk.dll dir

For apple platforms, FFmpeg.framework will also be searched(after libffmpeg.*.dylib and lib${avmodule}.*.dylib on macOS).

macOS

Since macOS 10.15, dlopen is not allowed in app by default, so mdk has to (weak)link against ffmpeg and standard ffmpeg libraries. Make sure ffmpeg libs are in mdk's rpath list(@loader_path/Libraries, @loader_path, @executable_path/../Frameworks, /usr/local/lib) so they will be automatically loaded, or you can load manually

iOS, tvOS, visionOS

FFmpeg is embeded into mdk. You can bundle your own FFmpeg.framework(add it in xcode as a dependency), then FFmpeg.framework will be used

Build FFmpeg

https://github.com/wang-bin/avbuild

Clone this wiki locally