如何检测视频文件是以纵向方式录制还是以横向方式录制在iOS中 [英] How to detect if a video file was recorded in portrait orientation, or landscape in iOS

查看:560
本文介绍了如何检测视频文件是以纵向方式录制还是以横向方式录制在iOS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AlAssetsGroup enumerateAssetsAtIndexes 列出照片(相机)应用中的资源。对于给定的视频资产,我想确定它是以纵向还是横向模式拍摄的。

I am using AlAssetsGroup enumerateAssetsAtIndexes to list the assets in the Photos (Camera) app. For a given video asset I want to determine whether it was shot in portrait or landscape mode.

在以下代码中,资产是 AlAsset ,我已经测试过它是否是视频资产 [asset valueForProperty:ALAssetPropertyType] AlAssetTypeVideo ,然后:

In the following code, asset is an AlAsset and I have tested to see if it is a video asset [asset valueForProperty:ALAssetPropertyType] is AlAssetTypeVideo, then:

int orientation = [[asset valueForProperty:ALAssetPropertyOrientation] intValue];

在这种情况下,方向始终为0是 ALAssetOrientationUp 。也许这是可以预料的,所有视频都是正确的,但是纵向视频在MPEG-4中表示为90度的风景视频(即所有视频实际上都是风景,如果不是,请在Mac上尝试MediaInfo应用程序相信我)。

In this case orientation is always 0 which is ALAssetOrientationUp. Maybe this is to be expected, all videos are up right, but a portrait video is represented in MPEG-4 as a landscape video turned 90 degrees (i.e. all videos are actually landscape, try the MediaInfo app on the mac if you don't believe me).

文件中的位置和/或如何在纵向握住手机时访问告诉我实际记录的信息?

Where within the file and/or how do I access the information that tells me it was actually recorded while holding the phone in portrait orientation?

考虑到资产的网址,我也试过这个:

I have also tried this, given the url of the asset:

AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
CGSize size = [avAsset naturalSize];
NSLog(@"size.width = %f size.height = %f", size.width, size.height);
CGAffineTransform txf = [avAsset preferredTransform];
NSLog(@"txf.a = %f txf.b = %f  txf.c = %f  txf.d = %f  txf.tx = %f  txf.ty = %f",
            txf.a, txf.b, txf.c, txf.d, txf.tx, txf.ty);

总是产生宽度>高度因此对于iPhone 4,宽度= 1280高度= 720和变换a和d值 1.0 ,其他值 0.0 ,无论捕获方向如何。

Which always yields a width > height so for iPhone 4, width=1280 height=720 and the transform a and d values are 1.0, the others are 0.0, regardless of the capture orientation.

我在Mac上使用MediaInfo应用程序查看了元数据,我做了一个Hexdump,到目前为止还没有发现横向和纵向视频之间有任何区别。但QuickTime可以垂直识别和显示肖像视频,如果您在播放时以横向方向握住手机并且如果以纵向方式握住手机正确显示,则手机会通过旋转纵向视频来了解。

I have looked at the meta data using MediaInfo app on the Mac, I have done a Hexdump and so far have not found any difference between a landscape and portrait video. But QuickTime knows and displays portrait videos vertically, and the phone knows by rotating a portrait video if you are holding the phone in landscape orientation on playback and correctly displaying it if holding it in portrait.

BTW我不能使用 ffmpeg (不能使用许可证限制)。是否有iPhone SDK原生方式来执行此操作?

BTW I can't use ffmpeg (can't live with the license restrictions). Is there an iPhone SDK native way to do this?

推荐答案

苹果开发论坛上有人建议进行视频轨道的转换,这样做了。您可以从下面的日志中看到,对于这些方向,结果是有意义的,我们的Web开发人员现在能够旋转各种视频,以便它们全部匹配并将它们合成为一个视频。

Somebody on apple dev forums suggested getting the transform of the video track, this does the job. You can see from the logs below that for these orientations the results make sense and our web developer is now able to rotate a variety of vids so they all match and composite them into one video.

AVAssetTrack* videoTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize size = [videoTrack naturalSize];
NSLog(@"size.width = %f size.height = %f", size.width, size.height);
CGAffineTransform txf = [videoTrack preferredTransform];
NSLog(@"txf.a = %f txf.b = %f txf.c = %f txf.d = %f txf.tx = %f txf.ty = %f", txf.a, txf.b, txf.c, txf.d, txf.tx, txf.ty);

使用普通凸轮使用4个iPhone 4视频记录:
(1)landscape cam on右侧(左侧主页按钮)
(2)横向左侧
(3)纵向倒置
(4)纵向右上角(主页按钮位于底部)

Logs using 4 iPhone 4 videos with the normal cam: (1) landscape cam on right side (home button on left) (2) landscape left (3) portrait upside-down (4) portrait up-right (home button at bottom)



  1. 2011-01-07 20:07:30.024 MySecretApp [1442:307] size.width =
    1280.000000 size.height = 720.000000 2011-01-07 20:07:30.027
    MySecretApp [1442:307] txf.a =
    -1.000000 txf.b = 0.000000 txf.c = 0.000000 txf.d = -1.000000 txf.tx = 1280.000000 txf.ty = 720.000000

  1. 2011-01-07 20:07:30.024 MySecretApp[1442:307] size.width = 1280.000000 size.height = 720.000000 2011-01-07 20:07:30.027 MySecretApp[1442:307] txf.a = -1.000000 txf.b = 0.000000 txf.c = 0.000000 txf.d = -1.000000 txf.tx = 1280.000000 txf.ty = 720.000000

2011-01-07 20:07:45.052 MySecretApp [1442:307] size.width =
1280.000000 size.height = 720.000000 2011-01-07 20:07:45.056
MySecretApp [1442:307] txf.a = 1.000000
txf.b = 0.000000 txf.c = 0.000000

txf.d = 1.000000 txf.tx = 0.000000

txf.ty = 0.000000

2011-01-07 20:07:45.052 MySecretApp[1442:307] size.width = 1280.000000 size.height = 720.000000 2011-01-07 20:07:45.056 MySecretApp[1442:307] txf.a = 1.000000 txf.b = 0.000000 txf.c = 0.000000
txf.d = 1.000000 txf.tx = 0.000000
txf.ty = 0.000000

2011-01 -07 20:07:53.763 MySecretApp [1442:307] size.width =
1280.000000 si ze.height = 720.000000 2011-01-07 20:07:53.766
MySecretApp [1442:307] txf.a = 0.000000
txf.b = -1.000000 txf.c = 1.000000

txf.d = 0.000000 txf.tx = 0.000000
txf.ty = 1280.000000

2011-01-07 20:07:53.763 MySecretApp[1442:307] size.width = 1280.000000 size.height = 720.000000 2011-01-07 20:07:53.766 MySecretApp[1442:307] txf.a = 0.000000 txf.b = -1.000000 txf.c = 1.000000
txf.d = 0.000000 txf.tx = 0.000000 txf.ty = 1280.000000

2011-01-07 20:08:03.490 MySecretApp [1442:307] size.width =
1280.000000 size.height = 720.000000 2011-01-07 20:08:03.493
MySecretApp [1442:307] txf.a = 0.000000
txf。 b = 1.000000 txf.c = -1.000000

txf.d = 0.000000 txf.tx = 720.000000
txf.ty = 0.000000

2011-01-07 20:08:03.490 MySecretApp[1442:307] size.width = 1280.000000 size.height = 720.000000 2011-01-07 20:08:03.493 MySecretApp[1442:307] txf.a = 0.000000 txf.b = 1.000000 txf.c = -1.000000
txf.d = 0.000000 txf.tx = 720.000000 txf.ty = 0.000000


这篇关于如何检测视频文件是以纵向方式录制还是以横向方式录制在iOS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆