Last updated: March 2, 2025

<span aria-hidden="true" id="extract-audio-on-ios--macos-with-ffmpeg"></span>

# Extract audio on iOS & macOS with FFmpeg

![Kevin van Zonneveld](/assets/images/teammates/avatar-kvz-4.jpg?dpl=dpl_C6YH6XrtnwbHLJcm1LDKywQn4CB3)

#### Kevin van Zonneveld

Co-founder · Amsterdam, The Netherlands · Show bio

[](https://x.com/kvz)[](https://github.com/kvz)

FFmpeg is a versatile multimedia framework widely used for audio and video processing. On Apple platforms like iOS and macOS, FFmpeg enhances multimedia app development by enabling efficient audio extraction from video files. In this DevTip, we'll explore how to set up and use FFmpegKit—a popular FFmpeg wrapper—to extract audio streams in your Swift applications.

<span aria-hidden="true" id="introduction-to-ffmpeg-on-apple-platforms"></span>

## Introduction to FFmpeg on Apple platforms

FFmpeg is an open-source multimedia processing tool capable of handling various audio and video formats. It supports encoding, decoding, transcoding, and stream extraction. FFmpegKit simplifies integrating FFmpeg's capabilities into your Swift projects on iOS and macOS.

<span aria-hidden="true" id="setting-up-ffmpegkit-on-ios-and-macos"></span>

## Setting up FFmpegKit on iOS and macOS

We'll use FFmpegKit, a wrapper around FFmpeg, designed for mobile and desktop platforms.

<span aria-hidden="true" id="install-via-swift-package-manager"></span>

### Install via Swift Package Manager

In Xcode:

1. Open your project and navigate to `File > Add Packages`.
2. Enter the repository URL: `https://github.com/arthenica/ffmpeg-kit.git`.
3. Select the latest stable version and add it to your project.

<span aria-hidden="true" id="extracting-audio-streams-from-video-files"></span>

## Extracting audio streams from video files

Let's explore a practical example of extracting audio from a video file using FFmpegKit in Swift.

<span aria-hidden="true" id="step-by-step-guide-for-audio-extraction"></span>

### Step-by-step guide for audio extraction

Here's a Swift example demonstrating audio extraction:

```swift
import FFmpegKit

func extractAudio(from videoURL: URL, outputURL: URL, completion: @escaping (Bool, String?) -> Void) {
    let command = "-i \"\(videoURL.path)\" -vn -acodec copy \"\(outputURL.path)\""

    FFmpegKit.executeAsync(command) { session in
        guard let returnCode = session?.getReturnCode() else {
            completion(false, "No return code available")
            return
        }

        if returnCode.isValueSuccess() {
            completion(true, nil)
        } else {
            let failStackTrace = session?.getFailStackTrace()
            completion(false, failStackTrace)
        }
    }
}

```

<span aria-hidden="true" id="usage-example"></span>

### Usage example

```swift
let videoURL = Bundle.main.url(forResource: "sample-video", withExtension: "mp4")!
let outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("extracted-audio.m4a")

extractAudio(from: videoURL, outputURL: outputURL) { success, error in
    if success {
        print("Audio extraction successful: \(outputURL.path)")
    } else {
        print("Audio extraction failed: \(error ?? "Unknown error")")
    }
}

```

<span aria-hidden="true" id="handling-common-issues"></span>

## Handling common issues

Here are common issues and how to resolve them:

* **Permission Issues**: Ensure your app has the necessary permissions to read and write files.
* **Unsupported Formats**: Verify the input video format and audio codec compatibility.
* **Debugging FFmpeg Commands**: Use `session?.getOutput()` to inspect detailed FFmpeg logs.

<span aria-hidden="true" id="optimizing-performance"></span>

## Optimizing performance

To optimize performance:

* Use hardware acceleration when available (`-hwaccel videotoolbox`).
* Avoid unnecessary transcoding by copying audio streams directly (`-acodec copy`).
* Manage memory efficiently by processing large files asynchronously.

<span aria-hidden="true" id="additional-resources"></span>

## Additional resources

Integrating FFmpegKit into your iOS and macOS apps provides powerful multimedia processing capabilities. For further exploration, check out the[FFmpegKit documentation⁠](https://github.com/arthenica/ffmpeg-kit).

At Transloadit, we leverage FFmpeg in our[Audio Encoding](/services/audio-encoding.md) and[Video Encoding](/services/video-encoding.md) services, poweringRobots like [/audio/encode](/docs/robots/audio-encode.md) and[/video/encode](/docs/robots/video-encode.md).

\#ffmpeg#ios#macos#audio-extraction#swift#multimedia-development#audio-encoding-service#audio-encode-robot#video-encode-robot

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
