RepositorySwift
SU

sunlubo/SwiftFFmpeg

A Swift wrapper for the FFmpeg API

651stars
101forks
17issues
2views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: multimedia

#1 Repository of the month

1
TINIX TRENDING·Topic: ffmpeg-api

#1 Repository of the month

1
TINIX TRENDING·Topic: h264

#1 Repository of the month

SwiftFFmpeg

A Swift wrapper for the FFmpeg API.

Note: SwiftFFmpeg is still in development, and the API is not guaranteed to be stable. It's subject to change without warning.

Installation

You should install FFmpeg (Requires FFmpeg 7.1 or higher) before use this library, on macOS, you can:

brew install ffmpeg

Swift Package Manager

SwiftFFmpeg primarily uses SwiftPM as its build tool, so we recommend using that as well. If you want to depend on SwiftFFmpeg in your own project, it's as simple as adding a dependencies clause to your Package.swift:

dependencies: [
    .package(url: "https://github.com/sunlubo/SwiftFFmpeg.git", from: "1.0.0")
]

Documentation

Usage

import Foundation
import SwiftFFmpeg

if CommandLine.argc < 2 {
    print("Usage: \(CommandLine.arguments[0]) <input file>")
    exit(1)
}
let input = CommandLine.arguments[1]

let fmtCtx = try AVFormatContext(url: input)
try fmtCtx.findStreamInfo()

fmtCtx.dumpFormat(isOutput: false)

guard let stream = fmtCtx.videoStream else {
    fatalError("No video stream.")
}
guard let codec = AVCodec.findDecoderById(stream.codecParameters.codecId) else {
    fatalError("Codec not found.")
}
let codecCtx = AVCodecContext(codec: codec)
codecCtx.setParameters(stream.codecParameters)
try codecCtx.openCodec()

let pkt = AVPacket()
let frame = AVFrame()

while let _ = try? fmtCtx.readFrame(into: pkt) {
    defer { pkt.unref() }

    if pkt.streamIndex != stream.index {
        continue
    }

    try codecCtx.sendPacket(pkt)

    while true {
        do {
            try codecCtx.receiveFrame(frame)
        } catch let err as AVError where err == .tryAgain || err == .eof {
            break
        }

        let str = String(
            format: "Frame %3d (type=%@, size=%5d bytes) pts %4lld key_frame %d",
            codecCtx.frameNumber,
            frame.pictureType.description,
            frame.pktSize,
            frame.pts,
            frame.isKeyFrame
        )
        print(str)

        frame.unref()
    }
}

print("Done.")

Similar Projects

Discover other similar projects and AI models.

RepositoryC
FFmpeg/FFmpeg

Mirror of https://git.ffmpeg.org/ffmpeg.git

68.4k
Nov 29, 2026
RepositoryC
mpv-player/mpv

🎥 Command line media player

38.4k
Nov 29, 2026
RepositoryC#
Tyrrrz/YoutubeDownloader

Downloads videos and playlists from YouTube

16.8k
Dec 2, 2026
RepositoryPython
harry0703/MoneyPrinterTurbo

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

147.3k
Dec 1, 2026
RepositoryC
ffmpegwasm/ffmpeg.wasm

FFmpeg for browser, powered by WebAssembly

18.0k
Sep 29, 2026
RepositoryC
ffmpegwasm/ffmpeg.wasm

FFmpeg for browser, powered by WebAssembly

18.0k
Sep 29, 2026
RepositoryJupyter Notebook
leandromoreira/digital_video_introduction

A hands-on introduction to video technology: image, video, codec (av1, vp9, h265) and more (ffmpeg encoding). Translations: 🇺🇸 🇨🇳 🇯🇵 🇮🇹 🇰🇷 🇷🇺 🇧🇷 🇪🇸

16.4k
Nov 5, 2026
RepositorySwift
iina/iina

The modern video player for macOS.

47.4k
Dec 1, 2026
RepositoryTypeScript
mifi/lossless-cut

The swiss army knife of lossless video/audio editing

46.6k
Nov 25, 2026