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.
This Continuum-owned fork consumes the pinned CFFmpeg.xcframework generated
from FFmpeg 8.1.2 source. It deliberately does not discover Homebrew, pkg-config,
or another host FFmpeg installation.
just ffmpeg-sdkThe source URL, checksums, patch, build configuration, LGPL notices, and artifact hashes are owned by the Continuum repository tooling.
Continuum's source changes are published on the
continuum branch
of the owned fork. That branch intentionally does not distribute a prebuilt
FFmpeg binary. Continuum vendors the source and supplies its reproducibly built
CFFmpeg.xcframework at build time.
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.")