如何从video中提取方向信息?

浏览网上的大量文档后,似乎iPhone总是以480×360的宽高比拍摄video,并在video轨道上应用变换矩阵。 (480×360可能会改变,但对于给定的设备,它总是相同的)

这是一种在iOS项目中修改ffmpeg源并访问矩阵的方法http://www.seqoy.com/correct-orientation-for-iphone-recorded-movies-with-ffmpeg/

以下是在iOS-4中查找转换矩阵的更简洁方法如果以纵向或横向方式记录video文件,如何检测(iPhone SDK)。

如何在以下任一选项中提取video的方向 –
– iOS 3.2
– ffmpeg(通过命令行服务器端)
– ruby

任何帮助将不胜感激。

由于大多数相机将它们的旋转/方向存储在exif元数据中,我建议使用exifttool和一个名为mini_exiftool的ruby包装gem,它是积极维护的。

安装exiftool:

apt-get exiftool || brew install exiftool || port install exiftool

或者使用包管理器可用的东西

安装mini_exiftool:

gem install mini_exiftool

试试吧:

 irb> require 'mini_exiftool' movie = MiniExiftool.new('test_movie.mov') movie.orientation #=> 90 

干杯

从我迄今为止发现的情况来看,ffmpeg无法检测iPhone的方向。 但是,开源库, mediainfo可以。 命令行示例:

 $ mediainfo test.mp4 | grep Rotation Rotation : 90° 

来自同一iphonevideo的更多示例输出:

 Video ID : 1 Format : AVC Format/Info : Advanced Video Codec Format profile : Baseline@L3.0 Format settings, CABAC : No Format settings, ReFrames : 1 frame Codec ID : avc1 Codec ID/Info : Advanced Video Coding Duration : 7s 941ms Bit rate mode : Variable Bit rate : 724 Kbps Width : 480 pixels Height : 360 pixels Display aspect ratio : 4:3 Rotation : 90° Frame rate mode : Variable Frame rate : 29.970 fps Minimum frame rate : 28.571 fps Maximum frame rate : 31.579 fps Color space : YUV Chroma subsampling : 4:2:0 Bit depth : 8 bits Scan type : Progressive Bits/(Pixel*Frame) : 0.140 Stream size : 702 KiB (91%) Title : Core Media Video Encoded date : UTC 2011-06-22 15:58:25 Tagged date : UTC 2011-06-22 15:58:34 Color primaries : BT.601-6 525, BT.1358 525, BT.1700 NTSC, SMPTE 170M Transfer characteristics : BT.709-5, BT.1361 Matrix coefficients : BT.601-6 525, BT.1358 525, BT.1700 NTSC, SMPTE 170M 

你可以使用ffprobe 。 不需要任何grep ,或任何其他额外的进程,或任何正则表达式操作来解析输出,如其他答案所示。

如果您想要旋转元数据:

命令:

 ffprobe -loglevel error -select_streams v:0 -show_entries stream_tags=rotate -of default=nw=1:nk=1 input.mp4 

输出示例:

 90 

如果要显示矩阵旋转侧数据:

命令:

 ffprobe -loglevel error -select_streams v:0 -show_entries side_data=rotation -of default=nw=1:nk=1 input.mp4 

输出示例:

 -90 

如果你想要显示矩阵:

命令:

 ffprobe -loglevel error -select_streams v:0 -show_entries side_data=displaymatrix -of default=nw=1:nk=1 input.mp4 

输出示例:

 00000000: 0 65536 0 00000001: -65536 0 0 00000002: 15728640 0 1073741824 

选项意味着什么

  • -loglevel error忽略输出中的标题和其他信息。

  • -select_streams v:0仅处理第一个video流并忽略其他所有内容。 如果您的输入包含多个video流并且您只想要一个信息,则非常有用。

  • -show_entries stream_tags=rotate选择从video流输出rotate标签。

  • -of default=nw=1:nk=1使用默认输出格式 ,但省略包括节头/页脚封装器和每个字段键。

输出格式

ffprobe的输出可以通过多种方式进行格式化 。 例如,JSON:

 ffprobe -loglevel error -show_entries stream_tags=rotate -of json input.mp4 { "streams": [ { "tags": { "rotate": "90" }, "side_data_list": [ { } ] } ] 

ffmpeg使用.mov文件的旋转值报告元数据:

 ffmpeg -i myrotatedMOV.mov 

….

 Duration: 00:00:14.31, start: 0.000000, bitrate: 778 kb/s Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 480x360, 702 kb/s, 29.98 fps, 30 tbr, 600 tbn, 1200 tbc Metadata: rotate : 180 creation_time : 2013-01-09 12:47:36 handler_name : Core Media Data Handler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, s16, 62 kb/s Metadata: creation_time : 2013-01-09 12:47:36 handler_name : Core Media Data Handler 

在我的应用程序中,我使用正则表达式将其拉出来,即在python中:

 import subprocess, re cmd = 'ffmpeg -i %s' % pathtofile p = subprocess.Popen( cmd.split(" "), stderr = subprocess.PIPE, close_fds=True ) stdout, stderr = p.communicate() reo_rotation = re.compile('rotate\s+:\s(?P.*)') match_rotation = reo_rotation.search(stderr) rotation = match_rotation.groups()[0] 

我没有尝试使用各种video,只有几个.movs从iphone5录制,使用ffmpeg版本1.0。 但到目前为止一切顺利。

类似于@ HdN8的答案,但没有python正则表达式:

 $ ffprobe -show_streams any.MOV 2>/dev/null | grep rotate TAG:rotate=180 

或者JSON:

 $ ffprobe -of json -show_streams IMG_8738.MOV 2>/dev/null | grep rotate "rotate": "180", 

或者您可以解析JSON(或其他输出格式)。

我在iOS上使用AVAssetExportSession,AVMutableComposition和输入AVAssetTrack的preferredTransform进行了提取。 我将首选变换与转换连接起来以填充目标大小。

导出到文件后,我使用ASIHTTPRequest上传到我的rails服务器,并使用paperclip将数据发送到Amazon S3。