Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop Video to Square in iOS [Swift 3]

I am having trouble cropping a video to square with Swift 3, and iOS 10.x. After I perform my crop routine, I save the video to my photo library and it looks the same as the original.

I am using the following post as reference: Cropping AVAsset video with AVFoundation not working iOS 8

func suqareCropVideo(inputURL: NSURL, completion: @escaping (_ outputURL : NSURL?) -> ())
{
    let videoAsset: AVAsset = AVAsset( url: inputURL as URL )
    let clipVideoTrack = videoAsset.tracks( withMediaType: AVMediaTypeVideo ).first! as AVAssetTrack

    let composition = AVMutableComposition()
    composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = CGSize( width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.height )
    videoComposition.frameDuration = CMTimeMake(1, 30)

    let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))


    let transform1: CGAffineTransform = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2)
    let transform2 = transform1.rotated(by: .pi/2)
    let finalTransform = transform2


    transformer.setTransform(finalTransform, at: kCMTimeZero)

    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    // Export
    let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
    print ("random id = \(NSUUID().uuidString)")

    let croppedOutputFileUrl = URL( fileURLWithPath: getOutputPath( NSUUID().uuidString) ) // CREATE RANDOM FILE NAME HERE
    exportSession.outputURL = croppedOutputFileUrl
    exportSession.outputFileType = AVFileTypeQuickTimeMovie

    exportSession.exportAsynchronously() { handler -> Void in
        if exportSession.status == .completed {
            print("Export complete")
            DispatchQueue.main.async(execute: {
                completion(croppedOutputFileUrl as NSURL)
            })
            return
        } else if exportSession.status == .failed {
            print("Export failed - \(String(describing: exportSession.error))")
        }

        completion(nil)
        return
    }
}

Usage shown below:

    if videoURL != nil {
        print ("crop video")
        suqareCropVideo(inputURL: self.videoURL, completion: { (outputURL) -> () in
            print ("compressed url = \(String(describing: outputURL))")

            // Save video to photo library
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:outputURL! as URL)
            }) { saved, error in
                if saved {
                    print ("save successful")
                }
                else {
                    print ("save failed")
                }
            }
        })
    }
like image 384
CTRLALTDELx64 Avatar asked Mar 24 '17 01:03

CTRLALTDELx64


Video Answer


1 Answers

You're not actually setting the video composition on the exporter

So try

exportSession.videoComposition = videoComposition

before starting the export.

like image 151
Agreensh Avatar answered Oct 17 '22 20:10

Agreensh