Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sunlubo/SwiftFFmpeg
A Swift wrapper for the FFmpeg API
https://github.com/sunlubo/SwiftFFmpeg
audio av1 ffmpeg ffmpeg-api h264 hevc mp4 multimedia swift video webm
Last synced: 12 days ago
JSON representation
A Swift wrapper for the FFmpeg API
- Host: GitHub
- URL: https://github.com/sunlubo/SwiftFFmpeg
- Owner: sunlubo
- License: apache-2.0
- Created: 2018-07-08T10:56:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-26T10:48:35.000Z (over 1 year ago)
- Last Synced: 2024-10-01T06:33:40.429Z (about 1 month ago)
- Topics: audio, av1, ffmpeg, ffmpeg-api, h264, hevc, mp4, multimedia, swift, video, webm
- Language: Swift
- Size: 24.2 MB
- Stars: 515
- Watchers: 14
- Forks: 83
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](http://ffmpeg.org/) (Requires FFmpeg 4.0 or higher) before use this library, on macOS, you can:
```bash
brew install ffmpeg
```### Swift Package Manager
SwiftFFmpeg primarily uses [SwiftPM](https://swift.org/package-manager/) 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`:
```swift
dependencies: [
.package(url: "https://github.com/sunlubo/SwiftFFmpeg.git", from: "1.0.0")
]
```## Documentation
- [API documentation](https://sunlubo.github.io/SwiftFFmpeg)
## Usage
```swift
import Foundation
import SwiftFFmpegif CommandLine.argc < 2 {
print("Usage: \(CommandLine.arguments[0]) ")
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.")
```