我试图在我刚刚打开的视频中找到帧数,而不解码所有帧.
我用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)