I am trying to export the last X seconds of a video being recorded to a seperate file. So a whole video is being written to disk, and snippets of length X seconds can also be written. However, I am having trouble exporting the AVAsset that is being written to, I believe because the asset is not marked as exportable (the precondition fails). If I remove the precondition, then I receive the error The operation could not be completed. How can I accomplish this?
func trimVideoAndSaveToFile(videoURL: URL, startSeconds: Double, endSeconds: Double, outputURL: URL) {
print("trim video!")
let asset = AVURLAsset(url: videoURL)
precondition(asset.isExportable)
let exportSession = AVAssetExportSession.init(asset: asset, presetName: AVAssetExportPresetHighestQuality)!
exportSession.outputURL = outputURL
print(outputURL)
exportSession.shouldOptimizeForNetworkUse = true
exportSession.outputFileType = AVFileType.mov
let start = CMTimeMakeWithSeconds(startSeconds, preferredTimescale: 600)
let end = CMTimeMakeWithSeconds(endSeconds, preferredTimescale: 600)
let duration = CMTimeSubtract(end, start)
let range = CMTimeRangeMake(start: start, duration: duration)
exportSession.timeRange = range
exportSession.exportAsynchronously {
switch(exportSession.status) {
case .completed:
print("finished recording to \(outputURL)")
break
//
case .failed:
print("change recording failed! Reason: \(String(describing: exportSession.error?.localizedDescription))")
break
//
case .cancelled:
print("change recording cancelled!")
break
//
default: break
}
}
}
videoURL is the url used in self.fileOutput.startRecording(to: url, recordingDelegate: self)
from Export AVAsset while writing to disk?
No comments:
Post a Comment