kusumoto_teruya
kusumoto_teruya

Reputation: 2475

AVAssetExportSession.exportAsynchronously does not make progress

Extended AVMutableComposition so that it can be exported.
However, even a 1-second video does not progress at all when export is performed.
I tried to print out the progress, and found that it didn't make any progress at 0.0.

What could be the possible cause?

extension AVMutableComposition {

    private struct AssociatedKeys {
        static var initialTransform: CGAffineTransform!
    }

    var initialTransform: CGAffineTransform {
        get {
            objc_getAssociatedObject(self, &AssociatedKeys.initialTransform) as! CGAffineTransform
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.initialTransform, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }

    convenience init(asset: AVAsset) {
        self.init()

        let videoTrack = asset.tracks(withMediaType: .video).last!
        self.initialTransform = videoTrack.preferredTransform
    
        do {
            try insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: asset, at: .zero)
        }  catch let error {
            debugPrint(error)
        }
    }

    func export() {
        guard let exportSession = AVAssetExportSession(asset: self, presetName: AVAssetExportPresetLowQuality) else {
            print("Could not create an export session")
        }
            
        let outputURL = URL(fileURLWithPath: NSTemporaryDirectory() + "video.mp4")
        exportSession.outputURL = outputURL
        exportSession.outputFileType = .mp4
        exportSession.shouldOptimizeForNetworkUse = true
                            
        let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
            print(exportSession.progress, exportSession.status.rawValue) // 0.0 1
        }
                        
        exportSession.exportAsynchronously {
            //
        }
    }
}

Upvotes: 0

Views: 1032

Answers (2)

kusumoto_teruya
kusumoto_teruya

Reputation: 2475

I restarted my Mac and iPhone and the problem was solved.

Upvotes: 0

Frank Rupprecht
Frank Rupprecht

Reputation: 10408

I think the issue is that you don't retain the exportSession (and the timer). Both objects are deallocated as soon as you leave the export() method. You need to reference them somewhere for as long as the export is running.

Upvotes: 0

Related Questions