在IOS中沿水平轴翻转前置摄像头录制的视频

时间:2014-11-24 07:21:43

标签: ios video-capture cgaffinetransform avmutablecomposition front-camera

我正在尝试在录制视频时解除由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 个答案:

答案 0 :(得分:14)

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

替换代码:

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);

答案 1 :(得分:1)

Swift 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)

答案 2 :(得分: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)
    }

答案 3 :(得分:0)

在Swift(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