如何在Swift中修剪/裁剪视频?

时间:2019-07-04 11:43:13

标签: ios swift iphone xcode

我想制作一个视频以在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)
            })
        }
    })
}

0 个答案:

没有答案