How to Rotate A Video using FFmpeg Easily

Tags:

Sometimes when a video is captured wrong due to bad technique or simply bad luck, its useful to have a simple way to rotate it either clockwise or anti-clockwise.

Lucky for us, FFmpeg has a filter to rotate a video by specifying the direction. It takes a video as the input and a set of commands that you can use to instruct FFmpeg on how to rotate the video, and then FFmpeg will take care of the rest.

How to Rotate a Video 90° With FFmpeg?

Here is the command line for rotating a video using FFmpeg’s transpose filter.

ffmpeg -i inputVideo.mp4 -vf "transpose=dir=1" rotatedVideo.mp4

You are using the transpose filter using -vf in FFmpeg. This takes a set of parameters to control the direction of the transpose/flip/rotate operation and to prevent the transpose from taking place.

Let’s learn more about this.

Here are the options that are available in FFmpeg to rotate a video. You can either mention a number from 0 – 4 or the words cclock_flip, clock, cclock, or clock_flip.

0/cclock_flip = 90 degrees CounterCLockwise and Vertical Flip (default)
1/clock = 90 degrees Clockwise
2/clock = 90 degrees CounterClockwise
3/clock_flip = 90 degrees Clockwise and Vertical Flip

How to Rotate a Video 180° With FFmpeg?

Let’s look at this use case now. How do you rotate a video 180° using the transpose command?

ffmpeg -i inputVideo.mp4 -vf "transpose=dir=2:transpose=dir=2" rotatedVideo.mp4

I used the ParkJoy sequence and flipped it 180 using this command. You can see the result for yourself in the video below. So basically, you are instructing FFmpeg to rotate the video 90 degrees counter-clockwise two times which totals up to 180 degrees.

How to Prevent Rotation for Landscape or Portrait Modes?

A typical use case for this is for post-processing videos captured from your mobile phone – some are probably in portrait mode and some are in landscape mode.

Now, if you use FFmpeg to process them as a batch, you probably want a way to tell FFmpeg to not rotate videos that are either in landscape or portrait mode.

Thankfully, there is an option for that as well in FFmpeg’s transpose filter.

ffmpeg.exe -i parkjoy.mp4 -vf "transpose=dir=2:passthrough=landscape" parkjoy-90-flip.mp4

The above commandline says that FFmpeg should transpose the video 90 degrees CounterClockwise ONLY if the video is NOT landscape mode.

Conclusion

I hope you understood how to rotate a video using FFmpeg. If you liked this, please let us know in the comments section, share the post with your friends, and check out the rest of our FFmpeg tutorials.

About The Author

I’m Dr. Krishna Rao Vijayanagar, and I am the Founder and Editor of OTTVerse.com. I've spent several years working hands-on with Video Codecs (AVC, HEVC, MultiView Plus Depth), ABR streaming, and Video Analytics (QoE, Content & Audience, and Ad). I hope to use my experience and love for video streaming to bring you information and insights into the OTT universe. Please use the Contact Page to get in touch with me.

Leave a Reply