The goal is to export an arbitrary segment of some video (e.g., middle third, last half), but AVAssetExportSession
only succeeds if the starting point is the start of the video.
If cmStartTime
is not 0, AVAssetExportSession
fails with this error:
Failed: Optional(Error Domain=AVFoundationErrorDomain Code=-11841 "Operation Stopped" UserInfo=0x175872d00 {NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The video could not be composed.}).
// Create main composition & its tracks
let mainComposition = AVMutableComposition()
let compositionVideoTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
let compositionAudioTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
// Get source video & audio tracks
let videoURL = NSURL(fileURLWithPath: fileURL)
let videoAsset = AVURLAsset(URL: videoURL, options: nil)
let sourceVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
let sourceAudioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]
// Define time values for video
let timescale = Int32(600)
let cmStartTime = CMTimeMake(Int64(CGFloat(0.5) * CGFloat(timescale)), timescale)
let cmEndTime = CMTimeMake(10, 1)
let timeRange = CMTimeRangeMake(cmStartTime, cmEndTime)
// Add source tracks to composition
do {
try compositionVideoTrack.insertTimeRange(timeRange, ofTrack: sourceVideoTrack, atTime: cmStartTime)
try compositionAudioTrack.insertTimeRange(timeRange, ofTrack: sourceAudioTrack, atTime: cmStartTime)
} catch {
printError("Error with insertTimeRange while exporting video: \(error)")
}
// Create video composition
let renderSize = compositionVideoTrack.naturalSize
let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = renderSize
videoComposition.frameDuration = CMTimeMake(Int64(1), Int32(frameRate))
// Add layer instruction to video composition
...
// Apply effects to video
...
// Define export URL
let exportPath = getUniqueTempPath(gMP4File)
let exportURL = NSURL(fileURLWithPath: exportPath)
// Create exporter
let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)!
exporter.videoComposition = videoComposition
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = exportURL
exporter.shouldOptimizeForNetworkUse = true
exporters.append(exporter)
// Export video
exporter.exportAsynchronouslyWithCompletionHandler() {
// Finish stuff
}
The problem arose from not understanding CMTimeRangeMake
and insertTimeRange
.
The second value of CMTimeRangeMake
should be the clip duration, not the end time. So if your start time is the 5 second mark, and the clip lasts 10 seconds, the second value should be 10, not 15.
The atTime
parameter of insertTimeRange
should be kCMTimeZero
since the goal is to create a new clip. In other words, this value says where in the new track to insert the clip from the source track.