Ios AVJ旋转屏幕-Obj C

Ios AVJ旋转屏幕-Obj C,ios,objective-c,avfoundation,transform,video-processing,Ios,Objective C,Avfoundation,Transform,Video Processing,我正在努力改变我的视频方向。它记录在肖像中,然后保存在风景中。更改变换只会使视频在横向视频中旋转。在本例中,使用M_PI_2时,它会消失,因为它会离开屏幕旋转或是平的。但是如果我把它改成M_PI_2/2或者其他什么东西,它看起来是弯曲的。我知道AVFoundation默认情况下会这样做。我如何改变这个?我从本教程中获得了很多这样的代码:但是使用avmutablevideocompositionlayerrinstruction是行不通的 AVMutableCompositionTrack *vi

我正在努力改变我的视频方向。它记录在肖像中,然后保存在风景中。更改变换只会使视频在横向视频中旋转。在本例中,使用M_PI_2时,它会消失,因为它会离开屏幕旋转或是平的。但是如果我把它改成M_PI_2/2或者其他什么东西,它看起来是弯曲的。我知道AVFoundation默认情况下会这样做。我如何改变这个?我从本教程中获得了很多这样的代码:但是使用avmutablevideocompositionlayerrinstruction是行不通的

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime insertTime = kCMTimeZero;
for(AVAsset *videoAsset in self.videoArray){
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:insertTime error:nil];
    // Updating the insertTime for the next insert
    insertTime = CMTimeAdd(insertTime, videoAsset.duration);
}
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
videoTrack.preferredTransform = rotationTransform;

// 3.1 - Create AVMutableVideoCompositionInstruction
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = videoTrack.timeRange;

// 3.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[videoTrack.asset 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 rotationTransform = videoAssetTrack.preferredTransform;

[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
[videolayerInstruction setOpacity:0.0 atTime:videoTrack.timeRange.duration];
有没有办法设置一个定位点或进行我自己的转换?

检查,这是使用AVFoundation设置视频方向的官方解释。简单地说,您需要使用AVMutableVideoCompositionLayerInstruction对象来修改变换,以应用于视频合成中的给定曲目。
然后,让我们讨论一下应该应用的转换。有很多人建议使用AVAssetTrack的preferredTransform。在大多数情况下,它是有效的,您应该使用它来获取assetTrack的视频方向。但有时preferredTransform可能缺少tx(ty)的值(如果您不知道什么是tx(ty),则应在Apple API参考中检查CGAffineTransform),或者tx(ty)的值不准确,从而导致错误的视频定位。
因此,其主要思想是:使用preferredTransform确定原始视频方向,并自行进行变换。
以下是获取轨道方向的代码:

- (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset {
    UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait;
    NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];

    if([tracks count] > 0) {
        AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
        CGAffineTransform t = videoTrack.preferredTransform;

        // Portrait
        if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0) {
            orientation = UIInterfaceOrientationPortrait;
        }
        // PortraitUpsideDown
        if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
            orientation = UIInterfaceOrientationPortraitUpsideDown;
        }
        // LandscapeRight
        if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0) {
            orientation = UIInterfaceOrientationLandscapeRight;
        }
        // LandscapeLeft
        if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0) {
            orientation = UIInterfaceOrientationLandscapeLeft;
        }
    }
    return orientation;
}
以及获取要应用的所需转换的代码:

- (CGAffineTransform)transformBasedOnAsset:(AVAsset *)asset {
    UIInterfaceOrientation orientation = [AVUtilities orientationForTrack:asset];
    AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeVideo][0];
    CGSize naturalSize = assetTrack.naturalSize;
    CGAffineTransform finalTranform;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            finalTranform = CGAffineTransformMake(-1, 0, 0, -1, naturalSize.width, naturalSize.height);
            break;
        case UIInterfaceOrientationLandscapeRight:
            finalTranform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
            break;
        case UIInterfaceOrientationPortrait:
            finalTranform = CGAffineTransformMake(0, 1, -1, 0, naturalSize.height, 0);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            finalTranform = CGAffineTransformMake(0, -1, 1, 0, 0, naturalSize.width);
            break;
        default:
            break;
    }
    return finalTranform;
}
如果您感到困惑,我建议您先拍摄不同方向的视频,然后使用NSStringFromCGAffineTransform()或其他工具打印出视频的首选变换以查看详细信息。

检查,这是使用AVFoundation设置视频方向的官方解释。简单地说,您需要使用AVMutableVideoCompositionLayerInstruction对象来修改变换,以应用于视频合成中的给定曲目。
然后,让我们讨论一下应该应用的转换。有很多人建议使用AVAssetTrack的preferredTransform。在大多数情况下,它是有效的,您应该使用它来获取assetTrack的视频方向。但有时preferredTransform可能缺少tx(ty)的值(如果您不知道什么是tx(ty),则应在Apple API参考中检查CGAffineTransform),或者tx(ty)的值不准确,从而导致错误的视频定位。
因此,其主要思想是:使用preferredTransform确定原始视频方向,并自行进行变换。
以下是获取轨道方向的代码:

- (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset {
    UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait;
    NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];

    if([tracks count] > 0) {
        AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
        CGAffineTransform t = videoTrack.preferredTransform;

        // Portrait
        if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0) {
            orientation = UIInterfaceOrientationPortrait;
        }
        // PortraitUpsideDown
        if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
            orientation = UIInterfaceOrientationPortraitUpsideDown;
        }
        // LandscapeRight
        if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0) {
            orientation = UIInterfaceOrientationLandscapeRight;
        }
        // LandscapeLeft
        if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0) {
            orientation = UIInterfaceOrientationLandscapeLeft;
        }
    }
    return orientation;
}
以及获取要应用的所需转换的代码:

- (CGAffineTransform)transformBasedOnAsset:(AVAsset *)asset {
    UIInterfaceOrientation orientation = [AVUtilities orientationForTrack:asset];
    AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeVideo][0];
    CGSize naturalSize = assetTrack.naturalSize;
    CGAffineTransform finalTranform;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            finalTranform = CGAffineTransformMake(-1, 0, 0, -1, naturalSize.width, naturalSize.height);
            break;
        case UIInterfaceOrientationLandscapeRight:
            finalTranform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
            break;
        case UIInterfaceOrientationPortrait:
            finalTranform = CGAffineTransformMake(0, 1, -1, 0, naturalSize.height, 0);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            finalTranform = CGAffineTransformMake(0, -1, 1, 0, 0, naturalSize.width);
            break;
        default:
            break;
    }
    return finalTranform;
}

如果您感到困惑,我建议您先拍摄不同方向的视频,然后使用NSStringFromCGAffineTransform()或其他工具打印出视频的首选变换以查看详细信息。

第一个答案考虑到缺少TX,TY,我看到了这一点。从高级开发人员到高级开发人员,谢谢。你帮了我们很多。这对我们来说是一个重大问题。我们可以通过Skype或电子邮件咨询您吗?任何帮助都将不胜感激。非常感谢你,先生!好啊dylzjl@gmail.comFirst答案考虑到TX的缺乏,我已经看到了。从高级开发人员到高级开发人员,谢谢。你帮了我们很多。这对我们来说是一个重大问题。我们可以通过Skype或电子邮件咨询您吗?任何帮助都将不胜感激。非常感谢你,先生!好啊dylzjl@gmail.com