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.
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.
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.
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.
If the video needs to be rotated a different direction replace the number in the transpose= parameter to one of the following:
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
quickly get the tool installed.
Stackflow question:
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
This will make sure that the video is rotated the same way in VLC and QuickTime
Here's the documentation on the
(from http://ffmpeg.org/ffmpeg.html):
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:
To set the language of the first audio stream:
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 andwill 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-ExifToolwill
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 serverperforms 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
-metadataoption
(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
相关文章推荐
- What Is Bootloader And How To Unlock Bootloader On Android Phones
- An ffmpeg and SDL Tutorial or How to Write a Video Player in Less Than 1000 Lines
- ffmpeg示例一:how to use libavformat and libavcodec to read video from a file.
- How to Play, Record, and Edit Videos in iOS
- Sending a URL to Another App on Android and iOS with Delphi XE5
- How to stream video to your android phone 如何将视频远程传输至你的android手机?
- with ffmpeg to encode video for live streaming and for recording to files for on-demand playback
- An ffmpeg and SDL Tutorial or How to Write a Video Player in Less Than 1000 Lines
- [iOS/Android开发之WebService]How to write a webservices using php with json format
- Why and how to pack your textures for iOS/Android
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
- how to extract and decrypt WeChat EnMicromsg.db on Android phone
- How to get Full Rom Dump / Stock ROM of MT6572 MediaTek Smartphones using SPFT and MTK Droid Root an
- How To Backup Your Android Phone’s Boot, Recovery And System Partition Images -- RomDump
- How to Play, Record, and Edit Videos in iOS
- Mobile Development with C#:Building Native iOS, Android, and Windows Phone Applications
- How to get started with WebRTC and iOS without wasting 10 hours of your life
- How to create iOS 8 Today extension and share data with containing app – tutorial
- What Is ADB And How To Install It With Android SDK
- How to build monitoring system with Face Recognization by HTC Hero、VideoLAN and EmguCV.