我正在开发具有修剪功能的视频播放器。我为此使用 ABVideoRangeSlider。每当我尝试修剪和保存视频时,我都会收到Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
此错误。这是我的代码片段
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!))")
}
}
任何帮助将不胜感激。