Skip to content

Using Multiple FFmpeg Implementations In The Same Application on Apple Platforms

Taner Sener edited this page Sep 28, 2022 · 2 revisions

On iOS, macOS and tvOS, FFmpeg inside FFmpegKit is packaged as a separate set of frameworks/libraries. This layout allows you to use FFmpeg alone without FFmpegKit.

Below you can see the contents of ffmpeg-kit-min package published in Cocoapods. As you can see, there are 8 different frameworks inside the package. ffmpegkit.xcframework belongs to FFmpegKit and the other 7 frameworks belong to FFmpeg.

You can use those 7 FFmpeg frameworks without FFmpegKit. But you can not use FFmpegKit framework alone because it depends on FFmpeg.

If you add another FFmpeg implementation into your application you may experience unexpected errors or crashes. It all depends on how the second FFmpeg implementation is packaged and in which order frameworks/libraries are loaded.

Remember that sometimes it may not be possible to resolve those errors.

1. Second FFmpeg as Single Framework/Library

Some libraries like MobileVLCKit does not use the same package layout as FFmpegKit and produce only a single framework/library. FFmpeg is included in that single library.

If you add one of those libraries into your application, you need to make sure that the second FFmpeg library is linked after FFmpegKit. If not, FFmpegKit will use the second FFmpeg implementation, not the one it is linked and packaged with and most probably will fail.

You need to edit OTHER_LDFLAGS flag in Pods-<Your App>.debug.xcconfig file of your project and move the second FFmpeg implementation after FFmpegKit.

For example, in order to use MobileVLCKit inside your application, edit OTHER_LDFLAGS and move -framework "MobileVLCKit" flag to the end. Or make sure that all FFmpegKit frameworks -framework "ffmpegkit” -framework "libavcodec" -framework "libavdevice" -framework "libavfilter" -framework "libavformat" -framework "libavutil" -framework "libswresample" -framework "libswscale" are ahead of -framework "MobileVLCKit". If you do that, you will be able to use both libraries without any major issues.

1.1 Post Install Script for MobileVLCKit

Instead of manually editing OTHER_LDFLAGS, it is possible to define a post_install script in Podfile to make the necessary adjustments. Use the following one if you use FFmpegKit and MobileVLCKit in the same application. It will re-arrange OTHER_LDFLAGS and resolve the incompatibilities. Do not forget to replace MYPROJECT with your target name.

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-MYPROJECT"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                xcconfig_path = config.base_configuration_reference.real_path

                # read from xcconfig to build_settings dictionary
                build_settings = Hash[*File.read(xcconfig_path).lines.map{|x| x.split(/\s*=\s*/, 2)}.flatten]

                # modify OTHER_LDFLAGS
                vlc_flag = ' -framework "MobileVLCKit"'
                build_settings['OTHER_LDFLAGS'].gsub!(vlc_flag, "")
                build_settings['OTHER_LDFLAGS'].gsub!("\n", "")
                build_settings['OTHER_LDFLAGS'] += vlc_flag + "\n"

                # write build_settings dictionary to xcconfig
                File.open(xcconfig_path, "w") do |file|
                  build_settings.each do |key,value|
                    file.write(key + " = " + value)
                  end
                end
            end
        end
    end
end

2. Second FFmpeg as Multiple Frameworks/Libraries

If you add another FFmpeg implementation into your application where FFmpeg is packaged as a separate set of libraries like FFmpegKit, then it may not be possible to use that library with FFmpegKit. Technically, applications can not load multiple versions of the same library and one of the versions needs to be ignored/excluded.

In this particular case, there are two sets of FFmpeg libraries inside your application and only one of them can be used. You need to edit OTHER_LDFLAGS flag in Pods-<Your App>.debug.xcconfig file of your project, change the order in which libraries are loaded and test if FFmpegKit can use the second FFmpeg implementation or the second FFmpeg library can use FFmpegKit's FFmpeg implementation. If either of these two works then you can ignore/exclude the other FFmpeg implementation and use both libraries. If not, then you will not be able to use FFmpegKit with that library in the same application.

FFmpegKit uses -framework "libavcodec" -framework "libavdevice" -framework "libavfilter" -framework "libavformat" -framework "libavutil" -framework "libswresample" -framework "libswscale" flags in OTHER_LDFLAGS to link FFmpeg. You may need to move or delete that section in your tests.

Clone this wiki locally