Duet - Merge 2 Videos Side by Side

三世轮回 提交于 2019-11-28 11:49:25

To achieve this, I would create a new AVMutableComposition object containing 2 tracks, and set transform on each to place them side by side:

let composition = AVMutableComposition(urlAssetInitializationOptions: <your options>)
let videoTrackA = composition.addMutableTrack(withMediaType:.video, preferredTrackID:kCMPersistentTrackID_Invalid);
let videoTrackB = composition.addMutableTrack(withMediaType:.video, preferredTrackID:kCMPersistentTrackID_Invalid);

videoTrackA.preferredTransform = CGAffineTransform(translationX: <yourX_for_A>, y:0.0)
videoTrackB.preferredTransform = CGAffineTransform(translationX: <yourX_for_B>, y:0.0)

Then. save it using:

let exporter = AVAssetExportSession(asset:<yourAsset>, presetName:<yourPresetName>)
exporter.exportAsynchronously(completionHandler: <yourCompletionHandler>)

(Swift code not tested).

in fact, AVAssetExportSession is for simple needs, and it is too simple for your situation.

You must use AVAssetWriter.

You add AVAssetWriterInput to your AVAssetWriter.

You can configure trasnform of the AVAssetWriterInput using its transform property.

Then, you feed your AVAssetWriterInput with CMSampleBuffer (each images buffer) using append calls.

See full Apple documentation for detailed example: https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!