AVAssetExportSession does not start export / pause waiting

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?

+5
source share
2 answers

This code, which basically does the same thing, adds an audio track to the video. It really seems to be yours, and he is currently working on the app in the 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 } 

I see nothing wrong with our code, except for the fact that I'm not sure if the resource has the correct duration, since getting it is an async task, unless you ask explicitly.
So, when you create the AVAsset pass option AVURLAssetPreferPreciseDurationAndTimingKey as true .

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

I would suggest that one of your microphones does not work correctly, and because of this it is stuck, I had similar behavior with the rear camera. Did you try to export the movie without recording simultaneously, just to check if a possible broken microphone is involved?

0
source

Source: https://habr.com/ru/post/1274624/


All Articles