How to Rotate Video by Arbitrary Angle in FFmpeg
Rotating a video by non-standard angles—such as 45, 33, or 120
degrees—cannot be done with simple transpose filters in FFmpeg. Instead,
you must use the powerful rotate video filter, which
processes rotations using radians. This guide provides a direct,
step-by-step tutorial on how to apply arbitrary rotations, prevent
cropped corners by automatically resizing the output window, and
customize the background fill color.
The Basic Rotation Command
To rotate a video, you must pass the angle to the rotate
filter in radians. The formula to convert degrees to radians is
degrees * PI / 180. FFmpeg allows you to use mathematical
expressions directly in the command.
To rotate a video clockwise by 45 degrees, use the following command:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180" output.mp4Note: In this basic command, the output video dimensions remain the same as the input video, which means the corners of your rotated video will be clipped.
Rotating Without Clipping (Auto-Sizing)
To prevent the corners of the video from being cut off, you must
dynamically recalculate the output width (ow) and height
(oh) of the canvas. FFmpeg provides built-in functions
rotw(a) and roth(a) to calculate the required
bounding box size for a given angle a.
Use this command to rotate the video 45 degrees and automatically expand the canvas size so the entire video remains visible:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180:ow='rotw(45*PI/180)':oh='roth(45*PI/180)'" output.mp4Changing the Background Fill Color
When you rotate a video by an arbitrary angle, empty black areas will
appear around the rotated frame. You can customize the color of this
empty space using the fillcolor option (either by name or
hex code).
To set the background color to blue, run:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180:ow='rotw(45*PI/180)':oh='roth(45*PI/180)':fillcolor=blue" output.mp4To use a hex color code (e.g., #FFFFFF for white),
format it like this:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180:ow='rotw(45*PI/180)':oh='roth(45*PI/180)':fillcolor=0xFFFFFF" output.mp4Rotating Counter-Clockwise
To rotate the video counter-clockwise, simply add a minus sign before the angle value. For example, to rotate 45 degrees counter-clockwise:
ffmpeg -i input.mp4 -vf "rotate=-45*PI/180:ow='rotw(-45*PI/180)':oh='roth(-45*PI/180)'" output.mp4