我正在尝试从视频中获取一系列帧。 这是我的代码:
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 ,但仍然会收到相同的错误。有什么建议吗?