Understanding Video Orientation Metadata
Modern video files often contain an rotate metadata tag that tells compatible players how to orient the image. If this tag is missing, incorrect, or ignored by your playback software, the video will display in its raw, potentially inverted state. The underlying pixel data might be perfectly fine, but the player needs explicit instructions on which way is up. This issue is common with videos captured from smartphones or cameras that automatically rotate footage during recording but fail to update the file header consistently.
Before attempting complex re-encoding, it is crucial to diagnose whether the problem lies in the metadata or the actual pixels. Some players ignore metadata entirely and rely solely on the visual content. In these cases, simply updating the tag will not solve the viewing issue, and you must physically rotate the frames using ffmpeg filters.
Method 1: Updating Metadata Tags
If your video player supports metadata-based rotation, the fastest solution is to write the correct rotation value into the file headers. This method is lossless because it does not re-encode the video stream, preserving original quality and completing almost instantly. You can use the -metadata flag to set the rotate value.
ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=180 output.mp4
In this command, -c copy instructs ffmpeg to copy the streams without re-encoding them. The -metadata:s:v:0 rotate=180 part targets the first video stream (s:v:0) and sets the rotation angle to 180 degrees. Adjust the angle based on your specific issue: use 90, 180, 270, or 0. Note that some older versions of ffmpeg may require you to use -metadata rotate=180 without the stream specifier if the tool fails to locate the specific stream index.
This approach is ideal when you know the metadata is broken but the video content itself is upright. However, if the video still appears sideways after this operation, the metadata was not being read by your player, or the pixel data itself is rotated. Proceed to Method 2 for a permanent fix.
Method 2: Physically Rotating Video Frames
When metadata updates fail, you must transform the actual pixel data. This involves re-encoding the video, which takes more time and may slightly reduce quality depending on the codec settings. You use the transpose filter within the -vf (video filter) option.
ffmpeg -i input.mp4 -vf "transpose=1" -c:a copy output.mp4
The transpose filter accepts integer values that define the rotation direction:
0: Rotate 90 degrees counter-clockwise and flip vertically (default).1: Rotate 90 degrees clockwise.2: Rotate 90 degrees counter-clockwise.3: Rotate 90 degrees clockwise and flip vertically.
To flip a video completely upside down, you typically need two transposes. For example, rotating 180 degrees can be achieved by applying transpose=1 twice or using a combination of transpose and flip filters. A common pattern for a full 180-degree inversion is:
ffmpeg -i input.mp4 -vf "transpose=1,transpose=1" -c:a copy output.mp4
Here, -c:a copy preserves the audio stream without re-encoding it, saving significant processing time. Only the video stream undergoes transformation. If you are dealing with high-resolution footage, consider adding -crf 23 (for H.264/H.265) to maintain a good balance between file size and quality during this re-encoding process.
Verifying the Fix
After running your chosen command, verify the output by playing it in multiple players. Web browsers, mobile devices, and desktop media players handle orientation differently. If one player shows the corrected video while another does not, check if the second player is ignoring metadata or if the filter application was incomplete.
For advanced users, inspecting the file properties with ffprobe can confirm the changes. Run the following command to view the stream details:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,codec_name -of default=noprint_wrappers=1:nokey=1 input.mp4
This helps ensure that the resolution dimensions have swapped correctly if a 90-degree rotation was applied. If you encounter issues with timestamp synchronization after heavy filtering, consider exploring related techniques such as those discussed in Fixing the 'ffmpeg -itsoffset is not working' Issue to manage timing offsets accurately.
Frequently Asked Questions
Why does my video look sideways on Instagram but normal on VLC?
Social media platforms often strip or ignore embedded rotation metadata tags during upload to standardize playback. VLC reads these tags natively, displaying the video correctly oriented. To fix this for social media, use the physical rotation method (Method 2) to bake the orientation into the pixel data permanently before uploading.
Can I rotate a video without losing quality?
Updating metadata tags is lossless and preserves original quality. However, physically rotating frames using filters requires re-encoding, which introduces generation loss. Using a high Constant Rate Factor (CRF) value, such as 18-23 for H.264, minimizes visible quality degradation during this process.
What if the audio is out of sync after rotating?
Physical rotation filters generally do not affect audio timing. If sync issues arise, they are likely due to other factors like variable frame rates or complex filter chains. Ensure you are copying the audio stream (-c:a copy) rather than re-encoding it unless necessary, as re-encoding can sometimes introduce minor delays if not handled carefully.
How do I rotate a video 180 degrees specifically?
Apply the transpose filter twice. The command -vf "transpose=1,transpose=1" rotates the video 90 degrees clockwise twice, resulting in a 180-degree flip. Alternatively, you can combine transpose with horizontal or vertical flip filters depending on the desired final orientation relative to the original capture.
Does ffmpeg support batch processing for multiple files?
Yes, you can use shell loops or ffmpeg's built-in glob patterns to process multiple files at once. For example, in Linux or macOS, you can iterate through all MP4 files in a directory and apply the rotation filter to each, creating a new output file for every input.