swift - 使用 AVFoundation 在 Swift 中录制视频时如何删除临时文件?

标签 swift avfoundation

我在 objective-c 中有这个,但我不知道如何将它转换为 Swift。当我尝试这个翻译时,我在 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];
                }];
            }];
        });

谢谢!!

最佳答案

我认为您需要在每次录制的开始和结束时删除您的临时文件,以防用户在录制期间退出应用程序或应用程序崩溃 留下一段视频 block ,这会让作者在下一次写作 session 中感到不安

可以在这里找到一个完整的 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

}

关于swift - 使用 AVFoundation 在 Swift 中录制视频时如何删除临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24664475/

相关文章:

swift - 保存密码警报不会出现在 Swift 中

objective-c - 检查是否播放音乐

ios - 尝试使用 AV Foundation 捕获静止图像时不断出现 "inactive/invalid connection passed"错误

ios - 从 firebase 存储下载并在 ios 中查看文档

swift - 将核心数据对象绑定(bind)到输入 SwiftUI

swift - 为什么我不能在 Playground 中使用 AVURLAsset 从媒体文件中提取数据?

objective-c - AVCaptureDevice isInUseByAnotherApplication 总是返回 false

ios - 在 AVPlayer 中显示播放控件

ios - 在 Game Center 比赛中出现错误 : cancelled by host

ios - "unrecognized selector"桥接头错误,即使它已添加到设置中