Usa*_*ama 7 video objective-c mpmovieplayercontroller uiinterfaceorientation ios
我正在尝试实现Instagram视频裁剪功能.为此,我需要获得正确的视频方向,我将以MPMoviePlayerController正确的宽高比布局我的视图,然后用户将能够平移视频并显示方形视频将在动作中被裁剪.
我在获取正确的视频方向时遇到问题.我正在使用以下代码
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
UIInterfaceOrientation orientation =[self orientationForTrack:track];
// method to get orientation
- (UIInterfaceOrientation)orientationForTrack:(AVAssetTrack *)videoTrack
{
CGSize size = [videoTrack naturalSize];
CGAffineTransform txf = [videoTrack preferredTransform];
if (size.width == txf.tx && size.height == txf.ty)
return UIInterfaceOrientationLandscapeRight;
else if (txf.tx == 0 && txf.ty == 0)
return UIInterfaceOrientationLandscapeLeft;
else if (txf.tx == 0 && txf.ty == size.width)
return UIInterfaceOrientationPortraitUpsideDown;
else
return UIInterfaceOrientationUnknown;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,有时候实际上是纵向的视频,上面的代码片段将其分类为横向,反之亦然.我只需要了解视频方向的两种状态(横向或纵向).我想要完全相同的方向值,这MPMoviePlayerController是解释.我的应用程序仅支持纵向方向.任何帮助将受到高度赞赏.
PS:以上代码适用于从iphone相机捕获的视频,通过whatsapp下载或共享的视频导致问题.
AVAssetTrack 有财产preferredTransform。您可以使用它来查找视频的实际方向。
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp;
CGFloat videoAngle = RadiansToDegrees(atan2(videoAssetOrientation_.b, videoAssetOrientation_.a));
int orientation = 0;
switch ((int)videoAngle) {
case 0:
orientation = UIImageOrientationRight;
break;
case 90:
orientation = UIImageOrientationUp;
break;
case 180:
orientation = UIImageOrientationLeft;
break;
case -90:
orientation = UIImageOrientationDown;
break;
default:
//Not found
break;
}
Run Code Online (Sandbox Code Playgroud)
解决这个问题的唯一方法是使用以下方法从视频中捕获缩略图:
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option
Run Code Online (Sandbox Code Playgroud)
然后添加对缩略图图像的检查,如:
if (singleFrameImage.size.height > singleFrameImage.size.width) {
// potrait video
} else {
// landscape video
}
Run Code Online (Sandbox Code Playgroud)
该解决方案在从iPhone相机拍摄的视频上以及从互联网上随机下载的一组(8-10)视频上进行测试.