在 iOS [Swift 3] 中将视频裁剪为正方形
Crop Video to Square in iOS [Swift 3]
我无法裁剪视频以符合 Swift 3 和 iOS 10.x。执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始视频一样。
我正在使用以下 post 作为参考:
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
开始导出之前。
presetName: AVAssetExportPresetHighestQuality,
更改现名,可以select多选。
就我而言:-
let preferredPreset = AVAssetExportPresetLowQuality
成功了。
我无法裁剪视频以符合 Swift 3 和 iOS 10.x。执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始视频一样。
我正在使用以下 post 作为参考:
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
开始导出之前。
presetName: AVAssetExportPresetHighestQuality,
更改现名,可以select多选。
就我而言:-
let preferredPreset = AVAssetExportPresetLowQuality
成功了。