0

I'm developing video player in which I have trimming functionality. I am using ABVideoRangeSlider for that. Whenever I try to trim and save video I'm getting Error Domain=NSCocoaErrorDomain Code=-1 "(null)" this error. Here is my code snippet

    let composition = AVMutableComposition()
    let track = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID:Int32(kCMPersistentTrackID_Invalid))

    try! track.insertTimeRange(CMTimeRangeMake(startTimeForCurrentSlice, endTimeForCurrentSlice), of: asset.tracks(withMediaType: AVMediaTypeVideo)[0] as AVAssetTrack, at: kCMTimeZero)
    var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0] as String
    let videoPathToSave = documentsDirectory.appending("mergeVideo-\(arc4random()%1000)-d.mp4")
    let videoURLToSave = NSURL(fileURLWithPath: videoPathToSave)

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURLToSave as URL)
    }) { saved, error in
        if saved {
            let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(defaultAction)
            self.present(alertController, animated: true, completion: nil)
        } else {
            print("Error: \(String(describing: error!))")
        }
    }

Any help will be appreciated.

pravin nagargoje
  • 70
  • 1
  • 1
  • 9

1 Answers1

0

Before saving to camera roll You need to export your AVMutableComposition with AVAssetExportSession or with AVAssetWriter, and after that You can save it to camera roll.

Checkout this tutorial for AVAssetExportSession usage. For saving to camera roll it uses ALAssetsLibrary, but You can save it with Photos.

Tiko
  • 485
  • 4
  • 10