I want to resize the video I took with AVCaptureMovieFileOutput into squares.

Asked 1 years ago, Updated 1 years ago, 97 views

I'd like to resize the video I took using AVCaptureMovieFileOutput into a square.

At the time of shooting, we set the AVCaptureVideoPreviewLayer with the following properties in the square UIView as preview.

videoGravity=AVLayerVideoGravityResizeAspectFill

The preview is square, but of course the actual file (video) is iPhone camera size (rectangular), so
I would like to resize it in a square just like preview.

If you know a good way, please let me know.

swift swift2

2022-09-30 21:19

1 Answers

Regarding the article in English, I found something like this.

Record square video in iOS

The content is to edit the video taken and saved in the form of a file and output the square video, so I think it is almost in line with the purpose of your question.

Now I don't have time to make a sample application to actually take videos, so I will only post the Objective-C code converted to Swift2 in the article.If you can incorporate it into your app, please try it once.

// output file
    guardlet docURL=NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains:.UserDomainMask).lastelse{
        print("Failed to find Document directory")
        return
    }
    let outputURL=docURL.URLByAppendingPathComponent("output2.mov")
    if NSFileManager.defaultManager().fileExistsAtPath(outputURL.path!){
        _=try?NSFileManager.defaultManager().removeItemAtURL(outputURL)
    }

    // input file
    let asset = AVAsset (URL: fileURL)

    let composition = AVMutableComposition()
    composition.addMutableTrackWithMediaTypeVideo (AVMediaTypeVideo, preferredTrackID:kCMPPersistentTrackID_Invalid)

    // input clip
    letclipVideoTrack=asset.tracksWithMediaType(AVMediaTypeVideo)[0]

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

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

    // rotate to portrait
    let transformer = AVMutableVideoCompositionLayerInstruction (assetTrack: clipVideoTrack)
    lett1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, -(clipVideoTrack.naturalSize.width-clipVideoTrack.naturalSize.height)/2)
    let t2 = CGAffineTransformRotate(t1, CGFloat(M_PI_2))

    let finalTransform=t2
    transformer.setTransform (finalTransform, atTime:kCMTimeZero)
    instruction.layerInstructions=[transformer]
    videoComposition.instructions=[instruction]

    // export
    guard letter exporter = AVAssetExportSession(asset:asset, presetName:AVAssetExportPresetHighestQuality) else {
        print("Failed to create AVAssetExportSession")
        return
    }
    exporter.videoComposition=videoComposition
    exporter.outputURL = outputURL
    exporter.outputFileType=AVFileTypeQuickTimeMovie

    exporter.exportAsynchronouslyWithCompletionHandler{
        NSLog("Exporting done!")
    }

(If you run this code on the assumption that the captured video exists in fileURL (if the original article is in filePath, you will have a converted file named "output2.mov" in the Document directory.)

If you notice anything, such as a conversion error, please let us know in your comments.


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.