我正在尝试裁剪视频而不缩放视频。 此代码将视频裁剪到正确的帧,但也将其缩放到较大的放大比例
func cropVideo(outputFileUrl: URL ) -> (AVMutableVideoComposition) {
let videoAsset: AVAsset = AVAsset( url: outputFileUrl )
let videoComposition = AVMutableVideoComposition()
let width = ceil(UIScreen.main.bounds.size.width / 16) * 16
videoComposition.renderSize = CGSize.init(width: width, height: width * (8/6))
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds( videoAsset.duration.seconds, 30 ) )
if let clipVideoTrack = videoAsset.tracks( withMediaType: AVMediaTypeVideo ).first {
videoComposition.frameDuration = CMTimeMake(1, Int32(clipVideoTrack.nominalFrameRate))
let transformer = AVMutableVideoCompositionLayerInstruction( assetTrack: clipVideoTrack )
let transform1 = CGAffineTransform( translationX: clipVideoTrack.naturalSize.height,
y: -( clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height ) / 2 )
let transform2 = transform1.rotated(by: CGFloat( Double.pi / 2 ) )
transformer.setTransform( transform2, at: kCMTimeZero)
instruction.layerInstructions = [transformer]
}
videoComposition.instructions = [instruction]
return videoComposition
}