title: IOS:Swift: Video Screen Capture
body: <p>What is the screen device called in iOS/Swift?</p>
  
  <p>When I print the devices I get</p>
  
  <pre><code>(
  "&lt;AVCaptureFigVideoDevice: 0x134d0f210 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]&gt;",
  "&lt;AVCaptureFigVideoDevice: 0x134e0af80 [Front Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]&gt;",
  "&lt;AVCaptureFigAudioDevice: 0x174265440 [iPad Microphone][com.apple.avfoundation.avcapturedevice.built-in_audio:0]&gt;"
  )
  </code></pre>
  
  <p>So where's the screen ID?</p>
  
  <p>There's just too much outdated objective c code while swift is a moving target.  I'm looking for a swift solution to capture video from my iPad screen and audio from built-in microphone.  The audio will be a separate question.</p>
  
  <p>Here is a screen grabber for OS X</p>
  
  <p><a href="https://github.com/kennyledet/SwiftCap" rel="nofollow">https://github.com/kennyledet/SwiftCap</a></p>
  
  <pre><code>    // AVCaptureSession holds inputs and outputs for real-time capture
  let mSession = AVCaptureSession()
  let mScreenCapOutput = AVCaptureMovieFileOutput()
  var mOutputPath = ""
  // Just capture main display for now
  let mMainDisplayId = CGMainDisplayID()
  </code></pre>
  
  <p>but I cannot find in the documentation the display ID, CGMainDisplayID, for an iPad...</p>
  
  <p>Here is a typical solution for a camera in swift</p>
  
  <p><a href="https://github.com/bradley/iOSSwiftSimpleAVCamera" rel="nofollow">https://github.com/bradley/iOSSwiftSimpleAVCamera</a></p>
  
  <p>but it has too many errors and doesn't compile with iOS 8.1 or 8.2 and grabs video from camera.  </p>
  
  <pre><code>func addVideoOutput() {
  var rgbOutputSettings: NSDictionary = NSDictionary(object: Int(CInt(kCIFormatRGBA8)), forKey: kCVPixelBufferPixelFormatTypeKey)
  
      self.videoDeviceOutput = AVCaptureVideoDataOutput()
  
      self.videoDeviceOutput.alwaysDiscardsLateVideoFrames = true
  
      self.videoDeviceOutput.setSampleBufferDelegate(self, queue: self.sessionQueue)
  
      if self.session.canAddOutput(self.videoDeviceOutput) {
          self.session.addOutput(self.videoDeviceOutput)
      }
  }
  </code></pre>
  
  <p><a href="https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW18" rel="nofollow">https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW18</a></p>
  
  <p>Apple gives an objective-c solution like this</p>
  
  <pre><code>/*
   * Create video connection
   */
  AVCaptureDeviceInput *videoIn = [[AVCaptureDeviceInput alloc] initWithDevice:[self videoDeviceWithPosition:AVCaptureDevicePositionBack] error:nil];
  if ([_captureSession canAddInput:videoIn])
      [_captureSession addInput:videoIn];
  
  AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
  [videoOut setAlwaysDiscardsLateVideoFrames:YES];
  [videoOut setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]}];
  dispatch_queue_t videoCaptureQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);
  [videoOut setSampleBufferDelegate:self queue:videoCaptureQueue];
  
  if ([_captureSession canAddOutput:videoOut])
      [_captureSession addOutput:videoOut];
  _videoConnection = [videoOut connectionWithMediaType:AVMediaTypeVideo];
  self.videoOrientation = _videoConnection.videoOrientation;
  
  if([self.session canSetSessionPreset:AVCaptureSessionPreset640x480])
      [self.session setSessionPreset:AVCaptureSessionPreset640x480]; // Lower video resolution to decrease recorded movie size
  
  return YES;
  }
  </code></pre>
  
  <p>This should be easy.....???</p>