Rotate a video with ffmpeg

Rotate with ffmpeg


Disclaimer: DeepL.com (free version) helped me a lot with the translation.

There are two main ways to rotate a video:

  1. Re-render (slower but reliable)
  2. Only adjust the MetaData (much faster but more error-prone)

And we don’t just want to rotate the image in the video. Because then parts of the image will be cut off and black bars will appear at the edges:

rotate by 90° with black bars

To avoid this, we want to rotate the entire video, together with its orientation. This means that a horizontal video becomes a vertical video. Or a vertical one becomes a horizontal video.

To do this, the image in the video is rotated, but the aspect ratio of the video is also adjusted:

rotate by 90° without black bars



1. Re-render (slower but reliable

If we rotate the video and completely re-render it, this will take a little longer than just adjusting the metadata.

But we can be sure that every video program will be able to handle the new video and there will be no problems.

90° (90° clockwise)

rotate by 90°

ffmpeg -i "input.mp4" -vf "rotate=90*PI/180:oh=iw:ow=ih" "output.mp4"

-90° (90° counterClockwise)

rotate by -90°

ffmpeg -i "input.mp4" -vf "rotate=-90*PI/180:oh=iw:ow=ih" "output.mp4"

180°

rotate by 180°

ffmpeg -i "input.mp4" -vf "rotate=180*PI/180" "output.mp4"

2. Only adjust the MetaData (much faster but more error-prone)

Now we only want to adjust the MetaData so that the video is rotated. This is much faster because the video does not have to be re-rendered, but can be copied almost completely.

Attention: This can lead to errors in some video programs if they do not handle the MetaData!

However, if you only want to use the video yourself and your own software can handle it, this can be a very fast alternative.

Attention: It is possible that e.g. an upright video (portrait video) already has the rotation value 90 or -90 in its metadata! Then we have to try out a bit which of the values is the right one for us: 0, 90, -90 oder 180.

But you can just try it out, because the method is so fast anyway.

90° (90° clockwise)

rotate by 90°

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

-90° (90° counterClockwise)

rotate by -90°

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

180°

rotate by 180°

ffmpeg -i "input.mp4" -map_metadata 0 -metadata:s:v rotate="180" -codec copy "output.mp4"

The sign does not matter here, because a rotation by +180° is the same as a rotation by -180°.