How to overlay one video on another in iOS?
I guess you want to produce something like this:
You don't want an AVCaptureSession
, because you're not capturing video. You want an AVMutableComposition
. You need to read the “Editing” section of the AV Foundation Programming Guide. Here's a summary of what you need to do:
Create the
AVAsset
objects for your videos and wait for them to load their tracks.Create an
AVMutableComposition
.Add a separate
AVMutableCompositionTrack
to the composition for each of the input videos. Make sure to assign explicit, different track IDs to each track. If you let the system pick, it will use track ID 1 for each and you won't be able to access both later in the compositor.Create an
AVMutableVideoComposition
.Create an
AVMutableVideoCompositionInstruction
.For each input video, create an
AVMutableVideoCompositionLayerInstruction
and explicitly assign the track IDs you used back in step 3.Set the
AVMutableVideoCompositionInstruction
'slayerInstructions
to the two layer instructions you created in step 6.Set the
AVMutableVideoComposition
'sinstructions
to the instruction you created in step 5.Create a class that implements the
AVVideoCompositing
protocol. Set thecustomVideoCompositorClass
of the video composition (created in step 4) to this custom class (e.g.videoComposition.customVideoCompositorClass = [CustomVideoCompositor class];
).In your custom compositor, get the input pixel buffers from the
AVAsynchronousVideoCompositionRequest
and use them to draw the composite frame (containing a background video frame overlaid by a circular chunk of the foreground video frame). You can do this however you want. I did it using Core Graphics because that's easy, but you'll probably want to use OpenGL (or maybe Metal) for efficiency in a production app. Be sure to specifykCVPixelBufferOpenGLESCompatibilityKey
if you go with OpenGL.Create an
AVAssetExportSession
using your composition from step 1.Set the session's output URL and file type.
Set the session's
videoComposition
to the video composition from step 4.Tell the session to
exportAsynchronouslyWithCompletionHandler:
. It will probably be slow!
You can find my test project in this github repository.
AVFoundation Overlay text on different videos, then combine and export
I found a way to solve the problem, if anyone finds it useful. Instead of stitching all of the 8 videos together, and then applying an animated layer of words over the top, with timed word switching, before the export...
...I exported each video individually with the word overlay'd on top of it. Then called a different method to stitch together those 8 newly exported videos into one video. Since the word changes matched exactly to the duration of the assets, this worked perfectly for me.
Hope this helps someone!
Related Topics
iPad 3 - Opengl Bug with Keagldrawablepropertyretainedbacking and Retina
Add a Navigation Bar to a View Without a Navigation Controller
Open an External Link in Safari (Cordova)
Undefined Symbols for Architecture Armv7 for Cocoapods Libraries
Avplayerviewcontroller Using Audio-Only Avplayer
Download PDF and Save to the "Files" in Iphone, Not to the App Data, Swift
How to Use Avcapturephotooutput to Capture Photo Swift + Xcode
Remove Underline on Uibutton in iOS 7
Memory Leak with "Libbacktracerecording.Dylib" in React Native iOS Application
Keep Getting "Unbalanced Calls to Begin/End Appearance Transitions for <Viewcontroller>" Error
Understanding Model-View-Controller
A Lighter Way of Discovering Text Writing Direction
Apns Push Notifications Not Working on Production