我正在尝试从视频中获取一组帧。这是我的代码:
var frames = [UIImage]()
let url = NSBundle.mainBundle().URLForResource(name, withExtension: ext, subdirectory: "Assets")!
let asset = AVAsset(URL: url)
let reader = try AVAssetReader(asset: asset)
let output = AVAssetReaderVideoCompositionOutput(videoTracks: asset.tracksWithMediaType(AVMediaTypeVideo), videoSettings: nil)
output.videoComposition = AVVideoComposition(propertiesOfAsset: asset)
reader.addOutput(output)
reader.startReading()
let frameCount = Int(asset.duration.seconds*23.973)
let context = CIContext()
print("Asset reader: \(reader.error)")
for _ in 0..<frameCount{
let buff = output.copyNextSampleBuffer()
if buff == nil{
continue
}
let pixelBuffer = CMSampleBufferGetImageBuffer(buff!)! as CVPixelBuffer
let ciImage = CIImage(CVPixelBuffer: pixelBuffer)
let cgImgRef = context.createCGImage(ciImage, fromRect: CGRectMake(0, 0, CGFloat(CVPixelBufferGetWidth(pixelBuffer)), CGFloat(CVPixelBufferGetHeight(pixelBuffer))))
frames.append(UIImage(CGImage: cgImgRef))
}
当我尝试使用.mp4时,它可以正常工作,但是当我想使用.mov(我需要 alpha 通道)时,它会说:
Error Domain=AVFoundationErrorDomain Code=-11833 "Cannot Decode" UserInfo={NSLocalizedFailureReason=The decoder required for this media cannot be found., NSUnderlyingError=0x15e77fb90 {Error Domain=NSOSStatusErrorDomain Code=-12906 "(null)"}, AVErrorMediaTypeKey=vide, NSLocalizedDescription=Cannot Decode})
我已尝试使用 Premiere Pro 中支持透明度和像素格式kCVPixelFormatType_32RGBA、kCVPixelFormatType_32BGRA、kCVPixelFormatType_32ARG 和 kCVPixelFormatType_32ABGR的所有编解码器,但仍然出现相同的错误。有什么建议吗?