2015-07-01 85 views
0

我已經轉換this代碼斯威夫特的語言,但我得到這個視頻修剪失敗,塊AVAssetExportSessionStatus.Failed

Error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x174278600 {NSUnderlyingError=0x170241d10 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed} in (case AVAssetExportSessionStatus.Failed).

請幫我解決了這個

func cropVideo(sourceURL: NSURL) 
    { 
     let asset = AVURLAsset(URL: sourceURL, options: nil) 

     let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) 
       var error : NSError? 
    let file = "Finaloutput.mp4" 
    /* let paths : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] 
     let outputURL1 = paths[0] as? String*/ 
     let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 
     let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 
     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) 

     let outputURL1 = paths[0] as? String 

     let filemgr = NSFileManager.defaultManager() 
     filemgr.createDirectoryAtPath(outputURL1!, withIntermediateDirectories: true, attributes: nil, error: &error) 
     var outputURL = outputURL1!.stringByAppendingPathComponent(file) 
     filemgr.removeItemAtPath(outputURL, error: &error) 

     let FinalUrlTosave = NSURL(string: outputURL) 
     exportSession.outputURL=FinalUrlTosave 
     exportSession.shouldOptimizeForNetworkUse = true 
     // exportSession.outputFileType = AVFileTypeQuickTimeMovie 
     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
     let start:CMTime 
     let duration:CMTime 
     start = CMTimeMakeWithSeconds(1.0, 600) 
     duration = CMTimeMakeWithSeconds(19.0, 600) 
     // let timeRangeForCurrentSlice = CMTimeRangeMake(start, duration) 
     let range = CMTimeRangeMake(start, duration); 
     exportSession.timeRange = range 

     let destinationURL1 = NSURL(string: outputURL) 

     exportSession.exportAsynchronouslyWithCompletionHandler({ 
      switch exportSession.status{ 
      case AVAssetExportSessionStatus.Failed: 

       println("failed \(exportSession.error)") 
      case AVAssetExportSessionStatus.Cancelled: 
       println("cancelled \(exportSession.error)") 
      default: 
       println("complete....complete") 
       self.SaveVideoToPhotoLibrary(destinationURL1!) 

      } 
     }) 
    } 

func SaveVideoToPhotoLibrary(outputFileURL: NSURL) 

    { 


     assetsLibrary = ALAssetsLibrary() 

     let videoURL = outputFileURL as NSURL? 

     if let library = assetsLibrary{ 

      if let url = videoURL{ 

       library.writeVideoAtPathToSavedPhotosAlbum(url, 
        completionBlock: {(url: NSURL!, error: NSError!) in 

         print(url) 

         if let theError = error{ 
          print("Error happened while saving the video") 
          print("The error is = \(theError)") 
         } else { 
          print("no errors happened") 
         } 

       }) 
      } else { 
       print("Could not find the video in the app bundle") 
      } 

     } 


    } 

回答

1

發現解決方案: 我已經改變了這條線,它適用於我

let FinalUrlTosave = NSURL(fileURLWithPath: outputURL) 

代替

let FinalUrlTosave = NSURL(string: outputURL) 

我沒有得到確切的路徑。