4

我正在尝试在录制视频时取消镜像 iPhone 前置摄像头创建的镜像效果。基本上,我想将前置摄像头拍摄的视频沿水平轴翻转,这样最终编辑的视频就不会像内置相机应用程序那样具有镜像效果。我正在使用 AVCaptureSession 录制视频。

// 3.1 - Create AVMutableVideoCompositionInstruction
    AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration);

// 3.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

UIImageOrientation videoAssetOrientation_  = UIImageOrientationUp;
BOOL isVideoAssetPortrait_  = NO;
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0)
{
    videoAssetOrientation_ = UIImageOrientationRight;
    isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0)
{
    videoAssetOrientation_ =  UIImageOrientationLeft;
    isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0)
{
    videoAssetOrientation_ =  UIImageOrientationUp;
}
if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0)
{
    videoAssetOrientation_ = UIImageOrientationDown;
}

//**********下面是我正在应用的转换,您可能会感兴趣**********

CGAffineTransform t = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, 0.0);
t = CGAffineTransformMakeRotation(degreesToRadians(90.0));
t = CGAffineTransformMakeScale(-1, 1);  //this line is where I am having problem

[videolayerInstruction setTransform:t atTime:kCMTimeZero];
[videolayerInstruction setOpacity:1.0 atTime:self.videoAsset.duration];


// 3.3 - Add instructions
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];


CGSize naturalSize;
if(isVideoAssetPortrait_)
{
    naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
}
else
{
    naturalSize = videoAssetTrack.naturalSize;
}

float renderWidth, renderHeight;
renderWidth = naturalSize.width;
renderHeight = naturalSize.height;
mainCompositionInst.renderSize = CGSizeMake(renderWidth, renderHeight);
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);

在上面的代码中,当我删除缩放变换 t = CGAffineTransformMakeScale(-1, 1);

我得到方向正确的视频帧,但它们没有翻转。当我应用缩放变换时,我得到空白视频帧。目前,我只为纵向模式视频执行此操作。

如果有人对此有任何了解,请分享。先感谢您。

4

4 回答 4

16

您在仿射变换方面犯了一个小错误。

替换代码:

CGAffineTransform t = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, 0.0);
t = CGAffineTransformMakeRotation(degreesToRadians(90.0));
t = CGAffineTransformMakeScale(-1, 1);  //this line is where I am having problem

使用更正的代码:

CGAffineTransform t = CGAffineTransformMakeScale(-1.0f, 1.0f);
t = CGAffineTransformTranslate(t, -videoAssetTrack.naturalSize.width, 0);
t = CGAffineTransformRotate(t, (degreesToRadians(90.0)));
t = CGAffineTransformTranslate(t, 0.0f, -videoAssetTrack.naturalSize.width);
于 2015-01-14T14:28:32.707 回答
1

斯威夫特 3 版本

        let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
    var transform:CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
    transform = transform.translatedBy(x: -clipVideoTrack.naturalSize.width, y: 0.0)
    transform = transform.rotated(by: CGFloat(Double.pi/2))
    transform = transform.translatedBy(x: 0.0, y: -clipVideoTrack.naturalSize.width)
于 2017-10-02T12:41:10.363 回答
0
var size = videotrackatfirst.naturalSize
var transform = videotrackatfirst.preferredTransform

if (size.width == transform.tx && size.height == transform.ty) {          
                println("UIInterfaceOrientationLandscapeRight")
                var invert = CGAffineTransformInvert(videotrackatfirst.preferredTransform)
                layerInstruction.setTransform(invert, atTime: insertTime)
    }
于 2015-07-28T20:32:25.803 回答
0

在斯威夫特 (2.0)

    var transform:CGAffineTransform = CGAffineTransformMakeScale(-1.0, 1.0)
    transform = CGAffineTransformTranslate(transform, -videoInput.naturalSize.width, 0.0)
    transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
    transform = CGAffineTransformTranslate(transform, 0.0, -videoInput.naturalSize.width)
    videoInput.transform = transform
于 2016-10-28T17:21:48.677 回答