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

10

26

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?

De answered 23/2, 2012 at 7:2 Comment(0)
H
10

Depending on which version of ffmpeg you have and how it's compiled, one of the following should work...

ffmpeg -vf "transpose=1" -i input.mov output.mp4

...or...

ffmpeg -vfilters "rotate=90" -i input.mov output.mp4
Hiatus answered 23/2, 2012 at 7:56 Comment(6)
For me, only ffmpeg -i input.mov -vf "transpose=1" output.mp4 workedEsperanto
@DanielH. you need a special plugin to use rotate=90, but you can get all the 90 degree rotations with transpose 0, 1, 2, 3 and you can also use "hflip", "vflip", or even "hflip,vflip" for every other case.Film
But how to create the ffmpeg class .and Where i create ffmpeg class.I am new in ffmpeg library.I have .a files of all and .h file of all(avcodec.h,avformat.h & etc).This all files are import in my project.So where write the above code....please help me.Lori
Be sure to add -map_metadata 0:g to preserve metadata (especially creation date)Tayyebeb
Unrecognized option 'vfilters'Undersized
@Undersized - Take note that this question is VERY old, and newer versions of FFmpeg use -vf instead of -vfilters.Pedate
E
41

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.

Enchilada answered 17/6, 2015 at 18:11 Comment(10)
Thanks for the tip. This really killed my setup when I upgraded.Peri
Same here. I was on my way to file a bug report when I discovered it was actually a feature.Enchilada
This is the correct answer at this point - ffmpeg will autorotate iphone video.Rissa
It's a best answerReadily
And ffmpeg will rotate not IPhone video only.Readily
Hi Albin I am converting video file in ios app and then uploading it to server, But every time i got rotated video. Can you please suggest ?Numbskull
@ChanWarde: I can't help you with your specific case. The information to solve your problem is here but you have to apply it on your own. Make sure your code is for the right version of ffmpeg (ffmpeg -version to find out)Enchilada
Re: "This will work in both old releases and 2.7", this is not true for all definitions of "old releases". I'm not sure when this was first allowed (yet not implemented), but in a 2012 vintage build of FFmpeg I get Unrecognized option 'noautorotate' Failed to set value '-i' for option 'noautorotate'Appeasement
@Lambart: "2012 vintage build" haha. I've updated to answer to clarify. Obviously they can't be expected to travel through time and add flags to past releases and/or maintain ancient unsupported branches.Enchilada
@Enchilada this saved the day for me! :)Primero
O
22

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.

Osteoarthritis answered 23/2, 2012 at 20:39 Comment(0)
F
19

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
Faggot answered 18/12, 2012 at 8:17 Comment(3)
I had a problem, where orientation metadata wasn't set on the remote server while it was when testing locally under osx. this worked fine locally, but failed on the dev server: "ffmpeg -i input.mov -metadata rotate=0 -codec copy -y output.mov" What I had to add to get it working on the dev server was the metadata specifier s:v:0 "ffmpeg -i input.mov -metadata:s:v:0 rotate=0 -codec copy -y otput.mov"Sling
I was wondering why flickr kept rotating my videos, while mplayer on my desktop was working fine. Turns out my phone had the wrong idea about which way I was holding it. Thanks.Viafore
I use this solution. But all my rotated videos are cropped to 3 seconds. They are longer (near 5 sec.)Ftc
H
10

Depending on which version of ffmpeg you have and how it's compiled, one of the following should work...

ffmpeg -vf "transpose=1" -i input.mov output.mp4

...or...

ffmpeg -vfilters "rotate=90" -i input.mov output.mp4
Hiatus answered 23/2, 2012 at 7:56 Comment(6)
For me, only ffmpeg -i input.mov -vf "transpose=1" output.mp4 workedEsperanto
@DanielH. you need a special plugin to use rotate=90, but you can get all the 90 degree rotations with transpose 0, 1, 2, 3 and you can also use "hflip", "vflip", or even "hflip,vflip" for every other case.Film
But how to create the ffmpeg class .and Where i create ffmpeg class.I am new in ffmpeg library.I have .a files of all and .h file of all(avcodec.h,avformat.h & etc).This all files are import in my project.So where write the above code....please help me.Lori
Be sure to add -map_metadata 0:g to preserve metadata (especially creation date)Tayyebeb
Unrecognized option 'vfilters'Undersized
@Undersized - Take note that this question is VERY old, and newer versions of FFmpeg use -vf instead of -vfilters.Pedate
K
6

Use the vflip filter

ffmpeg -i input.mov -vf "vflip" output.mp4

Rotate did not work for me and transpose=1 was rotating 90 degrees

Kit answered 8/12, 2012 at 21:59 Comment(0)
F
5

So - I too ran into this issue, and here my $0.02 on it:

1.) some videos DO have Orientation/Rotation metadata, some don't: MTS (sony AVHCD) or the AVIs I have - DO NOT have an orientation tag. MOVs and MP4s (ipad/iphone or samsung galaxy note2) DO HAVE it.

you can check the setting via 'exiftool -Rotation file'.
My videos often have 90 or 180 as the rotation.

2.) ffmpeg - regardless of the man-page with the metadata-tag, just doesn't EVER seem to set it in the output file. - the rotation-tag is ALWAYS '0'. it correctly reports it in the output - but it's never set right to be reported by exiftool. - But hey - at least it's there and always 0.

3.) rotation angles: if you want rotate +/- 90: transpose=1 for clockwise 90, 2 ccw now if you need 180 degree - just add this filter TWICE. remember - it's a filter-chain you specify. :-) - see further down.

4.) rotate then scale: this is tricky - because you quickly get into MP4 output format violations. Let's say you have a 1920x1080 MOV. rotate by 90 gives 1080x1920 then we rescale to -1:720 -> 1080*(720/1920) = 405 horiz And 405 horizontal is NOT divisable by 2 - ERROR. fix this manually. FIXING THIS automatically - requires a bit of shell-script work.

5.) scale then rotate: you could do it this way - but then you end up with 720x1280. yuck. But the filter-example here would be: "-vf yadif=1,scale=-1:720,transpose=1" It's just not what I want - but could work quite OK.

Putting it all together: - NOTE - 'intentionally WRONG Rotation-tag', just to demonstrate - it won't show up AT ALL in the output ! This will take the input - and rotate it by 180 degree, THEN RESCALE IT - resetting the rotation-tag. - typically iphone/ipad2 can create 180deg rotated material. you just can leave '-metadata Rotation=x' out the line...

/usr/bin/ffmpeg -i input-movie.mov -timestamp 2012-06-23 08:58:10 -map_metadata 0:0 -metadata Rotation=270 -sws_flags lanczos -vcodec libx264 -x264opts me=umh -b 2600k -vf yadif=1,transpose=1,transpose=1,scale=1280:720 -f mp4 -y output-movie.MP4

I have multiple devices - like a settop box, ipad2, note2, and I convert ALL my input-material (regardless whether it's mp4,mov,MTS,AVI) to 720p mp4, and till now ALL the resulting videos play correct (orientation,sound) on every dev.

Hope it helps.

Falange answered 24/11, 2013 at 9:59 Comment(2)
This has been really helpful. Thanks for listing all the cases and how you solved them. It hadn't occurred to me that scale resets the rotate values, and that I could simply do another transpose after any other filters to get back to the orientation I needed. Great response.Guido
scale:-2:720 makes sure the calculated width is dividable by 2 and therefore conforms to MP4Hatten
F
0

For including into web pages my portrait-format videos from iPhone, I just discovered the following recipe for getting .mp4 files in portrait display.

Step 1: In QuickTime Player, Export your file to 480p (I assume that 720p or 1080p would work as well). You get a .mov file again.

Step 2: Take the new file in QT Player, and export to “iPad, iPhone…”. You get a .m4v file.

Step 3: I’m using Miro Video Converter, but probably any readily-available converter at all will work, to get your .mp4 file.

Works like a (long-winded) charm.

Friendship answered 27/7, 2015 at 22:40 Comment(1)
Those are desktop apps. The question was about ffmpeg, which is a command line unix application, that can be scripted and used for online video conversion etc.Enchilada
P
0

I've filmed the video with Ipad3 and it was oriented upside down, which I suppose is the common situation of all Apple devices at some versions. Besides of it, the 3-minutes long MOV file (1920x1090) took about 500 Mb in size, which made it not available to share easily. I had to convert it to MP4, and analyzing all threads I've found on stackoverflow, here's the final code string for ffmpeg I've used (ffmpeg ver. 2.8.4):

ffmpeg -i IN.MOV -s 960x540 -metadata:s:v rotate="0" -acodec libmp3lame OUT.mp4

I suppose you may just leave '-metadata:s:v rotate="0"' if you don't need the resize and audio codec change. Note that if you resize the video, width and height should fully divide to 4.

Propose answered 10/4, 2017 at 21:15 Comment(0)
S
0

Although the topic is old. Hope this will help some one:

Get ffmpeg latest version : https://www.ffmpeg.org/download.html

The command that worked for me (to flip 180 degrees):

ffmpeg -noautorotate -i input.mp4 -filter:v "rotate=PI" output.mp4

When the degrees are determined by -filter:v "PI/180*degrees"

for example -filter:v "45*PI/180" for 45 degrees

A nice explanation is here https://superuser.com/questions/578321/how-to-rotate-a-video-180-with-ffmpeg

Severally answered 5/4, 2018 at 12:58 Comment(0)
E
0

Or... to simply change the tag in an existing file:

Read the current rotation

exiftool -Rotation <file>

then, for example:

exiftool -Rotation=180 <file>

to set it to 180

Effieeffigy answered 25/2, 2020 at 14:28 Comment(1)
Doesn't look like exiftool edits directly. It uses temp file end editint 32MB file on SSD takes ages.Dextrorse

© 2022 - 2024 — McMap. All rights reserved.