Correct Smartphone Video Orientation and How To Rotate iOS and Android Videos with ffmpeg

The following has been verified on an iPhone 4, iPhone 5, and a Nexus 4. Results may differ with other smartphones.

Correct Smartphone Video Orientation

When shooting video in landscape mode be sure the bottom of the phone (i.e. where the microphone or Home Button is) is facing 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 due to interpreting the Rotation parameter that was set in the metadata when recording began. If the bottom of the phone was facing 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. Open the video file in VLC, mplayer, or even Google Chrome and the video will be displayed upside down; these programs are not interpreting theRotation parameter.

Rotating iOS and Android Videos Using ffmpeg

I am not aware of a lossless way to rotate the videos back to proper orientation. To rotate the video, it will need to be transcoded resulting in some quality loss; luckily, 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 in Fedora 18 to do this. The same should be possible with ffmpeg GUI frontends.

ffmpeg -i input.mp4 -metadata:s:v rotate="0" -vf "hflip,vflip" -sameq -acodec copy output.mp4

I have encountered instances where after transcoding 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 is in place to fix this problem and reset the Rotation parameter to 0.

If the video only needs a horizontal flip, remove the vflip parameter from above. The same applies if the video only needs a vertical flip, remove the hflip parameter from above. Remove the comma as well in either case.

I have encountered one instance 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" -sameq -acodec copy output.mp4

If the video needs to be rotated 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

An excellent, cross-platform tool to view metadata for a video file is Phil Harvey’s ExifTool. If you happen to be running Fedora, a simple yum install perl-Image-ExifTool will quickly get the tool installed.


Stackflow question:

video captured from iphone gets rotated when converted to .mp4 using ffmpeg


Q:When I try to upload videos captured from my iPhone in my app, the server performs a conversion from .mov to .mp4 so that it can be played in other platforms. However the problem is that when I shoot the video (in portrait orientation) and it is converted (using ffmpeg) and then played back from the server, it appears to be rotated. Any idea?

A:

What you can also do is remove the QuickTime specific metadata when rotate the .mov. This will make sure that the video is rotated the same way in VLC and QuickTime

ffmpeg -i in.mov -vf "transpose=1" -metadata:s:v:0 rotate=0 out.mov

Here's the documentation on the -metadata option (from http://ffmpeg.org/ffmpeg.html):

-metadata[:metadata_specifier] key=value (output,per-metadata)

Set a metadata key/value pair.

An optional metadata_specifier may be given to set metadata on streams or chapters. See -map_metadata documentation for details.

This option overrides metadata set with -map_metadata. It is also possible to delete metadata by using an empty value.

For example, for setting the title in the output file:

 ffmpeg -i in.avi -metadata title="my title" out.flv 

To set the language of the first audio stream:

 ffmpeg -i INPUT -metadata:s:a:1 language=eng OUTPUT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值