How to extract orientation information from videos?

Since most Cameras store their rotation/orientation within the exif-metadata, i would suggest using exifttool and the a ruby wrapper gem called mini_exiftool which is actively maintained.

Install exiftool:

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

or use what ever package manager is available

Install mini_exiftool:

gem install mini_exiftool

Try it:

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

cheers


From what I've found thus far, ffmpeg doesn't have the ability to detect iPhone's orientation. But, the open source library, mediainfo can. A command line example:

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

More example output from the same iphone video:

Video
ID                               : 1
Format                           : AVC
Format/Info                      : Advanced Video Codec
Format profile                   : [email protected]
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

You can use ffprobe. No need for any grep, or any other additional processes, or any regex operations to parse the output as shown in other answers.

If you want the rotate metadata:

Command:

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

Example output:

90

If you want the display matrix rotation side data:

Command:

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

Example output:

-90

If you want the display matrix:

Command:

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

Example output:

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

What the options mean

  • -loglevel error Omit the header and other info from output.

  • -select_streams v:0 Only process the first video stream and ignore everything else. Useful if your input contains multiple video streams and you only want info from one.

  • -show_entries stream_tags=rotate Chooses to output the rotate tag from the video stream.

  • -of default=nw=1:nk=1 Use default output format, but omit including the section header/footer wrappers and each field key.

Output format

The output from ffprobe can be formatted in several ways. For example, JSON:

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

                }
            ]
        }
    ]