2016-02-20 32 views
2

我使用AVCapture會話生成視頻,然後使用AVVideoCompositionCoreAnimationTool添加簡單覆蓋。然後我使用AVAssetExportSession輸出文件。這一切似乎工作,但後來當我試圖將它保存到使用PHPhotoLibrary(因爲ALAssetsLibrary已折舊)的照片庫失敗,並顯示消息:「無法完成操作可可錯誤-1」。谷歌廣泛使用並檢查文檔後,我無法弄清楚什麼是錯誤的。(Cocoa error -1)嘗試保存使用AVCaptureSession創建的視頻

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

回答

3

那麼它原來我失蹤我的電影名稱末尾的擴展:

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

所以它應該是「FinalVideo(隨機).MOV」

希望這有一天會幫助某人。