当我使用AVFoundation在Swift中录制视频时,如何删除临时文件?

时间:2014-07-09 22:02:46

标签: swift avfoundation

我在目标c中有这个,但我不知道如何将其转换为Swift。当我尝试这个traduction时,我在library.removeItemAtPath中获得了一个崩溃。我不知道是什么:^ {...还有其他形式要保存然后删除这个没有获得冲突?

   dispatch_async(_captureQueue, ^{
            [_encoder finishWithCompletionHandler:^{
                self.isCapturing = NO;
                _encoder = nil;
                ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                [library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
                    NSLog(@"save completed");
                    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
                }];
            }];
        });

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您希望在每次录制的开头和结尾删除您的临时文件,以防录制期间用户退出应用程序或崩溃 留下一个视频块,这会在下一次写会话中扰乱作者

swift中的视频编写器的完整示例可以在这里找到:

https://github.com/OhioVR/CineVid/blob/master/CineVid/ViewController.swift

虽然它没有在2015年4月30日完成其声明的任务,但可以轻松修改它以消除尝试的iso翻转效果,您可以使用它来保存设备支持的任何帧速率和分辨率的视频。你可以得到一个完整的图像处理流程。

以下是swift的相关部分:

func startRecording () {

     ///stuff you'd do to start the recording including deleting
     ///your temp file if it exists from the last recording session
     ////SET OUTPUT URL AND PATH. DELETE ANY FILE THAT EXISTS THERE
     var tmpdir = NSTemporaryDirectory()
     outputPath = "\(tmpdir)output.mov"
     outputURL = NSURL(fileURLWithPath:outputPath as String)!
     let filemgr = NSFileManager.defaultManager()
     if filemgr.fileExistsAtPath(outputPath) {
        filemgr.removeItemAtPath(outputPath, error: nil)
     }

}


func stopRecording() {

    ////FINISH WRITING AND TRANSFER RESULTS TO THE CAMERA ROLL
    assetWriterVideoInput.markAsFinished()
    assetWriterAudioInput.markAsFinished()
    self.videoWriter.finishWritingWithCompletionHandler({ () -> Void in
        if self.videoWriter.status == AVAssetWriterStatus.Failed {
            println("VIDEO WRITER ERROR: \(self.videoWriter.error.description)")
        } else {
            let fileManager = NSFileManager.defaultManager()
            if fileManager.fileExistsAtPath(self.outputPath as String) {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                    if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.outputPath) {  ///(fileURL) {
                        var complete : ALAssetsLibraryWriteVideoCompletionBlock = {reason in println("reason \(reason)")}
                        UISaveVideoAtPathToSavedPhotosAlbum(self.outputPath as String, self, "savingCallBack:didFinishSavingWithError:contextInfo:", nil)
                    } else {
                        println("the file must be bad!")
                    }
                });
            } else {
                println("there is no file")
            }
        }
    });


}

func savingCallBack(video: NSString, didFinishSavingWithError error:NSError, contextInfo:UnsafeMutablePointer<Void>){
    println("the file has been saved sucessfully to the camera roll")
    //you could delete the temp file here if you like

}