Video files produced on camera phones are always saved out as landscape. So how does a mobile phone know how to rotate the video back to portrait if that is how it was originally shot? Most original media files carry with them a collection of metadata fields that describe the content in various ways, such as encoding, dimensions, duration, etc. so that a media player can reproduce the content with as little effort and highest fidelity possible. A smartphone will write to and read from one of these fields so that it can make itself aware of the intended orientation of the video file during playback at a later time. In particular, it will write to a field called Rotation, whose value will be in degrees, not the binary landscape or portrait as one might expect.

There are a number of ways to inspect the metadata attached to media files. If like us you need to perform this task programmatically then you have the option of either executing some command line statements and listening to the output on stdout or using one of the many libraries available that implement ffmpeg by providing some interface between the grubby command line and your program – as we are using Node we chose fluent-ffmpeg for its convenient method ffprobe. There are other methods of discovering the value of Rotation, however, so take a look below where we offer some examples.

fluent-ffmpeg

var ffmpeg = require('fluent-ffmpeg'),
    pathToVideo = 'video.file';

ffmpeg.ffprobe(pathToVideo, function(err, data) {
    if(err) { return; }

    if(data.streams.length && data.streams[0].tags) {
        switch(data.streams[0].tags.rotate) {
            case '90':
                // Rotate by 90 degrees clockwise on playback
                break;
            // ...
        }
    }
}

ExifTool

Download the ExifTool executable from the program’s homepage. To pull the Rotation metadata tag from a video file, we use the -*TAG* flag, like so:

$ exiftool -Rotation "C:/pathToVideo/video.file"

And if this tag exists in the metadata the program will print the following (whitespace and all):

Rotation                        : 90

Using JavaScript we can obtain the value by performing a quick regular expression operation to remove all non-numeric characters:

var value = /* captured ExifTool output */;
var rotation = value.replace(/[^d.]/g, ''); // = '90'

MediaInfo

MediaInfo provides a GUI for viewing the metadata in media files. Download it here.

Drag or open the video file you want to inspect into MediaInfo, select View -> Tree, and you will be given a structured view of the metadata. Under Video you will find the Rotation property:

mediainfo

– Sam