Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop video to a particular size?

Tags:

ios

swift

video

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?

enter image description here

enter image description here

like image 958
Pooja Shah Avatar asked Jul 31 '15 11:07

Pooja Shah


1 Answers

Got solution: I have set value of AVCaptureSession to AVCaptureSessionPresetiFrame960x540 and I changed its value to AVCaptureSessionPreset1280x720 solve my issue.

like image 124
Pooja Shah Avatar answered Sep 26 '22 06:09

Pooja Shah