Rotate Video Continuously Over Time with FFmpeg

This article provides a step-by-step guide on how to continuously spin or rotate a video over time using FFmpeg's powerful rotate filter. You will learn the exact command-line syntax to apply time-based mathematical expressions for a seamless spinning effect, as well as how to adjust the output canvas size to prevent the edges of your video from being cropped during rotation.

The Basic Spinning Command

To make a video spin continuously, you must use FFmpeg's rotate video filter. This filter accepts mathematical expressions that update dynamically for every frame. By using the variable t (which represents the current time in seconds), you can increase the rotation angle as the video plays.

Here is the basic command to rotate a video 360 degrees (2 * PI radians) every 10 seconds:

ffmpeg -i input.mp4 -vf "rotate='2*PI*t/10'" output.mp4

Understanding the Formula

  • rotate: The name of the video filter.
  • t: The current timestamp of the frame in seconds.
  • PI: The mathematical constant \(\pi\) (approximately 3.14159), representing a 180-degree turn in radians. A full 360-degree rotation is equal to 2*PI radians.
  • /10: The duration in seconds for one full 360-degree rotation. Decreasing this number makes the video spin faster, while increasing it makes it spin slower. For example, rotate='2*PI*t/5' will complete a full spin every 5 seconds.

Preventing Cropping (Expanding the Canvas)

By default, the rotate filter keeps the original dimensions of the input video. Because a rotating rectangle is wider diagonally than it is horizontally or vertically, the corners of your video will get cut off during the spin.

To prevent this cropping, you must expand the output frame size to accommodate the diagonal length of the video. You can calculate this automatically using the hypotenuse function hypot(iw, ih) (where iw is input width and ih is input height).

Use this command to spin the video without cutting off the corners:

ffmpeg -i input.mp4 -vf "rotate='2*PI*t/10':ow='hypot(iw,ih)':oh='ow':fillcolor=black" output.mp4

Parameter Breakdown for the Advanced Command:

  • ow='hypot(iw,ih)': Sets the output width to the diagonal length of the input video.
  • oh='ow': Sets the output height equal to the new output width, creating a perfect square canvas that prevents clipping at any angle.
  • fillcolor=black: Fills the newly created empty space in the background with black. You can change this to any hexadecimal color (e.g., fillcolor=#FFFFFF for white) or use none if your output format supports transparency (like ProRes or VP9 WebM).