Why is my video resource invalid for PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:)

I am processing an H264 encoded video stream from a non-apple IoT device. I want to record bits of this video stream.

I'm getting an error when I try to save to the photo gallery:

The operation couldn’t be completed. (PHPhotosErrorDomain error 3302.)

My Code, let me know if I need to share more:

  private func beginRecording() {
    self.handlePhotoLibraryAuth()
    self.createFilePath()
    guard let videoOutputURL = self.outputURL,
       let vidWriter = try? AVAssetWriter(outputURL: videoOutputURL, fileType: AVFileType.mp4),
       self.formatDesc != nil else {
         print("Warning: No Format For Video")
         return
       }
    let vidInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: nil, sourceFormatHint: self.formatDesc)
    guard vidWriter.canAdd(vidInput) else {
      print("Error: Cant add video writer input")
      return
    }
     
    let sourcePixelBufferAttributes = [
      kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_32ARGB),
      kCVPixelBufferWidthKey as String: "1280",
      kCVPixelBufferHeightKey as String: "720"] as [String : Any]
     
    self.videoWriterInputPixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(
      assetWriterInput: vidInput,
      sourcePixelBufferAttributes: sourcePixelBufferAttributes)
    vidInput.expectsMediaDataInRealTime = true
    vidWriter.add(vidInput)
    guard vidWriter.startWriting() else {
      print("Error: Cant write with vid writer")
      return
    }
    vidWriter.startSession(atSourceTime: CMTimeMake(value: self.videoFrameCounter, timescale: self.videoFPS))
    self.videoWriter = vidWriter
    self.videoWriterInput = vidInput
    print("Recording Video Stream")
  }

Save the Video

  private func saveRecordingToPhotoLibrary() {
    let fileManager = FileManager.default
    guard fileManager.fileExists(atPath: self.path) else {
      print("Error: The file: \(self.path) not exists, so cannot move this file camera roll")
      return
    }
    print("The file: \(self.path) has been save into documents folder, and is ready to be moved to camera roll")

This is what Fails

    PHPhotoLibrary.shared().performChanges({
      PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: self.path))
    }) { completed, error in
      guard completed else {
        print ("Error: Cannot move the video \(self.path) to camera roll, error: \(String(describing: error?.localizedDescription))")
        return
      }
      print("Video \(self.path) has been moved to camera roll")
    }
  }

 When Recording ends we save the video

  private func endRecording() {
    guard let vidInput = videoWriterInput, let vidWriter = videoWriter else {
      print("Error, no video writer or video input")
      return
    }
    vidInput.markAsFinished()
    if !vidInput.isReadyForMoreMediaData {
      vidWriter.finishWriting {
        print("Finished Recording")
        guard vidWriter.status == .completed else {
          print("Warning: The Video Writer status is not completed, status: \(vidWriter.status.rawValue)")
          print(vidWriter.error.debugDescription)
          return
        }
        print("VideoWriter status is completed")
        self.saveRecordingToPhotoLibrary()
      }
    }
  }

Anyone know how I can upload or capture the data of the video stream to debug with others? I would use one of my code-level support tickets but I don't know if I can provide them the data

Sadly, but it looks like having not enough free disk space can also result in reporting (PHPhotosErrorDomain error 3302)

This line of code was the issue somehow, but now it only saves grey videos(of correct length and size) to the photo library despite the video file being valid in the Files app on the physical iOS device.

    vidWriter.startSession(atSourceTime: CMTimeMake(value: self.videoFrameCounter, timescale: self.videoFPS))

Even using the share/ UIActivityViewController provided by apple saved a grey video. oh well, just will link to the files app instead inside the app.

Why is my video resource invalid for PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:)
 
 
Q