1. Overview

FFmpeg is a free and open-source tool used for processing media files. It contains various features that make it one of the best multimedia frameworks in Linux. Because it’s open-source, it’s the base for most popular media players and video editors including VLC, iTunes, YouTube, and more.

In this tutorial, we’ll look at the different methods used to rotate videos to different degrees using FFmpeg.

2. Setup

Before we start exploring how we can rotate videos, let’s first ensure we have FFmpeg installed:

$ ffmpeg -version
ffmpeg version 4.4-6ubuntu5 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-7ubuntu1)
...truncated...

In case we don’t have FFmpeg installed, we can use this command to install it:

$ sudo apt install ffmpeg

On Arch Linux Distro, we can use this command to install FFmpeg :

$ pacman -S ffmpeg

Let’s also download a sample 1MB video file. We’ll use it to try some of the different rotations.

Here’s a preview of this video:

sample video

3. Using transpose

transpose is an FFmpeg filter used to rotate videos. We can use it to rotate the sample video either clockwise or anti-clockwise. It also allows us to flip videos vertically or horizontally.

The transpose filter accepts the values from 0-3. Let’s take a look at what each value represents:

  • 0 = 90° counter-clockwise and vertical flip (default)
  • 1 = 90° clockwise
  • 2 = 90° counter-clockwise
  • 3 = 90° clockwise and vertical flip

Let’s see how we can rotate the sample video using the transpose filter:

$ ffmpeg -i big_buck_bunny_720p_1mb.mp4 -vf "transpose=0" output_0.mp4
...truncated...
 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 341 kb/s (default)
Metadata:
creation_time : 1970-01-01T00:00:00.000000Z
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc58.134.100 aac
frame= 132 fps= 27 q=-1.0 Lsize= 1274kB time=00:00:05.29 bitrate=1972.2kbits/s speed= 1.1x 
video:1040kB audio:229kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.406613%
...truncated...

We’re passing the value 0 to the transpose filter which rotates the video counterclockwise and vertically. This will change the video orientation to portrait. This is the default preset if we don’t pass any values to transpose.

We’ll get this output when playing the output_0.mp4 video:

rotate 45 ffmpeg

We can also combine multiple transpose filters to change the orientation of the video.

Let’s combine two transpose filters, each with a value of 1 for a 180° flip:

$ ffmpeg -i big_buck_bunny_720p_1mb.mp4 -vf "transpose=1, transpose=1" output_transpose_multiple.mp4

We’re flipping the video by 90° clockwise twice.

We get this output when playing the output_transpose_multiple.mp4 video:

transpose multiple

4. Using rotate

The FFmpeg rotate filter offers more flexibility by allowing us to rotate videos by an arbitrary angle expressed in radians. This allows us to rotate the videos to more precise angles that aren’t available with other methods.

This command will rotate the video by π/1 radians, or 180°:

$ ffmpeg -i big_buck_bunny_720p_1mb.mp4 -vf "rotate=PI:bilinear=0" output_rotate_180.mp4

We’re using “bilinear=0″ to switch off bilinear interpolation for angles divisible by 90. This is because the video might look blurry otherwise.

We can also use degrees instead of radians. One degree is equal to π/180 radians, so if we want to rotate to a specific degree, we can multiply it by π/180:

$ ffmpeg -i big_buck_bunny_720p_1mb.mp4 -vf "rotate=45*(PI/180)" output_rotate_45.mp4

We get this output when playing the output_rotate_45.mp4 video:

rotate 45

5. Using hflip and vflip

hflip is an FFmpeg filter that flips the input video horizontally while vflip flips the video vertically. We can use a combination of both filters to rotate the video by 180°.

Let’s rotate the sample video using the hflip and vflip filters:

$ ffmpeg -i big_buck_bunny_720p_1mb.mp4 -vf "hflip, vflip" output_combined.mp4
...truncated...
 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 341 kb/s (default)
Metadata:
creation_time : 1970-01-01T00:00:00.000000Z
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc58.134.100 aac
frame= 132 fps= 27 q=-1.0 Lsize= 1274kB time=00:00:05.29 bitrate=1972.2kbits/s speed= 1.1x 
video:1040kB audio:229kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.406613%
...truncated...

We get this result when playing the output_combined.mp4 video using a video player:

hflip and vflip

5.1. Rotating Upon Playback

Alternatively, we can rotate the video upon playback, this saves us the trouble of re-encoding the sample video.

For this, we need to use ffplay. It’s a lightweight media player that comes bundled with the installation of FFmpeg.

Let’s see how we can rotate the video by 180° before playing it:

$ ffplay -vf "hflip, vflip" -i big_buck_bunny_720p_1mb.mp4
...truncated...
Duration: 00:00:05.31, start: 0.000000, bitrate: 1589 kb/s
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1205 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      creation_time   : 1970-01-01T00:00:00.000000Z
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 384 kb/s (default)
...truncated...

This command flips the video both horizontally and vertically, and then auto-plays it.

6. Conclusion

In this article, we’ve seen the different methods available for rotating video files using the FFmpeg tool.

The first method allows us to flip the video vertically and rotate the video clockwise or anticlockwise. The second method allows us to specify a rotation degree which offers more flexibility in cases where precision is key. Finally, the last method allows us to flip the video vertically and horizontally.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.