Sometimes a video is recorded, it might end up sideways or upside down. Instead of recording it again, you can quickly rotate or flip it. Other times, you might want to rotate a video clip to achieve a desired effect. In either case, you can use FFmpeg to change your video’s orientation through a straightforward and precise process.
This article explains why FFmpeg is a great choice for video rotation, how to perform basic rotations and flips, best practices to ensure quality, and how Cloudinary can simplify the process further.
Key takeaways:
- How to rotate videos using FFmpeg’s
transpose
androtate
filters - How to use Cloudinary to simplify video rotation
- Best practices for rotating videos with FFmpeg
In this article:
- Why Bother to Rotate Videos Using FFmpeg?
- Getting Started with Basic Video Rotation in FFmpeg
- Best Practices for Using FFmpeg to Rotate Video
- Simplifying Video Transformations with Cloudinary
Why Bother to Rotate Videos Using FFmpeg?
FFmpeg is a popular and industry-standard command-line tool for handling video, audio, and other multimedia files processing. For video rotation, it offers several advantages, including:
- High Customizability: FFmpeg provides simple and advanced configuration settings that allow you control over rotation angles and output settings.
- Fast Processing: FFmpeg processes videos quickly, even on modest hardware, without requiring expensive software.
- Wide Format Support: FFMpeg allows you to rotate videos in virtually any video format, from old formats like MP4 and MOV, to modern formats like WebM and H.265 (HEVC).
- Batch Processing: FFmpeg allows you to rotate multiple videos at once using scripts, which saves you time for bulk edits.
- Cross-Platform: FFmpeg works across many OSes and programming languages, making it reliable for use in small and large projects alike.
Using FFmpeg to Rotate a Video
Before diving into the examples, you need to ensure FFmpeg is installed on your system. You can download it from the official website or install it via a package manager. Once installed, open a terminal and verify it’s working by typing ffmpeg -version. If you get an output with the version number, you’re good to go!
You can download the video we’ll be using in the demos from here. We named the file ‘sports-car.mp4.’
Simple Rotations
FFmpeg provides the transpose and rotate filters for rotating and/or flipping videos. The transpose filter takes numerical values to specify the desired rotation/flip:
- transpose=1: Rotate 90 degrees clockwise (90°).
- transpose=2: Rotate 90 degrees counter-clockwise (-90°). Equivalent to rotating 270 degrees.
- transpose=3: Rotate 90 degrees clockwise and flip vertically.
- transpose=4: Rotate 90 degrees counter-clockwise and flip vertically.
Here’s an image showing the orientation of different angles (assuming you’re rotating in the clock-wise direction):
It’s important to note that rotating a video and flipping it are often wrongly used interchangeably, but they achieve different things. Flipping a video mirrors it horizontally or vertically, while rotation changes its angle clockwise or counter. If you want to flip a video specifically, you can use the hflip
(horizontal flip) and vlip
(vertical flip) filters.
Rotate 90 Degrees Clockwise
To rotate a video 90 degrees clockwise, you can use transpose=1
:
ffmpeg -i sports-car.mp4 -vf "transpose=1" rotated_video.mp4
The -vf
flag in the command above stands for “video filter”. Running the command creates a new video rotated as shown below:
Rotate 90 degrees clockwise and flip vertically
To rotate a video 90 degrees and flip it, run:
ffmpeg -i sports-car.mp4 -vf "transpose=3" rotated_video.mp4
Rotate Upside Down (Rotating 180 degrees)
To rotate a video completely upside down, we can use a combination of the transpose
filter as follows:
ffmpeg -i sports-car.mp4 -vf "transpose=1,transpose=1" rotated_video.mp4
Here, “transpose=1,transpose=1” applies two rotations:
- transpose=1 rotates the video 90° counterclockwise.
- Applying it twice gives a total of 180° rotation.
Using the rotate Filter for Arbitrary Angles
The rotate filter is used for non-standard rotations and is less commonly used for simple 90-degree adjustments. It has an angle parameter, which is expressed in radians.
The basic syntax for the rotate filter is:
ffmpeg -i input.mp4 -vf "rotate=angle" rotated_video.mp4
The angle parameter sets an expression for the angle by which to rotate the input video clockwise. A negative value will result in a counter-clockwise rotation. By default, it is set to “0”. To convert from degrees to radians, use the following formula:
angle = degrees * PI / 180
Here are some examples:
Rotate 45 degrees clockwise:
ffmpeg -i input.mp4 -vf "rotate=45*PI/180" output_45_deg.mp4
Rotate 90 degrees counter-clockwise:
ffmpeg -i input.mp4 -vf "rotate=-90*PI/180" output_270_deg.mp4
Rotate 180 degrees:
ffmpeg -i input.mp4 -vf "rotate=PI" output_180_deg.mp4
This is equivalent to rotate=180*PI/180.
Best Practices for Using FFmpeg to Rotate Video
Now that you know how to rotate videos, it’s important to ensure your results are always as expected and consistent. By following these best practices, you can maintain video quality and avoid common issues when using FFmpeg for rotation.
- Preserve Original Video Quality: By default, FFmpeg re-encodes the rotated video, which may reduce quality. To prevent this, you can specify a target video quality using the CRF (Constant Rate Factor) quality factor (-crf). A good range to use is 18-28 (lower numbers mean higher quality, but result in a larger file size):
ffmpeg -i sports-car.mp4 -vf "transpose=1" -crf 20 rotated_video.mp4
- Copy the Audio Without Re-encoding: If you only need to rotate the video, use -c:a copy to copy the audio stream without re-encoding it. This will significantly save encoding time and maintain the audio’s original quality:
ffmpeg -i sports-car.mp4 -vf "transpose=1" -crf 20 -c:a copy rotated_video.mp4
- Use Metadata Rotation with Caution: One common trick people use to rotate videos without re-encoding is modifying the video’s metadata, which changes a flag in the file telling a video player how to orient the video. However, not all players honor the rotate metadata. For example, mobile players usually respect it, but some desktop players might ignore it, showing the video unrotated.
Simplifying Video Transformations with Cloudinary
FFmpeg, while useful, has downsides for complex applications regarding performance and security. To solve these problems, a cloud-based service like Cloudinary can handle the heavy processing load.
Cloudinary is a cloud-based media management platform that handles the processing, uploading, storing, manipulating, optimizing, and delivery of media assets, including video and audio. Their tools can do the media processing in the cloud, eliminating the need to perform it locally.
To rotate a video with Cloudinary, there are a few approaches you can take, depending on the programming language you’re using. However, the simplest approach is to use URL transformations. For videos that have been uploaded to your Cloudinary product environment, using the a (angle) parameter.
Take our demo video which has been uploaded to Cloudinary for example:
https://res.cloudinary.com/demo/video/upload/blue_sports_car.mp4
Adding a_90 to the URL rotates the video 90 degrees clockwise:
https://res.cloudinary.com/demo/video/upload/a_90/blue_sports_car.mp4
While a_-90 will rotate the video counterclockwise:
https://res.cloudinary.com/demo/video/upload/a_-90/blue_sports_car.mp4
As shown, clockwise video rotation uses positive integers, and counterclockwise uses negative. When the angle isn’t a multiple of 90, a rectangular bounding box is added to include the rotated video and empty space.
Using Cloudinary to rotate videos offers several advantages, including its simple approach to managing video assets, its scalability, and its robust set of features that are accessible even to those without specialized technical knowledge.
You can learn more about video rotation with Cloudinary in the docs.
Wrapping Up
FFmpeg is a powerful tool for a variety of video tasks, and rotating a video is one of its most useful functions. With a simple transpose or rotate command, you can quickly fix the orientation of your video clips. This approach is not only fast and efficient but also gives you a high degree of control over your media. While FFmpeg is great for simple applications, services like Cloudinary offer an automated solution for large-scale video transformation.
Frequently Asked Questions
Can I rotate multiple videos at once with FFmpeg?
FFmpeg is a command-line tool designed for single-file operations. To process multiple files, you can write a bash or powershell script to automate the process. Here’s an example script that iterates through all MP4 files in the current directory, rotates each one 90 degrees clockwise, and saves the output to a new directory named rotated:
mkdir -p rotated; for f in *.mp4; do ffmpeg -i "$f" -vf "transpose=1" -c:a copy "rotated/$f"; done
Will rotating a video with FFmpeg reduce its quality?
Rotating a video using the transpose or rotate filters will re-encode it, which may lead to some quality reduction. To minimize quality loss during re-encoding, use the -crf filter or -c:v libx264 to use a high-quality H.264 encoder.
Can I rotate videos without changing their size?
Yes, you can. By default, FFmpeg will keep the original resolution after rotation. However, if the rotated video has a different aspect ratio, you may see black borders added around the video.