A Swift wrapper for the FFmpeg API
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 年前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 5 年前 | ||
| 5 年前 | ||
| 7 年前 | ||
| 5 年前 | ||
| 7 年前 | ||
| 7 年前 | ||
| 3 年前 | ||
| 1 年前 | ||
| 1 年前 |
以下内容由 AI 翻译,如有问题请 点此提交 issue 反馈
SwiftFFmpeg
FFmpeg API 的 Swift 封装库。
注意:SwiftFFmpeg 尚处于开发阶段,API 的稳定性无法保证。可能会在未经通知的情况下发生变化。
安装
在使用这个库之前,您需要安装 FFmpeg(要求 FFmpeg 版本为 7.1 或更高版本)。在 macOS 上,您可以:
brew install ffmpeg
Swift 包管理器
SwiftFFmpeg 主要使用 SwiftPM 作为其构建工具,因此我们建议您也使用它。如果您想在您自己的项目中依赖 SwiftFFmpeg,只需在您的 Package.swift 中添加一个 dependencies 子句即可:
dependencies: [
.package(url: "https://github.com/sunlubo/SwiftFFmpeg.git", from: "1.0.0")
]
文档
使用方法
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.")
当然,请您提供需要翻译的英文文本以及您希望达到的风格要求,我将会根据您的指示进行翻译。