簡體   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