ios - 水平翻转视频如果这样则没有镜像效果

标签 ios swift xcode avfoundation avcomposition

在我的自定义相机中,当我使用前置摄像头拍摄视频时,它会像原始 iPhone 相机一样实现镜面效果。我不想那样。我想水平翻转视频,并在下面的这个函数中实现它。我有一个名为 filmedWithFront 的 bool 变量,当使用前置摄像头拍摄视频时,该变量为 true。

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
}

关于ios - 水平翻转视频如果这样则没有镜像效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860896/

相关文章:

objective-c - 无法使用 NSOpenPanel 打开图像

ios - 为什么在我的每个新 xcode 项目中都会显示两个 Storyboard?

xcode - 如果 VALID_ARCHS 设置为 armv7 armv7a,那么 xcode 如何为模拟器构建?

ios - TableView 扩展 indexPathsFor `Un` SelectedRows

ios - 已知 Xcode 错误/陷阱的列表

swift - 可编码 : give a default value to a new non-optional property

ios - 如何在 swift ios 中关闭 2 个 View Controller ?

ios - 选择其他文本字段时如何重置 UIPickerView 内的值?

ios - textField delegate 和 ReactiveCocoa 中使用 textSignal 有什么区别?

ios - UITableViewAutomaticDimension 引起的问题