Я хочу сделать видео на квадрат в Swift, XCode.Я мог бы сделать это из левого угла, но я хочу сделать видео в квадрат по любой координате.Ниже приведен код, который может обрезать видео на 50-50 квадратов от левого угла.Пожалуйста, научите меня, как обрезать видео квадрат.
func mergeMovie(url: URL) {
let baseMovieURL = url
var videoAsset: AVURLAsset
var videoTrack: AVAssetTrack
var audioTrack: AVAssetTrack
videoAsset = AVURLAsset(url: baseMovieURL, options:nil)
let videoTracks = videoAsset.tracks(withMediaType: AVMediaType.video)
videoTrack = videoTracks[0]
let audioTracks = videoAsset.tracks(withMediaType: AVMediaType.audio)
audioTrack = audioTracks[0]
let mixComposition : AVMutableComposition = AVMutableComposition()
let compositionVideoTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let compositionAudioTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
try! compositionVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: videoTrack, at: CMTime.zero)
try! compositionAudioTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: audioTrack, at: CMTime.zero)
compositionVideoTrack.preferredTransform = videoAsset.tracks(withMediaType: AVMediaType.video)[0].preferredTransform
let videoSize: CGSize = CGSize(width: 50, height: 50)
let videoComp: AVMutableVideoComposition = AVMutableVideoComposition()
videoComp.renderSize = videoSize
videoComp.frameDuration = CMTimeMake(value: 1, timescale: 30)
let instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration)
let layerInstruction: AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction.init(assetTrack: compositionVideoTrack)
instruction.layerInstructions = [layerInstruction]
videoComp.instructions = [instruction]
_assetExport = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
_assetExport?.videoComposition = videoComp
let exportPath: String = NSHomeDirectory() + "/tmp/createdMovie.mov"
let exportUrl: URL = URL(fileURLWithPath: exportPath)
_assetExport?.outputFileType = AVFileType.mov
_assetExport?.outputURL = exportUrl
_assetExport?.shouldOptimizeForNetworkUse = true
if FileManager.default.fileExists(atPath: exportPath) {
try! FileManager.default.removeItem(atPath: exportPath)
}
_assetExport?.exportAsynchronously(completionHandler: {() -> Void in
if self._assetExport?.status == AVAssetExportSession.Status.failed {
print("failed:", self._assetExport?.error)
}
if self._assetExport?.status == AVAssetExportSession.Status.completed {
print("completed")
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: exportUrl)
})
}
})
}