How to combine video clips with different orientation using AVFoundation

Viewed 18832

I am trying to combine several video clips into one using AVFoundation. I can create a single video using AVMutableComposition using the code below

AVMutableComposition *composition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

CMTime startTime = kCMTimeZero;

/*videoClipPaths is a array of paths of the video clips recorded*/

//for loop to combine clips into a single video
for (NSInteger i=0; i < [videoClipPaths count]; i++) {

    NSString *path = (NSString*)[videoClipPaths objectAtIndex:i];

    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    [url release];

    AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    //set the orientation
    if(i == 0)
    {
        [compositionVideoTrack setPreferredTransform:videoTrack.preferredTransform];
    }

    ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:videoTrack atTime:startTime error:nil];
    ok = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:audioTrack atTime:startTime error:nil];

    startTime = CMTimeAdd(startTime, [asset duration]);
}

//export the combined video
NSString *combinedPath = /* path of the combined video*/;

NSURL *url = [[NSURL alloc] initFileURLWithPath: combinedPath];

AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPreset640x480] autorelease];

exporter.outputURL = url;
[url release];

exporter.outputFileType = [[exporter supportedFileTypes] objectAtIndex:0];

[exporter exportAsynchronouslyWithCompletionHandler:^(void){[self combineVideoFinished:exporter.outputURL status:exporter.status error:exporter.error];}];

The code above works fine if all the video clips were recorded in the same orientation (portrait or landscape). However if I have a mixture of orientations in the clips, the final video will have part of it rotated 90 degrees to the right (or left).

I was wondering is there a way to transform all clips to the same orientation (e.g. the orientation of the first clip) while composing them. From what I read from the XCode document AVMutableVideoCompositionLayerInstruction seems can be used to transform AVAsset, but I am not sure how to create and apply several different layer instruction to corresponding clips and use then in the composition (AVMutableComposition*)

Any help would be appreciated!

2 Answers

Here is @bogardon's answer in swift 4+

 import ARKit

class ARKitSampleViewController: UIViewController {
    var label: UILabel?
    var planeFound = false

    func plane(from anchor: ARPlaneAnchor?) -> SCNNode? {
        let plane = SCNPlane(width: CGFloat(anchor?.extent.x ?? 0.0), height: CGFloat(anchor?.extent.z ?? 0.0))

        plane.firstMaterial?.diffuse.contents = UIColor.clear
        let planeNode = SCNNode(geometry: plane)
        planeNode.position = SCNVector3Make(anchor?.center.x ?? 0.0, 0, anchor?.center.z ?? 0.0)
        // SCNPlanes are vertically oriented in their local coordinate space.
        // Rotate it to match the horizontal orientation of the ARPlaneAnchor.
        planeNode.transform = SCNMatrix4MakeRotation(-.pi * 0.5, 1, 0, 0)

        return planeNode
    }

// MARK: -  ARSCNViewDelegate
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        if planeFound == false {
            if (anchor is ARPlaneAnchor) {
                DispatchQueue.main.async(execute: {
                    self.planeFound = true
                    self.label?.text = "DANCEFLOOR FOUND. LET'S BOOGIE"

                    let overlay = UIView(frame: self.view.frame)
                    overlay.backgroundColor = UIColor.black
                    overlay.alpha = 0
                    if let label = self.label {
                        self.view.insertSubview(overlay, belowSubview: label)
                    }

                    UIView.animate(withDuration: 1.5, delay: 2, options: .curveEaseIn, animations: {
                        self.label?.alpha = 0
                        overlay.alpha = 0.5
                    }) { finished in
                        let planeAnchor = anchor as? ARPlaneAnchor
                        // Show the disco ball here
                    }
                })
            }
        }
    }
}
Related