修剪视频时如何检测视频方向?

时间:2014-07-08 13:04:00

标签: ios objective-c video avassetexportsession

当我以横向模式录制视频时,我第二次出现错误的视频定位。对于第一次记录,我在didFinishMediaWithInfo

中获得了正确的视频方向

现在。我正在使用以下代码来检测视频方向。

 AVAsset *firstAsset = [AVAsset assetWithURL:videoUrl];
    AVAssetTrack *FirstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

        UIImageOrientation FirstAssetOrientation_  = UIImageOrientationUp;

        BOOL  isFirstAssetPortrait_  = NO;

        CGAffineTransform firstTransform = [FirstAssetTrack preferredTransform];

        if(firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0)
        {
            FirstAssetOrientation_= UIImageOrientationRight; isFirstAssetPortrait_ = YES;
        }
        if(firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0)
        {
            FirstAssetOrientation_ =  UIImageOrientationLeft; isFirstAssetPortrait_ = YES;
        }
        if(firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0)
        {
            FirstAssetOrientation_ =  UIImageOrientationUp;
        }
        if(firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0)
        {
            FirstAssetOrientation_ = UIImageOrientationDown;
        }

 if(isFirstAssetPortrait_)
    {
        NSLog(@"Portrait mode");
     }
else{
        NSLog(@"Landscape mode");

}

上面的代码给出正确的方向,如果首先记录肖像模式。然后记录横向模式。如果用户再次以横向模式录制视频。然后它提供错误的视频方向并使肖像BOOL是。

如果视频以横向模式录制并使用AVAssetExportSession进行修剪。然后trimed视频在MPMoviePlayer中以错误的方向播放。如何解决这个问题?

请指导我。

1 个答案:

答案 0 :(得分:2)

AVURLAsset* movieAsset = [AVURLAsset URLAssetWithURL: movieFileURL options: nil];
NSArray *tracks = [movieAsset tracksWithMediaType:AVMediaTypeVideo];

CGSize videoSize = CGSizeZero;
if ([tracks count] != 0)
{
    AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
    videoSize = videoTrack.naturalSize;

    CGRect videoRect = CGRectMake(0.0, 0.0, videoSize.width, videoSize.height);
    videoRect = CGRectApplyAffineTransform(videoRect, videoTrack.preferredTransform);

    if (videoRect.size.height > videoRect.size.width)
    {
        NSLog(@"Portrait mode");
    }
    else if (videoRect.size.height < videoRect.size.width)
    {
        NSLog(@"Landscape mode");
    }
    else
    {
        NSLog(@"Square mode");
    }
}