Неожиданный вывод после использования SetCropRectangle для обрезки видео - PullRequest
0 голосов
/ 22 октября 2018

Я использую приведенный ниже код для обрезки видео, но он дает масштабированный вывод и повернутое видео, я использую 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

...