写入磁盘时要导出AVAset吗?

时间:2018-12-06 19:05:58

标签: ios swift avfoundation avasset

我正在尝试将正在录制的视频的最后X秒导出到单独的文件中。因此,整个视频都将被写入磁盘,并且还可以写入长度为X秒的片段。但是,我无法导出正在写入的AVAset,因为该资产未标记为可导出(前提条件失败),因此我认为。如果我删除了前提条件,那么我会收到错误The operation could not be completed。我该怎么做?

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
        }
    }
}

videoURLself.fileOutput.startRecording(to: url, recordingDelegate: self)

中使用的网址

0 个答案:

没有答案