Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 571
aqs Avatar asked Feb 23 '12 07:02

aqs


2 Answers

FFMPEG changed the default behavior to auto rotate video sources with rotation metadata in 2015. This was released as v2.7.

If your ffmpeg version is v2.7 or newer, but your rotation metadata isn't respected, the problem is likely that you are using custom rotation based on metadata. This will cause the same logic to be applied twice, changing or cancelling out the rotation.

In addition to removing your custom rotation (recommended), there's an option to turn off auto rotation with -noautorotate.

ffmpeg -noautorotate -i input.mp4...

This will also work in some older releases.

like image 127
Albin Avatar answered Nov 06 '22 02:11

Albin


For sake of completeness, the reason this is happening is that iPhones only actually capture video in one fixed orientation. The measured orientation is then recorded in Apple-specific metadata.

The effect is that Quicktime Player reads the metadata and rotates the video to the correct orientation during playback, but other software (e.g., VLC) does not and shows it as oriented in the actual codec data.

This is why rotate=90 (or vflip, or transpose, or etc.) will work for some people, but not others. Depending on how the camera is held during recording, the rotation necessary could be 90, 180, or even 270 degrees. Without reading the metadata, you're just guessing at how much rotation is necessary and the change that fixes one video will fail for another.

like image 44
blahdiblah Avatar answered Nov 06 '22 01:11

blahdiblah