 class SampleViewController3: UIViewController {
     var context:VSContext = VSContext(device: MTLCreateSystemDefaultDevice()!)
     var runtime:VSRuntime?
+    var output:AVPlayerItemVideoOutput?
+    var playerItem:AVPlayerItem?
     var player:AVPlayer?
     lazy var renderer:VSRenderer = VSRenderer(context:self.context)
+    fileprivate lazy var textureCache:CVMetalTextureCache = {
+        var cache:CVMetalTextureCache? = nil
+        CVMetalTextureCacheCreate(nil, nil, self.context.device, nil, &cache)
+        return cache!
+    }()
 
     override func viewDidLoad() {
         super.viewDidLoad()

            mtkView.delegate = self
            context.pixelFormat = mtkView.colorPixelFormat
        }
    }
    
    @IBAction func importMovie(_ sender:UIBarButtonItem) {

                if status == AVKeyValueStatus.loaded {
                    print("didFinish, loaded")
                    let settings:[String:Any] = [kCVPixelBufferPixelFormatTypeKey as String:kCVPixelFormatType_32BGRA]
                    self.output = AVPlayerItemVideoOutput(outputSettings: settings)
                    self.playerItem = AVPlayerItem(asset: asset)
                    self.playerItem!.add(self.output!)
                    self.player = AVPlayer(playerItem: self.playerItem!)
                    self.player!.addPeriodicTimeObserver(forInterval: CMTime(seconds:1.0/30.0, preferredTimescale:600), queue: DispatchQueue.main, using: { (time) in
                        print("time", time)
                        guard let pixelBuffer = self.output?.copyPixelBuffer(forItemTime: self.playerItem!.currentTime(), itemTimeForDisplay: nil) else {
                            print("failed to copy pixel buffer")
                            return
                        }
                        
                        let width = CVPixelBufferGetWidth(pixelBuffer), height = CVPixelBufferGetHeight(pixelBuffer)
                        var metalTexture:CVMetalTexture? = nil
                        let status = CVMetalTextureCacheCreateTextureFromImage(nil, self.textureCache, pixelBuffer, nil,
                                                                               self.context.pixelFormat, width, height, 0, &metalTexture)
                        if let metalTexture = metalTexture, status == kCVReturnSuccess {
                            self.context.set(sourceImage: metalTexture)
                        } else {
                            print("VSVS: failed to create texture")
                        }
                        
                    })
                    self.player!.play()
                }
            }
        }
