title: Rotate CMSampleBuffer by arbitrary angle and append to AVAssetWriterInput in swift 3
body: <p>I convert the sample buffer to a <code>CGContext</code>. Then I apply a transformation to the context and create a <code>CIImage</code> from that, which in turn gets displayed in an <code>UIImageView</code>. </p>
  
  <p>At the same time I want to append this to the <code>AVAssetWriterInput</code> to create a movie of these transformations.</p>
  
  <p>So far the transformations I apply to the context have no effect whatsoever. When I display the so called transformed image in the imageview. it looks exactly the same.</p>
  
  <p><strong>UPDATE:</strong>
  I managed to record the sample buffer to a video file (it's still stretched because of the wrong orientation though). I've used this code as a base</p>
  
  <p><a href="http://geek-is-stupid.github.io/blog/2017/04/13/how-to-record-detect-face-overlay-video-at-real-time-using-swift/" rel="nofollow noreferrer">http://geek-is-stupid.github.io/blog/2017/04/13/how-to-record-detect-face-overlay-video-at-real-time-using-swift/</a></p>
  
  <p>But I'm still struggling with applying the rotating to the CGContext. basically everything I do to the context is completely ignored. </p>
  
  <pre><code>func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
  
          let writable = canWrite()
          if writable , sessionAtSourceTime == nil {
                  print("starting session")
                  sessionAtSourceTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
                  assetWriter!.startSession(atSourceTime: sessionAtSourceTime!)
              }
  
          let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
          if writable {
              autoreleasepool {
                  CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
                  var renderedOutputPixelBuffer: CVPixelBuffer? = nil
                  let options = [
                      kCVPixelBufferCGImageCompatibilityKey as String: true,
                      kCVPixelBufferCGBitmapContextCompatibilityKey as String: true,] as CFDictionary
                  let status = CVPixelBufferCreate(kCFAllocatorDefault,
                                                   CVPixelBufferGetWidth(pixelBuffer),
                                                   CVPixelBufferGetHeight(pixelBuffer),
                                                   kCVPixelFormatType_32BGRA, options,
                                                   &amp;renderedOutputPixelBuffer)
                  guard status == kCVReturnSuccess else { return }
  
                  CVPixelBufferLockBaseAddress(renderedOutputPixelBuffer!,CVPixelBufferLockFlags(rawValue: 0))
  
                  let renderedOutputPixelBufferBaseAddress = CVPixelBufferGetBaseAddress(renderedOutputPixelBuffer!)
  
                  memcpy(renderedOutputPixelBufferBaseAddress,CVPixelBufferGetBaseAddress(pixelBuffer),CVPixelBufferGetHeight(pixelBuffer) * CVPixelBufferGetBytesPerRow(pixelBuffer))
  
                  CVPixelBufferLockBaseAddress(renderedOutputPixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
  
                  let context = CGContext(data: renderedOutputPixelBufferBaseAddress,
                                          width: CVPixelBufferGetWidth(renderedOutputPixelBuffer!),
                                          height: CVPixelBufferGetHeight(renderedOutputPixelBuffer!),
                                          bitsPerComponent: 8,
                                          bytesPerRow: CVPixelBufferGetBytesPerRow(renderedOutputPixelBuffer!),
                                          space: CGColorSpaceCreateDeviceRGB(),
                                          bitmapInfo: bitmapInfo!)
  
  
                  let radians : Float = atan2f(Float(boxView!.transform.b), Float(boxView!.transform.a));
                  context!.translateBy(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2)
                  context!.rotate(by:CGFloat(radians))
  
                  let image: CGImage = context!.makeImage()!
  
                  self.imageView!.image = UIImage(cgImage: image)
  
                  if (bufferAdaptor?.assetWriterInput.isReadyForMoreMediaData)!, canWrite() {
                     bufferAdaptor?.append(renderedOutputPixelBuffer!, withPresentationTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
                  }
  
              CVPixelBufferUnlockBaseAddress(renderedOutputPixelBuffer!,CVPixelBufferLockFlags(rawValue: 0))
              CVPixelBufferUnlockBaseAddress(pixelBuffer,CVPixelBufferLockFlags(rawValue: 0))
          } 
      }
  </code></pre>