I have followed this link for cropping and resizing video:
Swift: Crop and Export Video
I want to crop and resize video to 612*612. My code is given below:
let outputPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output.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 videoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 60)
print(clipVideoTrack.naturalSize.height)
videoComposition.renderSize = CGSizeMake(612,612)
var instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
var transformer: AVMutableVideoCompositionLayerInstruction =
AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
var t1: CGAffineTransform = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, 0)
var t2: CGAffineTransform = CGAffineTransformRotate(t1, CGFloat(M_PI_2))
var finalTransform: CGAffineTransform = t2
transformer.setTransform(finalTransform, atTime: kCMTimeZero)
instruction.layerInstructions = NSArray(object: transformer) as [AnyObject]
videoComposition.instructions = NSArray(object: instruction) as [AnyObject]
let exportPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output2.mov")
var exportUrl: NSURL = NSURL.fileURLWithPath(exportPath as! String)!
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 = videoComposition
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.outputURL = exportUrl
exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
dispatch_async(dispatch_get_main_queue()) {
() -> Void in
let outputURL:NSURL = exporter.outputURL;
self.videoURL = outputURL
let asset:AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
}
})
}
}
I get size video size as 612*612 but content is weird. What could be the issue?
Got solution: I have set value of AVCaptureSession to AVCaptureSessionPresetiFrame960x540 and I changed its value to AVCaptureSessionPreset1280x720 solve my issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With