I want to rotate my captured video from landscape to portrait, my code is below:
if self.isDeviceRotatedToLandscape
{
let outputPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output2.mov")
let outputURL : NSURL = NSURL(fileURLWithPath: outputPath as String)!
let fileManager : NSFileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(outputPath as String))
{
let asset : AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
if let clipVideoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
{
var FirstlayerInstruction:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
if let FirstAssetTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
{
let FirstAssetTrack_ = UIImageOrientation.Up
var isFirstAssetPortrait_ = false
var FirstAssetScaleToFitRatio:CGFloat = 1.0
var FirstAssetScaleFactor:CGAffineTransform = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio, FirstAssetScaleToFitRatio)
FirstlayerInstruction.setTransform(CGAffineTransformConcat(CGAffineTransformConcat(FirstAssetTrack.preferredTransform,FirstAssetScaleFactor), CGAffineTransformMakeTranslation(0, 160)), atTime: kCMTimeZero)
FirstlayerInstruction.setOpacity(0.0, atTime: asset.duration)
var MainInstruction:AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
MainInstruction.layerInstructions = [FirstlayerInstruction]
var MainCompositionInst: AVMutableVideoComposition = AVMutableVideoComposition()
MainCompositionInst.instructions = [MainInstruction]
MainCompositionInst.frameDuration = CMTimeMake(1, 30)
MainCompositionInst.renderSize = CGSizeMake(612,612)
let exportPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output1212.mov")
var exportUrl: NSURL = NSURL.fileURLWithPath(exportPath as! String)!
let fileManager : NSFileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(exportPath as String))
{
var error:NSError? = nil
if(fileManager.removeItemAtPath(exportPath as String, error: &error))
{
//Error - handle if requried
}
}
var exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exporter.videoComposition = MainCompositionInst
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.outputURL = exportUrl
exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
print("hi")
UISaveVideoAtPathToSavedPhotosAlbum(exportPath as String, self, nil, nil)
})
}
}
}
}
But video is not even created and save to photo app. I don't no where is mistake. Please help to solve this issue, I am stuck and need to solve this as soon as possible