簡體   English   中英

AVAssetExportSession 未開始導出/卡在等待中

[英]AVAssetExportSession not starting export / stuck waiting

我正在嘗試使用 AVFoundation 加入 2 個視頻剪輯。 出於測試目的,在這里我嘗試加載單個視頻剪輯,將其視頻和音頻軌道添加到合成中,然后使用AVAssetExportSession導出。

當我運行下面的代碼時,輸​​出“導出”,但從未執行導出回調。 此外,如果我定期檢查導出的進度( print(exporter.progress) ),我發現進度始終為 0.0,即使在幾分鍾后也是如此。 如果我打印狀態,我發現它正在“等待”某些東西。

// URL to video file
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("video.mov")

// Create composition and tracks
let comp = AVMutableComposition()
let videoTrack = comp.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let audioTrack = comp.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)

// Create asset from file
let asset = AVAsset(url: fileURL)

// Insert video
try! videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: asset.tracks(withMediaType: AVMediaType.video)[0] as AVAssetTrack, at: kCMTimeZero)

// Insert audio
try! audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: asset.tracks(withMediaType: AVMediaType.audio)[0] as AVAssetTrack, at: kCMTimeZero)

// Delete existing movie file if exists
let finalURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("FINAL.mov")
try? FileManager.default.removeItem(at: finalURL)

// Create the exporter
exporter = AVAssetExportSession(asset: comp, presetName: AVAssetExportPresetLowQuality)
exporter.outputURL = finalURL
exporter.outputFileType = AVFileType.mov

print("Exporting")
exporter.exportAsynchronously(completionHandler: {
    // This statement is never reached
    print(self.exporter.error)
})

不會拋出任何錯誤。 似乎導出從未開始。 我不確定它在等什么。

編輯:一些令人毛骨悚然的事情正在發生。 如果我重新啟動手機然后運行代碼,它就可以工作。 但它正好工作 1 次。 如果我第二次運行它,則不會像往常一樣發生任何事情。 但是當我重新啟動手機並再次運行它時,它又可以工作了。

編輯 2:我在別人的手機上試過,每次都能可靠地工作。 我的手機到底出了什么問題?

這是基本上做同樣事情的代碼,為視頻添加音軌。 它看起來真的是你的,它目前正在 appstore 上開發一個應用程序。

func merge() -> AVComposition? {
        guard let videoAsset = videoAsset, let audioAsset = audioAsset else {
            return nil
        }
        let avMutableComposition = AVMutableComposition()
        let audiotimeRange = CMTimeRange(start: kCMTimeZero, duration: audioAsset.duration)
        let compositionVideoTrack = avMutableComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
        do {
            try add(asset: videoAsset, withRemaining: audiotimeRange.duration, forComposition: compositionVideoTrack!, cursor: kCMTimeZero)
        } catch let error {
            print(error)
            return nil
        }

        let compositionAudioTrack = avMutableComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
        do {
            try compositionAudioTrack?.insertTimeRange(audiotimeRange, of: audioAsset.tracks(withMediaType: AVMediaType.audio)[0], at: kCMTimeZero)
        } catch let error {
            print(error)
            return nil
        }


        composition = (avMutableComposition.copy() as! AVComposition)
        return composition
    }

我看不出我們的代碼有什么問題,除了我不確定資產是否具有正確的持續時間這一事實,因為除非您明確詢問,否則獲取它是一項異步任務。
因此,當您創建AVAssetAVURLAssetPreferPreciseDurationAndTimingKey作為選項傳遞給true

AVURLAsset(url: <#T##URL#>, options: ["AVURLAssetPreferPreciseDurationAndTimingKey" : true])

我猜你的一個麥克風工作不正常,因此卡住了,我的后凸輪也有類似的行為。 您是否嘗試過在不同時錄制的情況下導出電影,只是為了測試是否涉及可能損壞的麥克風?

我在 iOS 10 上遇到了類似的問題(iOS 11 運行良好)。 在工作區設置中切換回標准/傳統構建系統為我修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM