6

I'm messing with h264 videos loaded with FFmpeg on the iPhone 3GS. The problem is any videos recorded in "Portrait" orientation have a transformation matrix applied to them causing them to display rotated 90 degrees counter-clock.

From what I understand thus far, I just need to modify the transform matrix in the 'tkhd' atom. The problem is I am having trouble accessing or modifying this data. I checked out the FFmpeg implementation for:

static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)

which clearly shows how the matrix is accessed in avformat but when I try to access the header bytes using the same functions I am not getting any rational values. Even if I were to successfully pull the matrix I'm not sure how to replace it? FFmpeg has functions for retrieving and appending to the track header but nothing for replace it seems?

Any help would be greatly appreciated.

Thanks, Matt.

Matthew McGoogan
  • 6,805
  • 3
  • 18
  • 13
  • is this a C++ call that will give you the Matrix information ? Do you have examples of using this ? I know how the Matrix information does the rotation I just need to know a simple way of getting that matrix information. I am hoping this will translate to a windows implementation.... Thanks in advance – The Lazy Coder Oct 11 '11 at 21:52

4 Answers4

13

To those who cannot get the v filter option to work, after some searching I found an alternative option which works for my build:

ffmpeg -i in.avi -vf "transpose=1" out.avi

this flips the video 90 degrees clockwise. hope this helps all those on ubuntu struggling like I was!! :)

James McMahon
  • 48,506
  • 64
  • 207
  • 283
Alex K
  • 131
  • 1
  • 4
4

I just had a patch accepted on the FFMPEG git master branch which should help all of us in adjusting for the iPhone's orientation and subsequent transform applied to the .mov file it sends out. FFMPEG will now send back in metadata:

'rotate'=90,180,or 270 depending on how many degrees you need to turn the image clockwise to get the true rotation.

Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57
  • could you add a link to your patch? – sghael Mar 18 '12 at 16:42
  • 2
    http://git.videolan.org/?p=ffmpeg.git;a=commit;h=62d2a75b024bf72e6f3648e33c5bb5baf9018358 – Piotr Tomasik May 07 '12 at 00:28
  • Does that change also affect the meta information in the video that tells players how to rotate/transform the video during playback. You can rotate a video 180 degrees by flipping it horizontally and vertically but that just switches which players display the video correctly or incorrectly. Players that don't honor the transformation (e.g., Windows Media Player) then show the video correctly, but since the video appears to still have the transformation embedded in it, QuickTime Player now plays the newly encoded video upside down. To fix that, you would also have to use -map_metadata -1. – spaaarky21 Apr 03 '14 at 16:42
2

Portrait video recorded in iPhone 3GS is strangely rotated in landscape, and only quicktime plays it correctly, players like VLC etc play the portrait mode videos in landscape because it is encoded that way! Maybe apple have set some bit so that quicktime identifies and sets the orientation properly.

You can use -vfilters option of ffmpeg to rotate the video. It is available in version r21242. You have to patch it on your version of ffmpeg.

configure the build by using --enable-vfilters

and you can use the option:

ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4

to rotate the input movie.

Raj Pawan Gumdal
  • 7,390
  • 10
  • 60
  • 92
  • 1
    Thanks Raj. I'm doing development with the FFmpeg library though so using ffmpeg as a command line program is not an option. I have to find a way using their source library. – Matthew McGoogan Feb 25 '10 at 16:06
  • Matthew, @"I'm doing development with the FFmpeg library" I know and the purpose of this post is the same! You create command line arguments with the options as shown above and pass it to ffmpeg main() method. The rest will be done by ffmpeg itself. You have to include ffmpeg.c file into your project and also static libraries which is created by using appropriate configuration command, which I hope is already done. – Raj Pawan Gumdal Feb 26 '10 at 03:54
2

the movie is recorded directly with the orientation of the hardware camera. when you turn it the camera still records with the same orientation and the same straight write to file.

What determines the orientation of the video is the Transform matrix

The matrix is set dependent on the iphone's orientation. And this is what will determine if you have to rotate the video. And that rotation could be 90 degrees and it could be 180 degrees. It depends on the phone orientation.

After you get an idea of what the matrix is and how it relates to the image you will be able to determine the orientation of the video.

The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
  • edit: my first comment back was under the assumption this was my question. The information I provided here was related to my understanding of the transform matrix, as well as where to get the information in the file. Since he was asking about it, I figured this post would be pertinent. Apology for my previous comment/edit as again I thought this was my question. Thanks :) – The Lazy Coder Apr 03 '14 at 17:45