We add subtitles to a video recorded by the user, but the export by our AVAssetExportSession object fails non-deterministically: sometimes it works, and sometimes it doesn\'
Late to the party, but here's what worked for me. The export would fail "randomly". So then I debug the length of the video track and the length of the audio track.
I noticed that when the audio track was longer than the video track the export would fail.
So I made this change:
let assetVideoTrack = asset.tracks(withMediaType: .video).first!
let assetAudioTrack = asset.tracks(withMediaType: .audio).first!
var validTimeRange:CMTimeRange
if assetVideoTrack.timeRange.duration.value > assetAudioTrack.timeRange.duration.value {
validTimeRange = assetVideoTrack.timeRange
} else {
validTimeRange = assetAudioTrack.timeRange
}
So then I would use that value here:
let instruction = AVMutableVideoCompositionInstruction()
instruction.layerInstructions = [layerInstructions]
instruction.timeRange = validTimeRange
This has solved the problem for me. Works 100% of the times now.
Exported video looks good and recorded audio with it sounds great.
The answer to the questions:
1) What's causing the problem, and what's the solution?
2) Suggestions on how to reproduce the error consistently, which hopefully helps debug the problem?
For me are the following:
slightly different durations between video and audio tracks. Using the shorter time in instruction.timeRange
would fail the export.
set instruction.timeRange
to the shorter time of the two tracks and the export fails.