Understanding Rotation Metadata vs. Pixel Data
Modern MP4 files created by smartphones and cameras rarely store the physical pixel data rotated. Instead, they store a rotate tag in the stream metadata indicating how the player should orient the image. For example, a phone held vertically might save a horizontal video but mark it as needing a 90-degree clockwise rotation. When FFmpeg processes these files individually, players respect this tag. However, when concatenating multiple clips, many older methods ignore this metadata, leading to the final output appearing in the wrong orientation.
This issue is distinct from actual pixel manipulation. If you look at the raw frame dimensions, a landscape video will still have width greater than height, even if the metadata says it should be portrait. This distinction is critical because re-encoding to "bake" the rotation changes the file size and quality, while metadata-aware concatenation preserves the original bitrate and speed.
Solution 1: Re-encoding with Transpose Filters
The most reliable way to ensure consistent orientation across concatenated segments is to normalize all input videos before joining them. This involves re-encoding each clip so that the rotation is physically applied to the video frames. You can achieve this using the transpose filter within an FFmpeg complex filter graph.
To rotate a video 90 degrees clockwise, you typically use transpose=1. Note that earlier guides sometimes incorrectly suggested transpose=0 for simple rotations, but transpose=0 actually performs a 90-degree counter-clockwise rotation followed by a vertical flip, which is not what you want for standard landscape-to-portrait conversion. The correct values are:
transpose=1: Rotate 90 degrees clockwise.transpose=2: Rotate 90 degrees counter-clockwise.transpose=3: Rotate 90 degrees counter-clockwise and flip vertically.transpose=4: Rotate 90 degrees clockwise and flip vertically.
You can apply this to a single file using the following command structure:
ffmpeg -i input.mp4 -vf "transpose=1" -c:a copy output_rotated.mp4
After rotating all individual clips, you can safely concatenate them using any standard method, as the orientation is now part of the pixel data. This approach is ideal if you need the final file to be compatible with legacy players that do not read rotation tags.
Solution 2: Using the Concat Demuxer with Metadata
A more efficient solution, which avoids re-encoding entirely, is to use the FFmpeg concat demuxer with the safe option and ensure your inputs are properly formatted. While the basic concat protocol ignores metadata, the demuxer can handle it better if the files are prepared correctly. However, even the demuxer may strip rotation tags depending on the container format and encoder settings.
If you prefer not to re-encode, you can try forcing the preservation of metadata during the concat process. Use a text file listing your inputs:
file 'clip1.mp4'
file 'clip2.mp4'
Then run the concat command:
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
In some cases, this preserves the last file's metadata, but it is inconsistent. For robust results without re-encoding, consider converting all files to a format that supports metadata well, such as MKV, concatenate, and then convert back to MP4. Alternatively, use the -metadata:s:v rotate=0 flag to explicitly set or clear rotation metadata after concatenation if the player ignores it.
Common Pitfalls and Debugging
One common mistake is assuming that copy mode always preserves all aspects of the original file. While it copies audio and video streams, it does not always carry over complex side-data like rotation tags unless the container supports it natively and the muxer is configured to pass it through. Always check the output file properties using ffprobe to see if the rotation tag exists in the new file.
Another pitfall is mixing codecs. If one video is H.264 and another is H.265, direct concatenation is impossible without re-encoding. Ensure all source videos have identical codecs, resolutions, and frame rates. If you are dealing with varying resolutions, you must scale them to a common dimension before concatenation, which further necessitates re-encoding. In such cases, combining scaling and rotation in one step is efficient.
For users experiencing issues where the video plays correctly in VLC but not in web browsers, the issue is almost certainly the missing rotation metadata. Browsers often ignore the rotate tag in MP4 containers. In this scenario, re-encoding with the transpose filter is the only viable solution.
Advanced Filtering for Complex Scenarios
If you need to perform more complex operations, such as overlaying graphics while rotating, you can chain filters. For instance, if you need to scale the video to fit a specific aspect ratio after rotation, you can add a scale filter:
ffmpeg -i input.mp4 -vf "transpose=1,scale=1080:1920" -c:a copy output.mp4
This ensures that the video is both rotated and resized in a single pass. Be aware that this process is CPU-intensive and will take longer than simple concatenation. If you have many large files, consider processing them in batches or using hardware acceleration with -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 if available on your system.
For related issues involving overlays or character rendering, refer to our guide on FFmpeg: Scale2Ref Overlay Complex Filters Returning Audio-Only MP4 Output to understand how complex filter graphs interact with audio streams. Additionally, if you suspect corruption in your source files causing rotation failures, check out FFmpeg Fails to Process MP4 File Despite VLC Media Player Success for diagnostic steps.
Frequently Asked Questions
Why does my concatenated video appear sideways?
This happens because the rotation information is stored in the metadata header, not in the pixel data itself. When files are joined, the metadata from the first file is often kept, or ignored entirely, leaving the viewer with the original unrotated pixel orientation.
Is transpose=0 the correct filter for 90-degree rotation?
No, transpose=0 rotates 90 degrees counter-clockwise and flips the image vertically. For a standard 90-degree clockwise rotation, use transpose=1.
Can I concatenate videos without re-encoding?
Yes, but you must ensure all videos have identical codecs and parameters. However, note that rotation metadata may not be preserved reliably in all container formats during a lossless concat operation.
How do I check if a video has rotation metadata?
Use the command ffprobe -v error -select_streams v:0 -show_entries stream_tags=rotate -of default=noprint_wrappers=1:nokey=1 input.mp4. If it returns a value like 90, 180, or 270, the video relies on metadata for orientation.
Why does VLC play the video correctly but Chrome does not?
VLC is very permissive and automatically applies rotation metadata. Web browsers often ignore the rotate tag in MP4 files due to HTML5 video specification ambiguities, requiring the rotation to be baked into the pixels via re-encoding.