How to Rotate Video and Change Background Color in FFmpeg

Rotating a video often alters its aspect ratio, which can result in empty spaces or black bars on the sides of the frame. This guide demonstrates how to use FFmpeg to rotate a video and customize the background color of those newly created empty areas using a single command.

Step 1: The Basic Command Structure

To rotate a video and fill the surrounding empty space with a specific color, you must combine the transpose filter (for rotation) and the pad filter (for adding the background color) within a video filter chain (-vf).

Here is the standard command:

ffmpeg -i input.mp4 -vf "transpose=1,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=blue" output.mp4

Step 2: Understanding the Parameters

To customize this command for your specific video, you need to understand how the filters work:

  • transpose=1: This rotates the video 90 degrees clockwise. You can change this value depending on your needs:
    • 0: Rotate 90 degrees counter-clockwise and flip vertically.
    • 1: Rotate 90 degrees clockwise.
    • 2: Rotate 90 degrees counter-clockwise.
    • 3: Rotate 90 degrees clockwise and flip vertically.
    • Note: To rotate 180 degrees, use transpose=2,transpose=2.
  • pad=width:height:x:y:color: This defines the new canvas size, positioning, and background color.
    • 1920:1080: The desired width and height of your output video. Adjust these numbers to match your target resolution.
    • (ow-iw)/2: Centers the video horizontally on the new canvas (Output Width minus Input Width, divided by two).
    • (oh-ih)/2: Centers the video vertically on the new canvas (Output Height minus Input Height, divided by two).
    • color=blue: Specifies the background color. FFmpeg accepts standard color names (e.g., red, black, white, blue, green) as well as Hex color codes (e.g., 0x333333 or #333333).

Example: Rotating Landscape to Portrait with a Gray Background

If you have a standard 1920x1080 landscape video, rotate it 90 degrees clockwise to make it portrait, and want to pad the sides to maintain a 1920x1080 frame with a dark gray background, use this command:

ffmpeg -i input.mp4 -vf "transpose=1,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=0x1A1A1A" -c:a copy output.mp4

In this command, -c:a copy is added to copy the audio stream without re-encoding it, which speeds up the rendering process.