您的位置:首页 > 移动开发 > IOS开发

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

2013-09-04 00:04 766 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FFMPEG 方向 Orientation
相关文章推荐