Iphone 如何从视频中提取方向信息?
在网上浏览了大量文档后,iPhone似乎总是以480x360的高宽比拍摄视频,并在视频轨道上应用变换矩阵。(480x360可能会改变,但对于给定的设备,它总是相同的) 下面是一种在iOS项目中修改ffmpeg源并访问矩阵的方法 下面是一种在iOS-4中查找转换矩阵的更简洁的方法 如何在以下任一选项中提取视频的方向-Iphone 如何从视频中提取方向信息?,iphone,ruby-on-rails,ruby,iphone-sdk-3.0,ffmpeg,Iphone,Ruby On Rails,Ruby,Iphone Sdk 3.0,Ffmpeg,在网上浏览了大量文档后,iPhone似乎总是以480x360的高宽比拍摄视频,并在视频轨道上应用变换矩阵。(480x360可能会改变,但对于给定的设备,它总是相同的) 下面是一种在iOS项目中修改ffmpeg源并访问矩阵的方法 下面是一种在iOS-4中查找转换矩阵的更简洁的方法 如何在以下任一选项中提取视频的方向- -iOS 3.2 -ffmpeg(通过命令行服务器端) -红宝石 任何帮助都将不胜感激。我在iOS上使用AVAssetExportSession、AVMutableComposit
-iOS 3.2
-ffmpeg(通过命令行服务器端)
-红宝石
任何帮助都将不胜感激。我在iOS上使用AVAssetExportSession、AVMutableComposition和输入AVAssetTrack的preferredTransform进行了提取。我将首选变换与一个变换连接起来,以填充目标大小
导出到文件后,我使用ASIHTTPRequest将数据上传到rails服务器,并使用回形针将数据发送到Amazon S3。根据我目前的发现,ffmpeg无法检测iPhone的方向。但是,开源库可以。命令行示例:
$ mediainfo test.mp4 | grep Rotation
Rotation : 90°
来自同一iphone视频的更多示例输出:
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
由于大多数相机将其旋转/方向存储在exif元数据中,因此我建议使用和一个名为gem的ruby包装器,它是主动维护的 安装exiftool:
apt get exiftool | | brew安装exiftool | |端口安装exiftool
或者使用任何可用的package manager
安装mini_exiftool:
gem安装mini\u exiftool
试试看:
irb>
require 'mini_exiftool'
movie = MiniExiftool.new('test_movie.mov')
movie.orientation #=> 90
cheersffmpeg使用.mov文件的旋转值报告元数据:
ffmpeg -i myrotatedMOV.mov
在我的应用程序中,我使用正则表达式(即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<rotation>.*)')
match_rotation = reo_rotation.search(stderr)
rotation = match_rotation.groups()[0]
导入子流程,重新
cmd='ffmpeg-i%s'%pathtofile
p=子进程Popen(
cmd.split(“”),
stderr=子流程.PIPE,
关闭\u fds=真
)
stdout,stderr=p.communicate()
reo_rotation=re.compile('rotate\s+:\s(?P.*))
match_rotation=reo_rotation.search(stderr)
旋转=匹配旋转。组()[0]
我还没有尝试过很多视频,只有几段.mov是用ffmpeg版本1.0从iphone5录制的。但到目前为止还不错 类似于@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(或其他输出格式)。您可以使用
ffprobe
。无需任何grep
,或任何其他附加过程,或任何正则表达式操作来解析输出,如其他答案所示
如果需要旋转元数据:
命令:
ffprobe -loglevel error -select_streams v:0 -show_entries stream_tags=rotate -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=rotation -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=displaymatrix -of default=nw=1:nk=1 input.mp4
示例输出:
90
-90
00000000: 0 65536 0
00000001: -65536 0 0
00000002: 15728640 0 1073741824
如果需要显示矩阵旋转侧数据:
命令:
ffprobe -loglevel error -select_streams v:0 -show_entries stream_tags=rotate -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=rotation -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=displaymatrix -of default=nw=1:nk=1 input.mp4
示例输出:
90
-90
00000000: 0 65536 0
00000001: -65536 0 0
00000002: 15728640 0 1073741824
如果需要显示矩阵:
命令:
ffprobe -loglevel error -select_streams v:0 -show_entries stream_tags=rotate -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=rotation -of default=nw=1:nk=1 input.mp4
ffprobe -loglevel error -select_streams v:0 -show_entries side_data=displaymatrix -of default=nw=1:nk=1 input.mp4
示例输出:
90
-90
00000000: 0 65536 0
00000001: -65536 0 0
00000002: 15728640 0 1073741824
选择意味着什么
从输出中忽略标题和其他信息-日志级别错误
仅处理第一个视频流,忽略所有其他内容。如果您的输入包含多个视频流,并且您只需要一个视频流中的信息,则此选项非常有用-选择_streams v:0
选择从视频流输出-show_entries stream_tags=rotate
标记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以提取此信息,感谢您的回答!我将mini_exiftool的答案标记为正确,因为它是通过gem集成的。mediainfo似乎也是合法的。哈在查找iOS视频元数据时遇到了这个问题@enobrevTo写入变量:ROTATION=“$(mediainfo--Inform=“Video;%ROTATION%”Video.mp4)“echo$ROTATION标记正确,用于建议gem版本和添加引用。谢谢exiftool版本10.05现在给了我movie.rotation,因为我不能对enobrev的回答发表评论。FFMPEG现在可以返回方向:你可以。嗨@llgoan,我试着按照你的答案使用ffprobe提取运动矢量。你能帮我吗?