Today I decided to join the videos taken from my smartphone and make a single movie-like file with audio. But the thing is some videos are shot in portrait mode and some are shot in landscape mode. Before joining the files, I thought that it would be better to align every video files in the same orientation (i.e mode). Of course, there are many GUI-based tools and media players, like Smplayer, VLC, have a built-in feature to rotate videos. But I prefer a commandline utility. I know how to use FFMpeg to perform various operations such as converting files to another format, cropping, splitting and joining files and many. I already have compiled most commonly used FFMpeg commands for beginners and posted them in this guide. However, I do not know how to rotate videos using FFMpeg until today. If you ever wanted to rotate videos using FFMpeg, follow the steps given below.
Make sure you have the latest FFMpeg version installed on your system. FFMpeg is available in the default repositories of many popular Linux operating systems, so installation won't be a big deal. If you haven't installed FFMpeg on your Linux box, refer the following guide.
Rotate Videos Using FFMpeg From Commandline
FFMpeg has a feature called "Transpose" that is used to rotate videos. Using this feature, we can easily rotate videos clockwise and counter-clockwise as well as flip them vertically and horizontally.
For example, the following command will rotate the given video by 90 degrees clockwise:
$ ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
Or, use this command:
$ ffmpeg -i input.mp4 -vf "transpose=clock" output.mp4
Here, transpose=1 parameter instructs FFMpeg to transposition the given video by 90 degrees clockwise. Here is the list of available parameters for transpose feature.
- 0 - Rotate by 90 degrees counter-clockwise and flip vertically. This is the default.
- 1 - Rotate by 90 degrees clockwise.
- 2 - Rotate by 90 degrees counter-clockwise.
- 3 - Rotate by 90 degrees clockwise and flip vertically.
To rotate videos by 180 degrees clockwise, you to need to mention transpose parameter two times like below.
$ ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4
The above commands will re-encode audio and video parts of the given video file. If you don't want to re-encode the video and change the rotation in the metadata only, use this command instead:
$ ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=90 output.mp4
Even better, you can copy all the global metadata (such as date, camera details) of the input file to output file like below.
$ ffmpeg -i input.mp4 -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.mp4
Please note that it doesn't work in some players that can't handle rotation metadata. I changed the video orientation in metadata only and played it in Smplayer. But it kept playing the video in the previous orientation. But it worked in Gnome media player! I don't know why. So, if the video orientation didn't work, you may need to re-encode it as shown in the first command.