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

标签 swift avassetexportsession avcapture

我使用 AVCapture session 生成视频,然后使用 AVVideoCompositionCoreAnimationTool 添加简单的叠加层。然后我使用 AVAssetExportSession 输出文件。这一切似乎都有效,但是当我尝试使用 PHPhotoLibrary 将其保存到照片库时(因为 ALAssetsLibrary 已被折旧)失败,并显示消息:“无法完成操作 cocoa 错误 -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/

相关文章:

ios - 在iOS10中使用AVCapturePhotoOutput-NSGenericException

ios - 点击时如何在 UICollectionViewCell 中制作此动画?

Swift:继承项目内的类

objective-c - 不能在带有 Objective-C++ 代码的 C++ 类中使用 dispatch_sync

ios - AVVideoComposition 在 iOS 8 中不起作用

ios - AVAssetExportSession 导出不确定性失败,错误为 : "Operation Stopped, NSLocalizedFailureReason=The video could not be composed."

ios - iOS5 中的 AVCaptureSession 预览屏幕方向

swift - 确实在索引路径中选择了用户,然后向 UI Collection View Controller 显示了用户详细信息

swift - iMessage 应用程序不会将消息插入事件对话

objective-c - AVAssetExportSession - 仅 mov 音频文件导出失败?