Skip to content

Instantly share code, notes, and snippets.

@ViktorNova
Created August 8, 2016 21:33
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ViktorNova/1dd68a2ec99781fd9adca49507c73ee2 to your computer and use it in GitHub Desktop.
Save ViktorNova/1dd68a2ec99781fd9adca49507c73ee2 to your computer and use it in GitHub Desktop.
Rotate a video with FFmpeg (100% lossless, and quick)
$INPUTVIDEO='input.mp4'
$OUTPUTVIDEO='output.mp4'
ffmpeg -i $INPUTVIDEO -metadata:s:v rotate="-90" -codec copy $OUTPUTVIDEO
@michaelahlers
Copy link

Finally! A concise answer which is also happens to be the correct answer.

@bolds07
Copy link

bolds07 commented May 8, 2018

can you explain what does metadata parameter do? is it only changing file meta?

what is the difference between this and:
ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4

@Adorfer
Copy link

Adorfer commented Jun 3, 2018

Negative degree values do not seem to work under all circumstances. I have to use (+)90 degrees, which seems to work in all cases (YMMV)

@banderlog
Copy link

It is just writes right metadata.
If your videoplayer does not support it, or you'll open 'converted' video with OpenCV -- you will see no difference.

@stevems522
Copy link

It doesn't seem to work with webm format vids?

@TizianoCoroneo
Copy link

TizianoCoroneo commented Jul 24, 2019

Negative degree values do not seem to work under all circumstances. I have to use (+)90 degrees, which seems to work in all cases (YMMV)

You have to use 270 instead of -90

It is just writes right metadata.

If you want to actually change the orientation of the pixel matrix you can use the transpose filter.

If your videoplayer does not support it, or you'll open 'converted' video with OpenCV -- you will see no difference.

That's correct, but it will work with transpose. Problem is: it will be incredibly slower than just modifying the metadata.

@jayadevanraja
Copy link

How to do hflip or horizontal flip in a similar, lossless manner?

@beterhans
Copy link

it works thanks so much!

@udippel
Copy link

udippel commented Apr 3, 2020

Alas, doesn't do anything here. Processes for some seconds, creates the output file, and playing with VLC is still upside down.
My command is:
ffmpeg -i VID_20200403_123919.mp4 -metadata:s:v rotate="270" -codec copy LDDS_Lab_raw.mp4

I guess VLC doesn't care?

@KaMyKaSii
Copy link

How to do hflip or horizontal flip in a similar, lossless manner?

I also want to know it

@TizianoCoroneo
Copy link

Have you tried this example using the geq filter? And what exactly is the issue with hflip?

@ozmartian
Copy link

i tried hflip on a video and it made no difference :-( not your fault, i should be complaining to FFmpeg which i will now do.

@Melirius
Copy link

It works, but only if the format of video supports rotation: OK for mp4, do nothing for WebM.

@rokezu
Copy link

rokezu commented Aug 14, 2022

In order to keep all metadata, for example creation_time, which was dropped in my video with the above command, consider these parameters:

ffmpeg -i $INPUTVIDEO -movflags use_metadata_tags -metadata:s:v rotate="0" -map_metadata 0 -codec copy $OUTPUTVIDEO

Also, if a file is already rotated (ie, already portrait at -90 or 90) and you want to get it in landscape, use 0 degrees like the example above.

And, exiftool can rotate the metadata attribute in-file, I prefer this method as it doesn't introduce any changes to the file like the "encoded with lavc" or so of ffmpeg, it just flips a few bits and that's it:

exiftool -rotation=0 $INPUTVIDEO

@S-M-R-Sadeghi
Copy link

S-M-R-Sadeghi commented Apr 10, 2023

Changing the mp4 files by changing the Metadata(Rotation) Flag is the great way, but it occures the wrong result when you decide to join that file, with some other files with Rotation=0 flag. So that times, you have not any choice other that use the -vf "transpose=[0|1|2|3]" command with ffmpeg program.

@MgFrobozz
Copy link

MgFrobozz commented Sep 8, 2023

I had no luck with this when using mplayer or a server's html5 player to play a video encoded on an iphone in portrait mode. Both the original clip, and the clip encoded using ffmpeg with the 'rotate="90"', displayed video rotated -90 degrees (top of the picture on the left)

This is likely related to how iphones rotate clips: the material is always encoded with width greater than height; the track header, or "tkhd", for the video track indicates width=1920 and height=1080 for this portrait-recorded clip. Apple attempts to signal rotation to the decoder (in this case mplayer or the html5 player) by using the "matrix" values in the "tkhd" to indicate that the decoder must rotate the video by 90 degrees. This clip uses a matrix which does that, rather than a unity matrix that would indicate no rotation.

However, the international standard for the video (ISO/IEC 14496-12 2015, section 8.3.2.3) indicates that the "matrix" values must be a unity matrix ...

matrix provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex (0,0,0x40000000)

In other words, ISO (International Standards Organization) does not allow the encoder to use the matrix for the rotation. Because of that, an ISO-compliant decoder can ignore the video rotation. Players may optionally support it (ffplay does), at the expense of computation complexity (cpu cycles).

I've dealt with this issue in web servers by always fully transcoding any video that's uploaded, using ffmpeg (which, like ffplay, is based on libavformat, which provides the rotation). This resets the tkhd matrix to a unity matrix.

@willwillis
Copy link

I had success with this
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment