Tuesday, June 4, 2013
Have you ever recorded a video with your smartphone only to view it on a computer or TV and have it displayed upside down? If so, here’s why.
The following has been verified on an iPhone 4, iPhone 5, and a Nexus 4. Results may differ with other and newer smartphones.
When shooting video in landscape mode, be sure the bottom of the phone (i.e. where the microphone or Home Button is) is pointing to your right, otherwise videos will be recorded upside down and will be displayed that way when viewing them on anything except the phone and QuickTime Player.
Videos are oriented properly when viewing them on the phone and QuickTime Player because they are reorienting the video in real time by interpreting the Rotation parameter that was set in the metadata when recording began.
If the bottom of the phone was pointing to your left when recording, the Rotation parameter would be set to 180. The phone and QuickTime Player interpret this parameter to rotate the video 180 degrees during playback. In actuality, the video file has been recorded upside down. You will notice increased CPU usage while playing the video because it is being transcoded on the fly. Open the video file in VLC, mplayer, or even Google Chrome, and the video will be displayed upside down; these programs are not interpreting the Rotation parameter.
I am not aware of a lossless way to rotate videos back to proper orientation. To rotate the video, it will need to be transcoded, resulting in some quality loss. However, in my experience, that quality loss cannot be seen by the naked eye.
If the video is upside down, a horizontal flip and vertical flip will be required to reorient the video properly. I used ffmpeg via the command line to do this.
ffmpeg -i INPUT.mp4 -metadata:s:v rotate="0" -vf "hflip,vflip" -c:v libx264 -crf 23 -acodec copy OUTPUT.mp4
I have encountered situations after transcoding where the Rotation parameter was not reset to 0: it was still set to 180 or 90. Programs that do not interpret the Rotation parameter, such as VLC or mplayer, will now display the video correctly, but programs that do interpret the Rotation parameter will now display the video incorrectly. The
-metadata:s:v rotate="0" ffmpeg command line switch has been added to the comand above to fix this problem.
If the video only needs a horizontal flip, remove the
vflip parameter from the command above:
-vf "hflip". The same applies if the video only needs a vertical flip, remove the
hflip parameter from the command above:
I have encountered one situation where I was attempting to shoot landscape, but the phone thought it was still in portrait mode resulting in a portrait video that you had to tilt your head counter-clockwise to view properly. To fix this, I used ffmpeg’s transpose video filter to rotate the video 90 degrees clockwise to make it landscape.
ffmpeg -i INPUT.mp4 -metadata:s:v rotate="0" -vf "transpose=1" -c:v libx264 -crf 23 -acodec copy OUTPUT.mp4
If the video needs to be rotated in a different direction, replace the number in the
transpose= parameter to one of the following:
0 = 90 degrees counter clockwise and vertical flip (default) 1 = 90 degrees clockwise 2 = 90 degrees counter clockwise 3 = 90 degrees clockwise and vertical flip
To view a video file’s metadata, I recommend Phil Harvey’s ExifTool. ExifTool can be installed on Windows, OS X, and most modern Linux distributions. Most modern Linux distributions will already have a package available in their package repositories. Search for ExifTool using your Linux distribution’s package manager. For example, on Fedora run
yum install perl-Image-ExifTool.