SDVideoCamera 前置摄像头颠倒问题

2020-09-14  本文已影响0人  H丶ym

// 处理视频尺寸大小

+ (void)changeVideoSizeWithAsset:(AVAsset *)asset passThroughLayer:(AVMutableVideoCompositionLayerInstruction *)passThroughLayer {



    AVAssetTrack *videoAssetTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;

    if(videoAssetTrack ==nil) {

        return;

    }

    CGSizenaturalSize = videoAssetTrack.naturalSize;



    STVideoOrientation * t = [STVideoUtils videoDegressWithVideoAsset:asset];

    if(t.videoDegress==90) {

        naturalSize =CGSizeMake(naturalSize.height, naturalSize.width);

    }



    if((int)naturalSize.width%2!=0) {

        naturalSize =CGSizeMake(naturalSize.width+1.0, naturalSize.height);

    }



    CGSizevideoSize =STVideoSize;

    if(t.videoDegress==90) {

        if (t.position == AVCaptureDevicePositionFront){

            CGAffineTransformtransform =CGAffineTransformIdentity;

            CGFloatheight = videoSize.width* naturalSize.height/ naturalSize.width;

            transform =CGAffineTransformScale(transform, videoSize.width/naturalSize.width* -1, height/naturalSize.height);

            transform =CGAffineTransformRotate(transform,M_PI_2);

            [passThroughLayersetTransform:transformatTime:kCMTimeZero];

        }else{

            CGFloatheight = videoSize.width* naturalSize.height/ naturalSize.width;

            CGAffineTransformtranslateToCenter =CGAffineTransformMakeTranslation(videoSize.width, videoSize.height/2.0- height/2.0);

            CGAffineTransformscaleTransform =CGAffineTransformScale(translateToCenter, videoSize.width/naturalSize.width, height/naturalSize.height);

            CGAffineTransformmixedTransform =CGAffineTransformRotate(scaleTransform,M_PI_2);

            [passThroughLayersetTransform:mixedTransformatTime:kCMTimeZero];

        }

    }else{

        CGFloatheight = videoSize.width* naturalSize.height/ naturalSize.width;

        CGAffineTransformtranslateToCenter =CGAffineTransformMakeTranslation(0, videoSize.height/2.0- height/2.0);

        CGAffineTransformscaleTransform =CGAffineTransformScale(translateToCenter, videoSize.width/naturalSize.width, height/naturalSize.height);

        [passThroughLayersetTransform:scaleTransformatTime:kCMTimeZero];

    }

}

// 计算视频角度

+ (STVideoOrientation *)videoDegressWithVideoAsset:(AVAsset *)videoAsset {



    NSIntegervideoDegress =0;

    AVCaptureDevicePosition device = AVCaptureDevicePositionUnspecified;

    STVideoOrientation * t = [[STVideoOrientation alloc] init];

    NSArray*assetVideoTracks = [videoAssettracksWithMediaType:AVMediaTypeVideo];

    if(assetVideoTracks.count>0) {

        AVAssetTrack*videoTrack = assetVideoTracks.firstObject;

        CGAffineTransformaffineTransform = videoTrack.preferredTransform;



        if(affineTransform.a==0&& affineTransform.b==1.0&& affineTransform.d==0){

            videoDegress =90;

            if(affineTransform.c==1.0){

                device =AVCaptureDevicePositionFront;

            }else{

                device =AVCaptureDevicePositionBack;

            }

        }elseif(affineTransform.a==0&& affineTransform.b== -1.0&& affineTransform.d==0){

            videoDegress =270;

            if(affineTransform.c==1.0){

                device =AVCaptureDevicePositionBack;

            }else{

                device =AVCaptureDevicePositionFront;

            }

        }elseif(affineTransform.a==1.0&& affineTransform.b==0&& affineTransform.c==0){

            videoDegress =0;

            if(affineTransform.c==1.0){

                device =AVCaptureDevicePositionBack;

            }else{

                device =AVCaptureDevicePositionFront;

            }

        }elseif(affineTransform.a== -1.0&& affineTransform.b==0&& affineTransform.c==0){

            videoDegress =180;

            if(affineTransform.c==1.0){

                device =AVCaptureDevicePositionFront;

            }else{

                device =AVCaptureDevicePositionBack;

            }

        }



        t.position= device;

        t.videoDegress= videoDegress;



    }

    returnt;

}

上一篇 下一篇

猜你喜欢

热点阅读