gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:20:58 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com