设置AVAssetExportSession的videoComposition时,视频旋转不适用于AVAsset

时间:2018-04-03 19:26:37

标签: swift rotation avasset avassetexportsession avvideocomposition

我只是想在拍摄后将视频旋转90度。我按照这个帖子中的答案:iOS how to correctly handle orientation when capturing video using AVAssetWriter

当我的资产完成写作时,我调用该方法设置视频的属性,然后导出。

        assetWriter?.finishWriting(completionHandler: {[unowned self] () -> Void in

            let firstAsset = AVURLAsset(url: self.movieURL() as URL)

            guard let exporter = AVAssetExportSession(asset: firstAsset, presetName: AVAssetExportPresetHighestQuality) else { return }

            guard let vidComp = self.getVideoComposition(asset: firstAsset,
                                                         videoSize: CGSize(width:1280,
                                                                           height:720)) else {
                print("Unable to create video composition")
                return
            }
            print(vidComp.instructions)
            exporter.videoComposition = vidComp
            exporter.outputURL = self.movieURL() as URL
            exporter.outputFileType = AVFileType.mov

            exporter.exportAsynchronously() {
                DispatchQueue.main.async(){

                    self.activityTextStatus.text = ""

                    fileURLSenderVal = self.movieURL() as URL

                    let manageCaptureVC = self.storyboard?.instantiateViewController(withIdentifier: "ManageCaptureVC") as! ManageCaptureVC

                    manageCaptureVC.fileURL = fileURLSenderVal
                    manageCaptureVC.takePhotoSelected = self.takePhotoSelected
                    manageCaptureVC.delegate = self

                    self.present(manageCaptureVC, animated: true, completion: nil)

                }
            }

这是getVideoComposition函数。请注意我通过删除任何可以检查方向或尺寸的逻辑来强制旋转,否则。它应该始终应用旋转:

private func getVideoComposition(asset: AVAsset, videoSize: CGSize) -> AVMutableVideoComposition? {

    guard let videoTrack = asset.tracks(withMediaType: AVMediaType.video).first else {
        print("Unable to get video tracks")
        return nil
    }

    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = videoSize

    let seconds: Float64 = Float64(1.0 / videoTrack.nominalFrameRate)
    videoComposition.frameDuration = CMTimeMakeWithSeconds(seconds, 600);

    let layerInst = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)

    var transforms = asset.preferredTransform

        transforms = transforms.concatenating(CGAffineTransform(rotationAngle: CGFloat(90.0 * .pi / 180)))
        transforms = transforms.concatenating(CGAffineTransform(translationX: videoSize.width, y: 0))       
    layerInst.setTransform(transforms, at: kCMTimeZero)

    let inst = AVMutableVideoCompositionInstruction()
    inst.backgroundColor = UIColor.black.cgColor
    inst.layerInstructions = [layerInst]
    inst.timeRange = CMTimeRange(start: kCMTimeZero, duration: asset.duration)

    videoComposition.instructions = [inst]

    return videoComposition

}

但是没有应用轮换。

我不知道这是否有帮助,但我打印出了vidComp的说明:

[AVVideoCompositionInstruction {
    EndTime =     {
        epoch = 0;
        flags = 1;
        timescale = 600;
        value = 1301;
    };
    LayerStack =     (
                {
            ConstantAffineMatrix =             (
                "6.123233995736766e-17",
                1,
                "-1",
                "6.123233995736766e-17",
                1280,
                0
            );
            ConstantCropRectangle =             {
                Height = "1.797693134862316e+308";
                Width = "1.797693134862316e+308";
                X = "-8.988465674311579e+307";
                Y = "-8.988465674311579e+307";
            };
            ConstantOpacity = 1;
            SourceVideoTrackID = 1;
        }
    );
    StartTime =     {
        epoch = 0;
        flags = 1;
        timescale = 1;
        value = 0;
    };
}]

0 个答案:

没有答案