2016-06-27 10 views
0

私は自分のビデオを録画するために「録画」ボタンを使用しています。ユーザーが「録画」ボタンを押したままにすると、録画したビデオを写真アルバムに保存したいと思います。私はUISaveVideoAtPathToSavedPhotosAlbum関数でちょっと固まっています。これまでのコード:スウィフトでGPUImageを使用してビデオを録画/保存する

@IBAction func RecordAction(sender: UIButton) { 
    // User is holding down record button 
    if (!isRecording) { 
     do { 
      self.isRecording = true 
      let documentsDir = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain:.UserDomainMask, appropriateForURL:nil, create:true) 
      let fileURL = NSURL(string:"test.mp4", relativeToURL:documentsDir)! 
      do { 
       try NSFileManager.defaultManager().removeItemAtURL(fileURL) 
      } catch { 
      } 
      movieOutput = try MovieOutput(URL:fileURL, size:Size(width:400, height:645), liveVideo:true) 
      camera.audioEncodingTarget = movieOutput 
      filter --> movieOutput! 
      movieOutput!.startRecording() 
     } catch { 
      fatalError("Couldn't initialize movie, error: \(error)") 
     } 
    } 
} 

@IBAction func RecordDone(sender: UIButton) { 
    // After user stops holding down the button, save video to gallery 
    if isRecording == true { 
     movieOutput?.finishRecording { 
      self.isRecording = false 
      self.camera.audioEncodingTarget = nil 
      self.movieOutput = nil 
      UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, nil, nil) 
     } 
    } 
} 

何か助けていただければ幸いです。

答えて

0

は、ビデオを保存するためにこれを使用してみてください:

func StartWritingvideo() 
{ 
    player.pause() 

    filters.removeAllObjects() 

    var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 

    let filePath : String! 

    if trim == true 
    { 
     filePath = paths[0].stringByAppendingString("/output/outPut.mov") 
    } 
    else 
    { 
     filePath = paths[0].stringByAppendingString("/image.mov") 
    } 
    self.movieFile = GPUImageMovie(URL: NSURL(fileURLWithPath: filePath)) 
    self.movieFile.runBenchmark = true 
    self.movieFile.playAtActualSpeed = false 
    filters = [GPUImageAmatorkaFilter(),GPUImageMissEtikateFilter(),GPUImageSoftEleganceFilter(),FIIIN()] 

    let indexpaths = self.collectionView!.indexPathsForSelectedItems()! 
    let indexpath = indexpaths[0] as NSIndexPath 


    self.movieFile.addTarget(self.filters[indexpath.row] as! GPUImageInput) 
    let pathToMovie = paths[0].stringByAppendingPathComponent("IMG_filtered.MOV") 
    do 
    { 
     try NSFileManager.defaultManager().removeItemAtPath(pathToMovie) 
    } 
    catch 
    { 
     error as NSError 
    } 
    let Data = NSData(contentsOfURL: NSURL(fileURLWithPath: filePath)) 
    print(Data?.length) 
    let anAsset = AVAsset(URL: NSURL(fileURLWithPath: filePath)) 

    let videoAssetTrack = anAsset.tracksWithMediaType(AVMediaTypeVideo)[0] 
    var naturalSize = CGSize() 

    print(naturalSize) 
    naturalSize = videoAssetTrack.naturalSize 
    self.movieWriter = GPUImageMovieWriter(movieURL: NSURL(fileURLWithPath:pathToMovie), size: naturalSize) 
    let input = self.filters[indexpath.row] as! GPUImageOutput 
    input.addTarget(self.movieWriter) 
    self.movieWriter.shouldPassthroughAudio = true 
    if anAsset.tracksWithMediaType(AVMediaTypeAudio).count > 0 { 
     self.movieFile.audioEncodingTarget = self.movieWriter 
    } 
    else 
    { 
     self.movieFile.audioEncodingTarget = nil 
    } 

    self.movieFile.enableSynchronizedEncodingUsingMovieWriter(self.movieWriter) 
    self.movieWriter.startRecording() 
    self.movieFile.startProcessing() 
    self.movieWriter.completionBlock = 

    {() -> Void in 

     self.stop = false 
     self.movieWriter.finishRecording() 
     self.movieFile.cancelProcessing() 
     input.removeAllTargets() 
     self.movieFile.removeAllTargets() 


     if self.isLive == false 
     { 
      ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(NSURL(fileURLWithPath: pathToMovie), completionBlock: 
      {_ in 

       self.dismissViewControllerAnimated(false, completion: nil) 
       self.player.pause() 
      }) 
     }else { 


       self.dismissViewControllerAnimated(false, completion: nil) 

     } 

    } 


} 

私に尋ねる何か助けが必要な場合、私はGPUImageFilterのための素晴らしい知識を持っています。私の答えが分かれば、マークをあきらめてください。

+0

ありがとうございました。このコードはどこにあるべきですか? 「IBAction func RecordDone(送信者:UIButton)」の部分にありますか? – AppCodinz

+0

これは、あなたのドキュメントディレクトリにビデオを書き込むためのコードです。あなたはそれを変換し、ドキュメントディレクトリに保存する単一シンプルなビデオを入力すると。 – JAck

+0

こんにちは@JAck .. GPUImageFilterを手伝ってもらえますか?私はビデオフィルタの問題に直面しており、私は完全に固執しています。前もって感謝します。 –

関連する問題