[Quick Tip] Single Command to Rotate a Video in Ubuntu Linux

Last updated: February 9, 2023

Got a video playing upside down? Here’s an easy way to rotate it via a single command in Ubuntu.

There are a few video players, e.g., SMPlayer, support for rotating by 90 degrees clockwise or counter-clockwise during video playback.

If you want to make it permanent by exporting video rotated, besides using a heavy video editing tool, e.g., Pitivi and Openshot, the single command in this tutorial may help.

An upside down video

1. Install FFmpeg:

Firstly install FFmpeg if you don’t have it. FFmpeg is a large suite of libraries and programs for handling multi-media files and streams.

It is very popular and most likely already installed on your system, if you have any audio, video, and other multimedia relevant applications installed.

To make sure, open terminal (Ctrl+Alt+T) and run command:

2. Command to rotate video:

Now you can run the single command to rotate a video:

ffmpeg -i input-video.mp4 -vf "transpose=1" -acodec copy output-video.mp4

Before this command, you may first navigate to the video folder either via cd command (e.g., cd ~/Videos), or in file browser go to the folder and right-click blank area and select “Open in Terminal”.

In the command, the number in “transpose=1” can also be:

  • 0 – means rotate by 90 degrees counterclockwise and flip
  • 1 – means rotate by 90 degrees clockwise
  • 2 – means rotate by 90 degrees counterclockwise
  • 3 – means rotate by 90 degrees clockwise and flip

(Thanks to Roman Sheydvasser) Add -c copy (or -codec copy) will copy all the frames instead of doing decode -> filter -> encode process. It will speed up the command quite a lot!

In my case, the command is:

ffmpeg -i ~/Videos/aisha.mp4 -vf "transpose=1" -acodec copy ~/Videos/aisha-rotated.mp4

This command however will re-encode the video. Depends on the video size and your CPU, the process may take a few minutes.

Optional

The last command can take quite a few minutes since it needs to re-encode the video. As a workaround, user can use this command instead to do the rotation in the metadata.

fmpeg -i input-video.mp4 -map_metadata 0 -metadata:s:v rotate="90" -codec copy output-video.mp4

The command is fast and will work for video players (such as VLC and MPV) that support can handle rotation metadata.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

10 responses to [Quick Tip] Single Command to Rotate a Video in Ubuntu Linux

  1. Awesome.
    I could have held the phone landscape to the binocular. I hardly ever take video. Of course HD is horizontal-is-wide. I could have guessed.
    My 11 year old desktop took 3.5 minutes to process a 43 second clip. I bet it would be faster if i got ffmpeg to use the video card. The video card can scale a video while displaying it and still be mostly idle. Oh look. A 23 page PDF on how to do that.
    https://developer.download.nvidia.com/compute/redist/ffmpeg/1511-patch/FFMPEG-with-NVIDIA-Acceleration-on-Ubuntu_UG_v01.pdf

  2. Roman Sheydvasser May 25, 2021 at 8:57 pm

    The command in the article would have taken about 20 minutes to rotate my 20 minute, 2gb video file. This command took about 10 seconds:

    ffmpeg -i input-video.mov -metadata:s:v rotate=”-90″ -codec copy output-video.mov

  3. I used your command. But the output file loses a lot of resolution.

    Do you know how to keep the original resolution?

    Thanks.

  4. My ffmpeg does not allow -vf and -c copy in the same command – it’s one or the other.

    • Try replace -c copy with -acodec copy to only copy audio codec, and let it re-encode the video. Though, the command will take longer time!

      • I left out the -c and it was fine (I have a Ryzen 9 5950x CPU, so speed is not an issue).

        The interesting (to me) part was that without any -vf, ffmpeg rotated the video to the proper orientation – changed from .mov to .mp4, no sweat.

        With the -vf flag, it either rotated 180 degrees (transpose=1) or not at all (transpose=2). I suspect it’s due to what I said above – ffmpeg knows what the orientation should be (or really is) and rotates from there, not from where it appears to be when played.

  5. is there a way to rotate all videos in a folder?

    • Of course.

      For example, run command below to use ‘for’ loop, to do the rotate command for all files with ‘.mp4’ extension in your Videos folder.

      cd ~/Videos && for file in *.mp4; do ffmpeg -i $file -vf "transpose=1" -acodec copy ${file%.mp4}-rotated.mp4; done

      You can also do this for all files in a certain folder. Just replace “~/Videos” with destination folder. Replace “*.mp4” with “*” for all files. Also edit ${file%.mp4}-rotated.mp4 accordingly to specify the output files’ name.