Logo
iOS / macOS Native Development

Xcode: Integrate FFmpeg 7+ with Swift

A complete guide to integrating pre-compiled FFmpeg C libraries into an Xcode project, configuring build settings, setting up the Objective-C bridging header, and executing FFmpeg functions natively in Swift.

Prerequisites & Tools

Ensure you have Xcode installed and the pre-compiled FFmpeg libraries for iOS/macOS.

Integration Note: This tutorial assumes you have already compiled FFmpeg for iOS (arm64). If you haven't, it is highly recommended to use the FFmpeg-Kit build scripts to generate the XCFrameworks first.

Step 1: Project Directory Structure

Organize your FFmpeg headers and libraries inside your Xcode project folder.

MySwiftApp/ # 你的 Xcode 项目根目录
├── MySwiftApp.xcodeproj
├── MySwiftApp/
├── AppDelegate.swift
├── ViewController.swift
├── MySwiftApp-Bridging-Header.h # Objective-C 桥接文件
└── FFmpeg-iOS/ # 手动拖入项目的 FFmpeg 依赖
├── include/ # 存放 libavcodec, libavformat 等头文件
│ ├── libavcodec/
│ └── libavformat/
└── lib/ # 存放编译好的 .a 静态库
├── libavcodec.a
├── libavformat.a
├── libavutil.a
├── libswscale.a
└── libswresample.a

提示:将 `FFmpeg-iOS` 文件夹拖入 Xcode 侧边栏时,弹窗中的选项请选择 "Create groups" 而不是 "Create folder references"。

Step 2: Configure Build Settings & Dependencies

Tell Xcode where to find the FFmpeg headers and libraries, and link necessary Apple frameworks.

1. Search Paths (搜索路径)

  • Header Search Paths
    $(SRCROOT)/FFmpeg-iOS/include
  • Library Search Paths
    $(SRCROOT)/FFmpeg-iOS/lib
  • Objective-C Bridging Header
    MySwiftApp/MySwiftApp-Bridging-Header.h

2. Link Binary With Libraries

Go to Build Phases -> Link Binary With Libraries, and add the following frameworks:

VideoToolbox.framework
AudioToolbox.framework
CoreMedia.framework
AVFoundation.framework
libz.tbd # zlib
libbz2.tbd
libiconv.tbd

Step 3: Objective-C Bridging Header

Expose FFmpeg C APIs to Swift.

MySwiftApp-Bridging-Header.h
#ifndef MySwiftApp_Bridging_Header_h
#define MySwiftApp_Bridging_Header_h

#import <Foundation/Foundation.h>

// 包含你需要的 FFmpeg 核心头文件
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>

#endif /* MySwiftApp_Bridging_Header_h */

Step 4: Swift Implementation & Verification

Create a Swift wrapper to interact with FFmpeg securely.

FFmpegManager.swift
import Foundation

class FFmpegManager {

    /// 获取 FFmpeg 版本号与配置信息 (用于测试集成是否成功)
    static func getFFmpegVersion() -> String {
        guard let versionPtr = av_version_info() else {
            return "Unknown"
        }
        let version = String(cString: versionPtr)
        return version
    }

    /// 测试打开一个网络流 (需要启用网络权限)
    static func testNetworkStream(url: String) {
        // 1. 初始化网络模块 (FFmpeg 4.0 以后大部分 init 已废弃,但 network_init 保留)
        avformat_network_init()

        var formatContext: UnsafeMutablePointer<AVFormatContext>? = nil
        let urlString = url.cString(using: .utf8)

        // 2. 尝试打开输入流
        let result = avformat_open_input(&formatContext, urlString, nil, nil)
        if result < 0 {
            print("❌ Failed to open stream, error code: \\(result)")
            return
        }

        print("✅ Successfully opened stream!")

        // 3. 获取流信息
        if avformat_find_stream_info(formatContext, nil) >= 0 {
            let duration = formatContext?.pointee.duration ?? 0
            print("Video duration: \\(duration) microseconds")
        }

        // 4. 释放资源
        avformat_close_input(&formatContext)
        avformat_network_deinit()
    }
}
// 在 ViewController 中调用:
let version = FFmpegManager.getFFmpegVersion()
print("FFmpeg Version: \\(version)")
// 如果控制台成功打印了长长一串类似 "7.0.1" 以及 configure 信息,说明你的集成已经大功告成!

Explore More#mobile-dev

Integrating FFmpeg 7+ with Kotlin via JNI

Article

A complete, step-by-step visual guide to integrating pre-compiled FFmpeg 7 dynamic libraries (.so) into a modern Android project using CMake, JNI, and Jetpack Compose.

Android FFmpeg + C++: Video Cropping and Filter Processing

Article

A detailed demonstration of how to use FFmpeg and C++ in an Android project to implement video cropping, filter processing, and progress callbacks.

Integrating OpenCV 4.x in Android Studio

Article

A detailed step-by-step guide on how to integrate OpenCV 4.x into an Android Studio project, configure CMake and JNI, and implement image processing features.

Using OpenCV for Image Sharpening in Android

Article

A detailed step-by-step guide on how to integrate OpenCV into an Android project and implement image sharpening functionality.

Android OpenCV + FFmpeg 7: Making Videos Clearer from Blurry

Article

A detailed demonstration of how to use OpenCV and FFmpeg 7 in an Android project for video processing, achieving clarity from blurriness.

FFmpeg Android + OpenSSL Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source and integrate OpenSSL on the Android platform for HTTPS streaming support.

FFmpeg Xcode + Swift Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source using Xcode and Swift on macOS, and integrate it into an iOS project.

Android Image Similarity Detection and Grouping

Article

A detailed demonstration of how to use OpenCV in an Android project to implement image similarity detection and grouping features.