Noah Labhart
Noah Labhart

Reputation: 157

AVFoundation Overlay text on different videos, then combine and export

I'm having trouble with some simple AVFoundation stuff, and can't seem to find the answer on Stack or the internet. I'm trying to take 8 videos, overlay text on each one individually, then combine them into one complete video. I am combining them successfully, but for some reason I can't seem to get a grip on how to add the text layer over them first.

I've been using Ray Wenderlich's tutorial, which is fantastic. However, I can't seem to figure out my exact scenario. Below is the code I have so far, for combining the videos. Thanks for the help!

        var mainComposition = AVMutableComposition()
        var videoCompositionTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
        var audioCompositionTrack = mainComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
        var insertTime = kCMTimeZero

        var videoCompositionLocal = AVMutableVideoComposition()

        for (index, playerItem) in enumerate(flipsArray) {

            var videoAsset = playerItem.asset
            var word = self.words![index]

            let videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
            let videoTrack: AnyObject = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]

            videoCompositionTrack.insertTimeRange(videoTimeRange,
                                             ofTrack: videoTrack as AVAssetTrack,
                                             atTime: insertTime,
                                             error: nil)


            let audioTimeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
            let audioTrack: AnyObject = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]

            audioCompositionTrack.insertTimeRange(audioTimeRange,
                ofTrack: audioTrack as AVAssetTrack,
                atTime: insertTime,
                error: nil)

            insertTime = CMTimeAdd(insertTime, videoAsset.duration)
        }

        // 4 - Get path
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
                                                        .UserDomainMask,
                                                        true);
        let documentsDirectory = paths[0] as NSString;
        let myPathDocs = documentsDirectory.stringByAppendingPathComponent("flip-\(arc4random() % 1000).mov")

        let url = NSURL.fileURLWithPath(myPathDocs)

        // 5 - Create exporter
        var exporter = AVAssetExportSession(asset: mainComposition,
                                            presetName: AVAssetExportPresetMediumQuality)

        println("-------------")
        println(url)
        println("-------------")

        exporter.outputURL = url
        exporter.outputFileType = AVFileTypeQuickTimeMovie
        exporter.shouldOptimizeForNetworkUse = true
        exporter.exportAsynchronouslyWithCompletionHandler({
            switch exporter.status {
            case  AVAssetExportSessionStatus.Failed:
                println("Merge/export failed: \(exporter.error)")
            case AVAssetExportSessionStatus.Cancelled:
                println("Merge/export cancelled: \(exporter.error)")
            default:
                println("Merge/export complete.")
                self.exportDidFinish(exporter)
            }
        })

EDIT: I have gotten the text to overlay over the video. Now the problem is that the text won't animate (change words) at all. My goal is to change the text value every X seconds, where x is the length of the current video snippet. HELP!

Upvotes: 1

Views: 1176

Answers (1)

Noah Labhart
Noah Labhart

Reputation: 157

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!

Upvotes: 1

Related Questions