gpt4 book ai didi

swift - ( cocoa 错误 -1)尝试保存使用 AVCaptureSession 创建的视频时

转载 作者:可可西里 更新时间:2023-11-01 01:00:05 26 4
gpt4 key购买 nike

我正在使用 AVCapture session 生成视频,然后使用 AVVideoCompositionCoreAnimationTool 添加简单的叠加层。然后我使用 AVAssetExportSession 来输出文件。这一切似乎都有效,但是当我尝试使用 PHPhotoLibrary(因为 ALAssetsLibrary 已贬值)将其保存到照片库时失败并显示消息:“Cant complete operation cocoa error -1”。在广泛使用 Google 并查看文档后,我无法弄清楚出了什么问题。

The problem

如果有任何帮助,我们将不胜感激。

 func videoOutput() {


videoToMake = AVAsset(URL: videoToMakeURL!)

if (videoToMake == nil) {

return
}


//This holds the different tracks of the video like audio and the layers
let mixComposition = AVMutableComposition()

let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

print("The duration of the video to create is \(videoToMake!.duration.seconds)")

do{
try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero,videoToMake!.duration), ofTrack: videoToMake!.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: kCMTimeZero)
}catch let error as NSError{
print("Error inserting time range on video track \(error.localizedDescription)")
return
}catch{
print("An unknown error occured")
}

//Make the instructions for the other layers
let mainInstrucation = AVMutableVideoCompositionInstruction()
mainInstrucation.timeRange = CMTimeRangeMake(kCMTimeZero, videoToMake!.duration)

//Create the layer instructions
let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
let videoAssetTrack = videoToMake!.tracksWithMediaType(AVMediaTypeVideo)[0]


let assetInfo = orientationFromTransform(videoAssetTrack.preferredTransform)
// sort size it in respect to the video orientation.

videoLayerInstruction.setTransform(videoAssetTrack.preferredTransform, atTime: kCMTimeZero)
videoLayerInstruction.setOpacity(0.0, atTime:videoToMake!.duration)

//Add the instructions
mainInstrucation.layerInstructions = [videoLayerInstruction]
let mainCompositionInst = AVMutableVideoComposition()

var naturalSize:CGSize
if assetInfo.isPortrait {
naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
}else{
naturalSize = videoAssetTrack.naturalSize
}

let renderWidth = naturalSize.width
let renderHeight = naturalSize.height

mainCompositionInst.renderSize = CGSizeMake(renderWidth, renderHeight)
mainCompositionInst.instructions = [mainInstrucation]
mainCompositionInst.frameDuration = CMTimeMake(1, 30);

//So now the main composition has been created add the video affects
applyVideoEffectsToComposition(mainCompositionInst, size: naturalSize)

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
let documentsDirectory = paths[0]
let random = Int(arc4random_uniform(1000))
let url = NSURL(fileURLWithPath:documentsDirectory).URLByAppendingPathComponent("FinalVideo\(random)")

//Create the exporter
let exporter = AVAssetExportSession(asset: mixComposition, presetName:AVAssetExportPresetHighestQuality)
exporter!.outputURL = url
exporter!.outputFileType = AVFileTypeMPEG4
exporter!.shouldOptimizeForNetworkUse = true
exporter!.videoComposition = mainCompositionInst

//Perform the export
exporter!.exportAsynchronouslyWithCompletionHandler() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.exportDidFinish(exporter!)
})
}
}

最佳答案

原来我遗漏了电影名末尾的扩展名:

let url = NSURL(fileURLWithPath:documentsDirectory).URLByAppendingPathComponent("FinalVideo\(random)")

所以它应该是“FinalVideo(random).mov”

希望有一天这对某人有所帮助。

关于swift - ( cocoa 错误 -1)尝试保存使用 AVCaptureSession 创建的视频时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com