How to Rotate Video in FFmpeg without Cropping
This article explains how to use the FFmpeg rotate
filter to rotate a video by any arbitrary angle while dynamically
adjusting the output frame size. By configuring the output width and
height to match the rotated bounds, you can prevent the video from being
cropped or having its corners cut off during the rotation process.
By default, the FFmpeg rotate filter retains the
original input dimensions. When you rotate a video at an angle other
than multiples of 180 degrees, this default behavior causes the corners
of the video to be clipped. To prevent this, you must explicitly
calculate and set the output width (ow) and height
(oh) to fit the new bounding box of the rotated image.
FFmpeg provides two built-in functions specifically for this
calculation: rotw(a) and roth(a), where
a is the rotation angle in radians.
The FFmpeg Command
To rotate a video and automatically expand the canvas to fit the rotated frame, use the following filter syntax:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180:ow='rotw(45*PI/180)':oh='roth(45*PI/180)'" output.mp4How It Works
rotate=45*PI/180: This sets the rotation angle. FFmpeg expects the angle in radians. To rotate by degrees, multiply the degree value byPI/180(e.g.,45*PI/180for 45 degrees).ow='rotw(45*PI/180)': This defines the output width. Therotwfunction calculates the minimum width required to hold the rotated input video at the specified angle without cropping.oh='roth(45*PI/180)': This defines the output height. Therothfunction calculates the minimum height required to hold the rotated input video at the specified angle.
Handling the Background Color
When the output frame size expands, empty areas (corners) will be
created around the rotated video. By default, FFmpeg fills these areas
with black. If you want to change this background color, you can use the
fillcolor (or c) option:
ffmpeg -i input.mp4 -vf "rotate=30*PI/180:ow='rotw(30*PI/180)':oh='roth(30*PI/180)':fillcolor=blue" output.mp4Using this method ensures your video is fully visible at any angle, with the output dimensions automatically scaling to perfectly fit the newly rotated boundaries.