FFmpeg command for Flipping and Rotating Video

FFmpeg, a powerful multimedia processing tool, offers a range of functionalities to manipulate videos, including flipping and rotating. These operations are useful for correcting orientation issues, adjusting the perspective, or creating artistic effects. In this detailed guide, we will explore the FFmpeg commands for flipping and rotating videos, providing step-by-step instructions and customization options

  1. Installing FFmpeg:
    Before getting started, ensure FFmpeg is installed on your system. Download the latest version from the official website (https://ffmpeg.org/download.html) or use package managers like Homebrew on macOS or APT on Linux.
  2. Navigate to the Video File Directory:
    Open a terminal or command prompt and navigate to the directory containing the video file you want to manipulate. Use the cd command to change the directory:
   cd /path/to/video/directory
  1. Execute FFmpeg Command – Vertical Flip:
    To vertically flip a video, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "vflip" -c:a copy output_vertical_flip.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "vflip": Applies a vertical flip filter.
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_vertical_flip.mp4: Defines the output filename for the vertically flipped video.
  1. Execute FFmpeg Command – Horizontal Flip:
    To horizontally flip a video, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "hflip" -c:a copy output_horizontal_flip.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "hflip": Applies a horizontal flip filter.
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_horizontal_flip.mp4: Defines the output filename for the horizontally flipped video.
  1. Execute FFmpeg Command – Rotate Clockwise:
    To rotate a video clockwise, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "transpose=1" -c:a copy output_clockwise_rotation.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "transpose=1": Applies a transpose filter, rotating the video clockwise.
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_clockwise_rotation.mp4: Defines the output filename for the clockwise-rotated video.
  1. Execute FFmpeg Command – Rotate Counterclockwise:
    To rotate a video counterclockwise, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "transpose=2" -c:a copy output_counterclockwise_rotation.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "transpose=2": Applies a transpose filter, rotating the video counterclockwise.
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_counterclockwise_rotation.mp4: Defines the output filename for the counterclockwise-rotated video.
  1. Adjusting Parameters:
  • Output Filename Format: Customize the output filenames based on your preferences.
  1. Executing the Commands:
    Press Enter to execute the FFmpeg commands. FFmpeg will process the videos, applying the specified flipping or rotating effects. The progress will be displayed in the terminal.
  2. Viewing the Output:
    Once the process is complete, navigate to the output directory where the manipulated videos are saved. Open the generated files using a media player to inspect the applied effects.
  3. Customization and Additional Options:
    Explore FFmpeg’s extensive documentation for advanced options, such as combining multiple filters, adjusting rotation angles, or using other flipping filters. Customizing the commands allows you to tailor the video transformations to your specific requirements.

Flipping and rotating of the video frame are common visual operations, that can be used to create various interesting effects like mirrored versions of the input

To test the horizontal flip on a testsrc video source, we can use the command

ffplay -f lavfi -i testsrc -vf hflip

FFmpeg command for Vertical flip

ffplay -f lavfi -i rgbtestsrc -vf vflip

FFmpeg Rotating Command

Rotation by 90 degrees counterclockwise and flip vertically

ffmpeg -i CMYK.avi -vf transpose=0 CMYK_transposed.avi

Rotation by 90 degrees clockwise

ffmpeg -i CMYK.avi -vf transpose=1 CMYK_transposed.avi

Rotation by 90 degrees counterclockwise

ffmpeg -i CMYK.avi -vf transpose=2 CMYK_transposed.avi

Rotation by 90 degrees clockwise and flip vertically

ffmpeg -i CMYK.avi -vf transpose=3 CMYK_transposed.avi

Conclusion:
Transforming videos with FFmpeg provides a flexible and efficient solution for correcting orientation issues or creating artistic effects. By following this comprehensive guide, you can use FFmpeg to flip and rotate videos, bringing creative possibilities to your video projects.

Leave a Comment