1

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?

kemkriszt
  • 280
  • 3
  • 17
  • Some files may not read from NSBundle.mainBundle(), Use document directory let paths = *NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as NSString* – Jan Aug 29 '16 at 09:41
  • I have my video as an Asset. How can I load it from the Documents directory? I've checked in a simulator and they do not appear there. – kemkriszt Aug 29 '16 at 09:52
  • Where is your video located in real? – Jan Aug 29 '16 at 11:23
  • For simply trying you can copy your .mov video into document directory and play from there, and check it is working or not – Jan Aug 29 '16 at 11:24
  • My video is in a folder in project hierarchy. I've tested to copy it to documents directory but it hasn't solved the problem... Still says that it can not be decoded because the needed decoder is missing – kemkriszt Aug 30 '16 at 07:19

0 Answers0