水平翻转视频,如果可以的话,它没有镜像效果



在我的自定义相机中,当我用前面摄像头拍摄视频时,它会像原始的iPhone摄像头一样发挥镜像效果。我不要那个。我想水平翻转视频,并在下面的此功能中实现。我有一个布尔变量,称为"拍摄Withfront",当视频与前置摄像头拍摄时是正确的。

var filmedWithFront = false
func cropVideo(_ outputFileURL:URL){
    let videoAsset: AVAsset = AVAsset(url: outputFileURL) as AVAsset
    let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaType.video).first! as AVAssetTrack
    let composition = AVMutableComposition()
    composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())
    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = CGSize(width: 720, height: 1280)
    videoComposition.frameDuration = CMTimeMake(1, 30)
    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))
    // rotate to portrait
    let transformer:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
    let t1 = CGAffineTransform(translationX: 720, y: 0);
    let t2 = t1.rotated(by: CGFloat(CGFloat.pi/2));
    transformer.setTransform(t2, at: kCMTimeZero)
    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    if filmedWithFront == true {
        // This is where I want to add the code to flip video horizontally
    }

    let removedPath = outputFileURL.path
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
    let cropUniqueId = NSUUID().uuidString
    let outputPath = "(documentsPath)/(cropUniqueId).mov"
    arrayOfStringPaths.append(outputPath)
    stringOfArrayPaths = outputPath
    let relativePath = "(cropUniqueId).mov"
    let relativeURL = URL(fileURLWithPath: relativePath)
    saveData(arrayPath: relativePath)
    let outputUrl = URL(fileURLWithPath: outputPath, relativeTo: relativeURL)
    let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPreset1280x720)!
    exporter.videoComposition = videoComposition
    exporter.outputURL = outputUrl
    exporter.outputFileType = AVFileType.mov
    exporter.shouldOptimizeForNetworkUse = true
    exporter.exportAsynchronously(completionHandler: { () -> Void in
        DispatchQueue.main.async(execute: {
            self.handleExportCompletion(exporter, removedPath)
        })
    })
}

这是我为最终修复前摄像头的镜像视频输出所做的转换的片段... videoinputwriter是avassetWriterInput。希望这会有所帮助。

if (cameraPosition == .front) {
    var transform: CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
    transform = transform.rotated(by: CGFloat(Double.pi/2))
    self.videoInputWriter.transform = transform
}

相关内容

  • 没有找到相关文章

最新更新