gpt4 book ai didi

ios - 在 iOS [Swift 3] 中将视频裁剪为正方形

转载 作者:搜寻专家 更新时间:2023-10-30 23:05:10 29 4
gpt4 key购买 nike

我无法裁剪视频以适应 Swift 3 和 iOS 10.x。执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始视频一样。

我使用以下帖子作为引用: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
}
}

用法如下所示:

    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")
}
}
})
}

最佳答案

您实际上并没有在导出器上设置视频合成

那么试试

exportSession.videoComposition = videoComposition

开始导出之前。

关于ios - 在 iOS [Swift 3] 中将视频裁剪为正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42989649/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com