Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func export(videos: [Video], overlay: UIImage?) {
- let videoComposition = AVMutableVideoComposition()
- videoComposition.frameDuration = CMTimeMake(1, 30)
- videoComposition.renderSize = CGSize(width: 1080, height: 1080)
- let composition = AVMutableComposition()
- var time: Double = 0
- let videoTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
- let audioTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
- for video in videos {
- let asset = AVAsset(url: video.url)
- let videoAssetTrack = asset.tracks(withMediaType: AVMediaTypeVideo).first
- let audioAssetTrack = asset.tracks(withMediaType: AVMediaTypeAudio).first
- let atTime = CMTime(seconds: time, preferredTimescale: 30)
- do {
- if let videoAssetTrack = videoAssetTrack {
- try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: videoAssetTrack, at: atTime)
- }
- if let audioAssetTrack = audioAssetTrack {
- try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: audioAssetTrack, at: atTime)
- }
- } catch( _) {
- self.delegate?.cameraDidFinishExportingVideo(nil)
- return
- }
- time += asset.duration.seconds
- }
- let outputURL = Utilities.getOutputUrl()
- self.exporter = AVAssetExportSession(asset: composition,
- presetName: AVAssetExportPresetHighestQuality)
- if let exporter = self.exporter {
- exporter.outputURL = outputURL
- exporter.shouldOptimizeForNetworkUse = true
- exporter.outputFileType = AVFileTypeMPEG4
- exporter.exportAsynchronously {
- if exporter.status == .completed {
- let video = Video(from: outputURL)
- DispatchQueue.main.async {
- self.delegate?.cameraDidFinishExportingVideo(video)
- }
- } else {
- self.delegate?.cameraDidFinishExportingVideo(nil)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement