I am trying to get an array of frames from a video. Here is my code:
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))
}
When I try with .mp4 it works fine but when I want to use .mov (I need the alpha channel) it says:
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})
I've tried with all the codecs available in premiere pro that supports transparency and pixel formats kCVPixelFormatType_32RGBA, kCVPixelFormatType_32BGRA, kCVPixelFormatType_32ARG and kCVPixelFormatType_32ABGR but still get the same error. Any suggestion?