swift – 通过AVFoundation获取视频中的帧数

前端之家收集整理的这篇文章主要介绍了swift – 通过AVFoundation获取视频中的帧数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我刚刚打开的视频中找到帧数,而不解码所有帧.

我用AVAsset打开然后为视频获取AVAssetTrack.接下来是什么 ?

一种相对昂贵的方法是使用AVAssetReader读取所有帧并随时计算它们.
let asset = AVAsset(url: url)
let assetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first!
let assetReader = try! AVAssetReader(asset: self)
let assetReaderOutputSettings = [
    kCVPixelBufferPixelFormatTypeKey as String: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)
]
let assetReaderOutput = AVAssetReaderTrackOutput(track: assetTrack,outputSettings: assetReaderOutputSettings)
assetReaderOutput.alwaysCopiesSampleData = false
assetReader.addOutput(assetReaderOutput)
assetReader.startReading()

var frameCount = 0
var sample: CMSampleBufferRef? = assetReaderOutput.copyNextSampleBuffer()

while (sample != nil) {
    frameCount++
    sample = assetReaderOutput.copyNextSampleBuffer()
}

// now you have frame count
print(frameCount)
原文链接:https://www.f2er.com/swift/318753.html

猜你在找的Swift相关文章