35

I have a video file. I open it with MediaInfo utility and I can see a video stream in this file having attribute Rotation 90 (along with other attributes such as CodecID, bitrate etc).

Now I have another video file which does not have that attribute Rotation 90, it does not have the Rotation attribute at all.

Can I use ffmpeg.exe so that it produces output file with Rotation 90 attribute added and with no other changes? I don't really want to do any transform, just want to set the Rotation attribute.

I've tried the -metadata option to no avail.

slhck
  • 30,965
  • 24
  • 125
  • 174
Alexander Kulyakhtin
  • 45,879
  • 35
  • 103
  • 155

1 Answers1

80

This works with recent FFmpeg:

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=90 output.mp4

This will stream copy the bitstreams, so no encoding is performed. Only the metadata of the first video stream (v:0) is changed here and the player will show the video in a rotated way. (Not all players will support this.)

Additional notes:

  • If you want to "physically" rotate the video, you have to use the transpose filter. Filtering will require re-encoding, so you will have to remove -c copy.

  • If you omit -c copy, and want to encode instead of only re-muxing, then ffmpeg will automatically rotate the video if there is any existing rotate metadata. You can disable this behavior with -noautorotate.

Community
  • 1
  • 1
slhck
  • 30,965
  • 24
  • 125
  • 174
  • I have compiled latest code of ffmpeg and there is no error in running this command but still video is not rotating.pls help – Mohit Chauhan Jan 08 '14 at 05:56
  • 1
    @MohitChauhan Please note that this does not actually rotate the video. It just sets the rotation flag, which causes some players to show it in a rotated way. If you have an issue with a command, please ask a question on [SU] and show the command and its full console output. – slhck Jan 08 '14 at 06:50
  • @davor While this is an Apple-exclusive feature I only know QuickTime. But then again I don't use Windows or Linux so I cannot give an authoritative answer, sorry. – slhck Jan 18 '14 at 15:24
  • 1
    @slhck This is very interesting. Any suggestion for players that actually support this? I am currently trying VLC and XBMC, and none seem to support this. Tried with MKV/MOV/MP4 containers. WMP12 and QuickTime do support this. – Davor Josipovic Jan 18 '14 at 15:25
  • @MohitChauhan To rotate the video 90 degrees clockwise including the dimensions, use the video filter `-vf "transpose=1"` (or other transpose value, e.g. http://stackoverflow.com/a/9570992/954643) – qix Apr 02 '14 at 05:31
  • I just tried this on an x265 video (converted from an x264 source of vertical video on an older iPhone) and it worked beautifully. Plays perfectly with VLC 2.2.1 on OS X. – Todd Lehman Oct 30 '15 at 06:13
  • @slhck: I can rotate a video 90 degrees, but when I try to rotate the same videos again using 180 degrees then I the output video has the same orientation like the original movie. I can re-encode the video with `-vf "vflip,hflip"` but your approach is much quicker. Is it possible to rotate 180°? – Besi Feb 19 '16 at 10:36
  • @Besi If you set `-metadata:s:v:0 rotate=180` then the player should show the video rotated. If it doesn't, it is probably a problem with the player itself? Since it's just metadata, the original video will always be left as-is. – slhck Feb 19 '16 at 11:49
  • This was exactly what I needed! `-metadata:s:v rotate=360` fixed the wrong metadata. It changed from `Side data: displaymatrix: rotation of -90.00 degrees` to `displaymatrix: rotation of -0.00 degrees` and is now correct. – user643011 Aug 29 '17 at 22:45
  • i am concatenating two videos using ffmpeg concat demux .The problem is that when i join the two videos ,the first video is maintaining its orientation but the second video's orientation is changed by 90 degrees .Somebody help me please – Sid Dec 15 '17 at 07:13
  • @Sid Please ask a new question, show the ffmpeg command, and include the full, uncut command line output from ffmpeg. – slhck Dec 15 '17 at 09:31
  • @slhck actually i found that the video with landscape does not have rotation tag in meta data and so it is causing problem.Just wanted to ask that is there any way to join one video in landscape mode and the other in potrait with correct orientation – Sid Dec 15 '17 at 09:41
  • @Sid Use a `-filter_complex` filtergraph, then the `rotate` filter for one of the clips, and then the `concat` filter to join the original and the rotated one. – slhck Dec 15 '17 at 10:30
  • @slhck thank you ....that worked .Actually im just curious if there is a way to check the orientation of the video ? i mean any command – Sid Dec 15 '17 at 10:32
  • `-noautorotate` saved me big time! without this, I had a hard time with output video to be all over the place. – KayaNatsumi Jan 31 '21 at 01:19