繁体   English   中英

AVAssetExportSession 卡住(未开始)导出

[英]AVAssetExportSession stuck (not starting) export

我试图从照片库中导出视频,但从未执行导出回调。 我定期检查导出的进度,进度始终为零。

下面的代码在 99.9% 的情况下有效,但有时在某些设备上(绝对随机)它会停止工作,只有重新启动 iPhone 才有帮助。

AVAssetExportSession.Status总是在waiting state


class FilesInteractor {
    static func tempDirectoryPath() -> String {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
        return documentsPath.appendingPathComponent("temp") as String
    }

    static func createTempDirectory() {
        if !FileManager.default.fileExists(atPath: tempDirectoryPath()) {
            try? FileManager.default.createDirectory(atPath: tempDirectoryPath(), withIntermediateDirectories: true, attributes: nil)
        }
    }

    static func testVideoURL(name: String, ext: String = "mov") -> URL {
        createTempDirectory()
        let outputURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("test").appendingPathComponent("\(name).\(ext)", isDirectory: false)

        log.debug("Test video URL: \(outputURL)")

        return outputURL
    }
}

import AVFoundation

let asset = AVAsset()
let outputURL = FilesInteractor.testVideoURL("output")

let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset1280x720)
exportSession?.outputFileType = .mov
exportSession?.outputURL = outputURL

try? FileManager.default.removeItem(at: outputURL)
exportSession?.exportAsynchronously(completionHandler: {
    print("sometimes never calls")
})

其他视频应用程序也冻结(Filto、Videoleap):

菲尔托

视频跳跃

我在一些 Github 项目中多次看到这个问题,通常它与 URL 的创建方式有关。 不确定您在问题中输入的代码是否只是一些占位符,但我认为您应该创建一个这样的 fileURL 而不是“string”。

var tempFileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("temp_video_data.mp4", isDirectory: false)
tempFileUrl = URL(fileURLWithPath: tempFileUrl.path)
exportSession.outputURL = tempFileUrl

也许这会解决它?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM