【问题标题】:How to extract orientation information from videos?如何从视频中提取方向信息?
【发布时间】:2011-03-13 05:15:06
【问题描述】:

在网上浏览了大量文档后,iPhone 似乎总是以 480x360 的纵横比拍摄视频,并在视频轨道上应用一个变换矩阵。 (480x360 可能会改变,但对于给定的设备它始终相同)

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

这是在 iOS-4 中查找转换矩阵的一种更简洁的方法 How to detect (iPhone SDK) if a video file was recorded in portrait orientation, or landscape.

如何在以下任一选项中提取视频的方向 -
- iOS 3.2
- ffmpeg(通过命令行服务器端)
- 红宝石

任何帮助将不胜感激。

【问题讨论】:

标签: iphone ruby-on-rails ruby iphone-sdk-3.0 ffmpeg


【解决方案1】:

由于大多数相机将其旋转/方向存储在 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

干杯

【讨论】:

  • 在建议 gem 版本和添加引用时标记为正确。谢谢!
  • exiftool 10.05 版现在给我movie.rotation
【解决方案2】:

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

$ 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

【讨论】:

  • 我最近还发现了一个链接,解释如何修改 ffmpeg 以提取此信息seqoy.com/…
  • 感谢您的回答!我将 mini_exiftool 答案标记为正确,因为它是通过 gem 集成的。 mediainfo 似乎也合法。
  • 哈哈,在查找 iOS 视频元数据时遇到了这个问题。 @enobrev
  • 写入变量:ROTATION="$(mediainfo --Inform="Video;%Rotation%" video.mp4)" echo $ROTATION
【解决方案3】:

您可以使用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 只处理第一个视频流,忽略其他所有内容。如果您的输入包含多个视频流并且您只需要一个视频流中的信息,这很有用。

  • -show_entries stream_tags=rotate 选择从视频流中输出rotate 标签。

  • -of default=nw=1:nk=1 使用default output format,但省略包含部分页眉/页脚包装器和每个字段键。

输出格式

ffprobe 的输出可以是formatted in several ways。例如 JSON:

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

                }
            ]
        }
    ]

【讨论】:

【解决方案4】:

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<rotation>.*)')
match_rotation = reo_rotation.search(stderr)
rotation = match_rotation.groups()[0]

我还没有尝试过使用 ffmpeg 1.0 版从 iphone5 录制的大量视频,只有几个 .mov。但到目前为止一切都很好。

【讨论】:

    【解决方案5】:

    类似于@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(或其他输出格式)。

    【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2019-07-07
      • 1970-01-01
      • 2020-05-09
      • 2012-02-15
      • 1970-01-01
      • 2013-05-09
      • 2017-07-10
      相关资源
      最近更新 更多