 
                         if let videoInput = self.assetWriterVideoInput {
 
-                            self.assetWriterPixelBufferAdaptor = self.makePixelBufferAdaptor(assetWriterInput: videoInput, videoTrackSettings: videoTrackSettings)
+                            let videoWidth = Int32(videoTrackSettings[AVVideoWidthKey] as! Int)
+                            let videoHeight = Int32(videoTrackSettings[AVVideoHeightKey] as! Int)
+                            let videoDimensions = CMVideoDimensions(width: videoWidth, height: videoHeight)
+
+                            self.assetWriterPixelBufferAdaptor = self.makePixelBufferAdaptor(assetWriterInput: videoInput, videoDimensions: videoDimensions)
 
                             if assetWriter.canAddInput(videoInput) {
                                 assetWriter.addInput(videoInput)


                        success = assetWriterPixelBufferAdaptor.appendPixelBuffer(pixelBuffer, withPresentationTime: timeStamp)

                        self.delegate?.writerCoordinatorDidRecording(self, seconds: self.recordingSeconds)

                    } else {

        }
    }

    private func makePixelBufferAdaptor(assetWriterInput input: AVAssetWriterInput, videoDimensions: CMVideoDimensions) -> AVAssetWriterInputPixelBufferAdaptor {

        let pixelBufferWidth = max(videoDimensions.height, videoDimensions.width)
        let pixelBufferHeight = min(videoDimensions.height, videoDimensions.width)

        // Use BGRA for the video in order to get realtime encoding.
        let sourcePixelBufferAttributes: [String: AnyObject] = [
            String(kCVPixelBufferPixelFormatTypeKey): Int(kCVPixelFormatType_32BGRA),
            String(kCVPixelBufferWidthKey): Int(pixelBufferWidth),
            String(kCVPixelBufferHeightKey): Int(pixelBufferHeight),
            String(kCVPixelFormatOpenGLESCompatibility): kCFBooleanTrue
        ]

