I am trying to join 2 video clips using AVFoundation. For testing purposes, here I am trying to download a single video clip, add its video and audio tracks to the composition, and then export using AVAssetExportSession .
When I run the code below, "Export" is displayed, but the export callback is never executed. In addition, if I periodically check the export progress ( print(exporter.progress) ), I believe that progress is always 0.0, even after a few minutes. If I print the status, I find that he is βwaitingβ for something.
// 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) })
Mistakes never occur. It seems that export never starts. I'm not sure what he expects.
Edit: Something creepy is happening. If I restart my phone and run the code, it will work. But it works exactly 1 time. If I run it a second time, nothing happens as usual. But when I restart my phone and start it again, it works again.
Edit 2: I tried it on another phone and it works reliably every time. What is wrong with my phone in the world?
source share