代码之家  ›  专栏  ›  技术社区  ›  Abdullah Umer

Swift:从url保存视频数据时出错

  •  0
  • Abdullah Umer  · 技术社区  · 5 年前

    我有以下用于保存录制视频的代码:

    func saveVideo() -> Void {
            guard let url = self.videoUrl else {
                Logger.shared.log(.video, .error, "error video url invalid")
                return
            }
            
            let homeDirectory = URL.init(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
            let fileUrl = homeDirectory.appendingPathComponent(self.adId.toString()).appendingPathComponent("video-clip").appendingPathComponent(UUID.init().uuidString, isDirectory: false).appendingPathExtension("mov")
            
            Logger.shared.log(.video, .debug, "saving video to: \(fileUrl)")
            
            self.savedVideoUrl = fileUrl
            
            do {
                let data = try Data(contentsOf: url)
                try data.write(to: fileUrl, options: .atomicWrite)
            } catch {
                Logger.shared.log(.video, .error, "error saving video: \(error.localizedDescription)")
            }
            
        }
    

    在这里 self.videoUrl url 从摄像机录制的视频中提取,并通过委托方法进行初始化: func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

    问题是,当 saveVideo() 方法执行时,会出现错误:

    [878     ] 13:22:16.045 | video | Main thread | error saving video: The folder “4A601A28-C4EB-405E-8110-6D01965D5920.mov” doesn’t exist.
    

    我不确定这段代码有什么问题。如果文件不存在,那么我们如何先创建文件,然后再将数据写入其中?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Glenn Posadas    5 年前

    好吧,几天前我也有一个类似的任务(即下载一个视频,无论网址来源(本地或远程)如何,然后将其保存在本地)。

    我用你的函数做了一个实验。

    请参阅github仓库,其中包含工作示例和对函数的应用修复 https://github.com/glennposadas/AVFoundation-SaveLocal

    你做错的是你为新文件提供URL路径的方式。

    编辑:我认为您需要先创建文件夹路径。我的 fileUrl 之所以有效,是因为路径存在。

    这样,它就可以工作了(.mp4或.mov,无论哪种方式):

    let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
                .first!
                .appendingPathComponent("\(UUID.init().uuidString).mp4")
    

    它有一条路径(如果你要打印它):

    /Users/myusername/Library/Developer/CoreSimulator/Devices/812428A5-0893-43BE-B343-B23E9F13D4AA/data/Containers/Data/Application/E278E093-BE29-410B-9C5D-810BD0F968F8/Documents/F227C8B5-5D8D-42B0-8205-3ADAD0DD38F5.mp4
    

    同时,您的文件URL:

        let homeDirectory = URL.init(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
        let fileUrl = homeDirectory
            .appendingPathComponent("someId")
            .appendingPathComponent("video-clip")
            .appendingPathComponent(UUID.init().uuidString, isDirectory: false)
            .appendingPathExtension("mov")
    

    给出路径:

    /Users/myusername/Library/Developer/CoreSimulator/Devices/812428A5-0893-43BE-B343-B23E9F13D4AA/data/Containers/Data/Application/570D3CFA-3C44-41A5-A642-C79E5590D16E/someId/video-clip/5B1C9C98-C9EA-40D2-805C-B326466837E6.mov
    

    编辑:要解决每次构建项目时文档中缺少文件的问题,请参阅我上面提供的仓库中的更改。我添加了一个修复程序,用于从文档文件夹中检索视频文件。

    所以基本上,你应该如何检索文件,你只需要保存 fileName ,因为每次运行构建都会产生不同的 filePath .