我正在开发具有修剪功能的视频播放器。我为此使用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!))")
}
}
任何帮助将不胜感激。
最佳答案
保存到相机胶卷之前,需要使用AVMutableComposition
或AVAssetExportSession
导出AVAssetWriter
,然后可以将其保存到相机胶卷。
查看this教程以了解AVAssetExportSession
的用法。为了保存到相机胶卷,它使用ALAssetsLibrary
,但是您可以使用Photos
保存它。
关于swift - 修剪后无法在相机胶卷中显示视频,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44757701/