gpt4 book ai didi

ios - 剪辑视频时如何检测视频方向?

转载 作者:行者123 更新时间:2023-11-29 10:40:52 24 4
gpt4 key购买 nike

当我以横向模式录制视频时,我第二次遇到错误的视频方向。对于第一次录制,我在 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 进行修剪。然后修剪后的视频在 MPMoviePlayer 中以错误的方向播放。如何解决这个问题?

请指导我。

最佳答案

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

关于ios - 剪辑视频时如何检测视频方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com