使用SetCropRectangle裁剪视频后出现意外输出

时间:2018-10-22 07:46:20

标签: ios swift video avfoundation crop

我正在使用下面的代码来裁剪视频,但是它提供了缩放的输出和旋转的视频,我正在使用setCropRectangle来定义裁剪区域,我已经附上了我的结果的屏幕截图,请参考下面的代码

func CroppingToRecttry(inputURL : NSURL , completion: ((_ outputUrl: URL) -> Void)? = nil ) {
    let fileManager = FileManager.default
    let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    var outputPath = documentDirectory.appendingPathComponent("output")
    do {

        try fileManager.createDirectory(at: outputPath, withIntermediateDirectories: true, attributes: nil)
        outputPath = outputPath.appendingPathComponent("\(String(describing: inputURL.lastPathComponent)).mp4")
    } catch let error {
        print(error)
    }

    try? fileManager.removeItem(at: outputPath)
    let asset = AVAsset.init(url: inputURL as URL)
    let composition = AVMutableComposition()
    let clipVideoTrack = asset.tracks(withMediaType: AVMediaType.video)[0]
    let vtrack =  composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
    do {
        print("abcd")
        let range = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60,600))
        try vtrack?.insertTimeRange(range, of: clipVideoTrack, at: kCMTimeZero)
    } catch { print(error) }

    let firstTransform = vtrack?.preferredTransform;
    let fromLayer = AVMutableVideoCompositionLayerInstruction(assetTrack: vtrack!)
    fromLayer.setTransform(firstTransform!, at: kCMTimeZero)
    fromLayer.setCropRectangle(CGRect.init(x: (cropView.cropRect?.origin.x)!, y: (cropView.cropRect?.origin.y)!, width: (cropView.cropRect?.size.width)!, height: (cropView.cropRect?.size.height)!), at: kCMTimeZero)
    let instruction = AVMutableVideoCompositionInstruction()
    instruction.layerInstructions = [fromLayer]
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60,600))
    let videoComposition = AVMutableVideoComposition()
    videoComposition.instructions = [instruction]
    videoComposition.renderSize = CGSize(width: 410, height: 410)
    videoComposition.frameDuration = CMTimeMake(1, 30)
    guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else { return }


    exporter.outputURL = outputPath

    exporter.outputFileType = .mp4
    exporter.videoComposition = videoComposition
    exporter.exportAsynchronously {
        switch exporter.status {
        case .completed:
            self.lastURL = outputPath
            self.frasset = AVAsset(url: outputPath)
            let item=AVPlayerItem(asset: self.frasset!)
            self.player.replaceCurrentItem(with: item)
            print("exported at \(String(describing: outputPath))")
            completion?(outputPath)
        case .failed:
            print("failed \(exporter.error.debugDescription)")
        case .cancelled:
            print("cancelled \(exporter.error.debugDescription)")
        default: break
        }
    }


}

下面是输入和输出屏幕截图:-

cropping area

Output

0 个答案:

没有答案