transpose video filter combined with metadata rotation tags to automatically correct orientation in FFMpeg. This approach ensures that both the visual stream and the container metadata align, preventing playback issues on mobile devices and web players.
Understanding Video Orientation Metadata
Most modern cameras and smartphones record video in a landscape orientation but store the actual footage rotated 90 degrees. They rely on a metadata tag calledrotate within the container (like MP4 or MOV) to tell the player how to display the frames correctly. When you process these files with FFMpeg, this metadata is often stripped out or ignored if not handled explicitly. Without it, the video plays sideways because the raw pixel data remains unrotated while the player expects the standard landscape view.
To fix this, you must either physically rotate the pixels using filters or update the metadata tags so the player knows how to orient the content. The best practice is to do both: apply the visual transformation for compatibility and update the metadata for clarity. This ensures that when you upload the file to social media or embed it in HTML, as seen in troubleshooting guides like Troubleshooting: Unable to Display FFMPEG-Created Video in HTML, the orientation is preserved correctly across all platforms.
Rotating Video Streams with Filters
The most reliable way to permanently rotate video frames is by using thetranspose filter. This filter accepts two main arguments: the direction of rotation and an optional flip parameter. The standard values are 0 for 90 degrees clockwise, 1 for 90 degrees counter-clockwise, and 2 for 180 degrees. You can also append _flip to these values if you need to mirror the image simultaneously, which is sometimes required depending on the source camera's sensor layout.
Here is the basic command structure to rotate a video 90 degrees clockwise:
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
In this example, transpose=1 rotates the video 90 degrees counter-clockwise. If your video needs to be rotated 90 degrees clockwise, use transpose=0. For a 180-degree rotation, use transpose=2. It is crucial to note that applying this filter re-encodes the video stream, which may result in quality loss if not done carefully. Always use a high enough bitrate or a lossless codec during the intermediate steps if you plan to transcode further.
Updating Rotation Metadata Tags
While filtering rotates the pixels, updating the metadata ensures that players who support EXIF-style rotation tags interpret the file correctly. Some players might ignore the rotated pixels if the metadata still says the video is upright, leading to double-rotation effects. To clear or set the rotation tag, you can use the-metadata:s:v:0 rotate=0 flag after the filter has applied the physical rotation.
If you want to keep the original metadata rotation but ensure the pixels match, you should first check the existing rotation value using:
ffprobe -v error -select_streams v:0 -show_entries stream_tags=rotate -of default=noprint_wrappers=1:nokey=1 input.mp4
This command outputs the current rotation angle in degrees. If the output is 90, 180, or 270, you know the file relies on metadata. After applying the transpose filter, you should reset this tag to zero to indicate that the pixels are now upright:
ffmpeg -i input.mp4 -vf "transpose=1" -metadata:s:v:0 rotate=0 output.mp4
This combination guarantees that the video looks correct regardless of whether the player uses hardware acceleration, software decoding, or metadata-based orientation.
Handling Image Output from Video
When extracting images from rotated videos, the resulting frames will inherit the physical rotation of the video stream at that moment. If you extract frames before rotating them, the images will be sideways. If you extract after rotating, they will be upright. However, some image viewers also look for EXIF rotation tags in JPEG files. To ensure consistency, you can add the-metadata:s:v:0 rotate=0 flag even when generating images, though it is less common for static images unless they are part of a sequence.
For batch processing multiple files, consider combining this with other operations like adding subtitles or music, similar to workflows described in Fixing FFMPEG Command: Creating Subtitles with the Right Font. Ensure that the rotation filter is placed early in the filter graph to avoid unnecessary processing overhead on already-transformed frames.
Frequently Asked Questions
Why does my video play upside down after encoding?
This happens because the rotation metadata was stripped during encoding, leaving the raw pixels in their original orientation. Always apply the transpose filter and reset the metadata tag to ensure consistent playback.
Can I rotate a video without re-encoding it?
No, physical rotation requires re-encoding the video stream. You can only update the metadata tag without re-encoding, but this only changes how the player displays the video, not the underlying pixel data.
What is the difference between transpose 0 and 1?
Transpose 0 rotates the video 90 degrees clockwise, while transpose 1 rotates it 90 degrees counter-clockwise. Use transpose 2 for a 180-degree rotation.
How do I check the current rotation of a video file?
Use the ffprobe command with -show_entries stream_tags=rotate to retrieve the rotation angle stored in the video stream's metadata.
Do I need to rotate images extracted from video?
If you extract frames after applying the transpose filter, the images will already be correctly oriented. No additional rotation is needed for the image files themselves.