title: Failure to append frames to video file using AVAssetWriterVideoInput. Fails with undocumented error -12738 at the end
body: <p>I am using an AVAssetWriterVideoInput to append buffers to a file. I have this code:</p>
  
  <pre><code> if ( _assetWriter.status == AVAssetWriterStatusWriting ) {
      // If the asset writer status is writing, append sample buffer to its corresponding asset writer input
      if (mediaType == AVMediaTypeVideo) {
        if (_assetWriterVideoInput.readyForMoreMediaData) {
          if (![_assetWriterVideoInput appendSampleBuffer:sampleBuffer]) {
            NSLog(@"error: %@", [_assetWriter.error localizedFailureReason]);
            NSLog(@"error: %@", [_assetWriter.error localizedRecoveryOptions]);
            NSLog(@"error: %@", [_assetWriter.error localizedDescription]);
            NSLog(@"error: %@", [_assetWriter.error domain]);
            NSLog(@"error: %@", [_assetWriter.error userInfo]);
          } else
            NSLog(@"frame saved");
        }
      }
  </code></pre>
  
  <p>this line</p>
  
  <pre><code>    if (![_assetWriterVideoInput appendSampleBuffer:sampleBuffer]) {
  </code></pre>
  
  <p>fails with <code>unknown error</code> and code <code>-12738</code> that, obviously is not found on any <a href="https://developer.apple.com/reference/avfoundation/1668907-av_foundation_error_constants/error_codes?language=objc" rel="nofollow noreferrer">documentation</a>, as expected with all apple docs.</p>
  
  <p>Also, I doubt this is an unknown error for the simple reason that there are a lot of codes for unknown errors inside AVFoundation and if the system is picking code <code>-12738</code> it obviously knows more than saying it is unknown. </p>
  
  <p>Looking at the file being saved, it stays 0 megabytes, because no buffer/frame is saved. </p>
  
  <p>This <code>AVAssetWriterVideoInput</code> was created like this:</p>
  
  <pre><code>  CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(currentFormatDescription);
    NSUInteger numPixels = dimensions.width * dimensions.height;
    NSUInteger bitsPerSecond;
  
    // Assume that lower-than-SD resolutions are intended for streaming, and use a lower bitrate
      NSUInteger bitsPerPixel = 11.4; // This bitrate matches the quality produced by AVCaptureSessionPresetHigh.
  
    bitsPerSecond = numPixels * bitsPerPixel;
  
    NSDictionary *videoCompressionSettings = @{AVVideoCodecKey                  : AVVideoCodecH264,
                                               AVVideoWidthKey                  : @(dimensions.width),
                                               AVVideoHeightKey                 : @(dimensions.height),
                                               AVVideoCompressionPropertiesKey  : @{ AVVideoAverageBitRateKey      : @(bitsPerSecond),
                                                                                     AVVideoMaxKeyFrameIntervalKey : @(30)}  };
  
    if ([_assetWriter canApplyOutputSettings:videoCompressionSettings forMediaType:AVMediaTypeVideo])
    {
      // Intialize asset writer video input with the above created settings dictionary
      _assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
      _assetWriterVideoInput.expectsMediaDataInRealTime = YES;
  </code></pre>
  
  <p>and the buffer, when it is appended has the following characteristics:</p>
  
  <pre><code>CMSampleBuffer 0x1009e12a0 retainCount: 1 allocator: 0x1b762cbb8
      invalid = NO
      dataReady = YES
      makeDataReadyCallback = 0x0
      makeDataReadyRefcon = 0x0
      formatDescription = &lt;CMVideoFormatDescription 0x170443210 [0x1b762cbb8]&gt; {
      mediaType:'vide' 
      mediaSubType:'BGRA' 
      mediaSpecific: {
          codecType: 'BGRA'       dimensions: 1920 x 1080 
      } 
      extensions: {&lt;CFBasicHash 0x17087c2c0 [0x1b762cbb8]&gt;{type = immutable dict, count = 2,
  entries =&gt;
      0 : &lt;CFString 0x1b1c6d460 [0x1b762cbb8]&gt;{contents = "Version"} = &lt;CFNumber 0xb000000000000022 [0x1b762cbb8]&gt;{value = +2, type = kCFNumberSInt32Type}
      2 : &lt;CFString 0x1b1c6d3e0 [0x1b762cbb8]&gt;{contents = "CVBytesPerRow"} = &lt;CFNumber 0xb00000000001e002 [0x1b762cbb8]&gt;{value = +7680, type = kCFNumberSInt32Type}
  }
  }
  }
      sbufToTrackReadiness = 0x0
      numSamples = 1
      sampleTimingArray[1] = {
          {PTS = {290309939228910/1000000000 = 290309.939}, DTS = {INVALID}, duration = {INVALID}},
      }
      imageBuffer = 0x170321180
  </code></pre>
  
  <p><a href="http://www.filedropper.com/test_162" rel="nofollow noreferrer">I have a sample code here</a> if you want to check the problem. That code is prepared to shoot video in 4K. Change the line <code>AVCaptureSessionPreset3840x2160</code> to AVCaptureSessionPresetHigh<code>inside</code>ProcessadorVideo.m` if your device cannot do that. The sample code crops a rectangle from the video stream and applies a comic effect to it.
  Thanks</p>