Rotating videos with FFmpeg

354,333

Solution 1

Rotate 90 clockwise:

ffmpeg -i in.mov -vf "transpose=1" out.mov

For the transpose parameter you can pass:

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

Use -vf "transpose=2,transpose=2" for 180 degrees.

Make sure you use a recent ffmpeg version from here (a static build will work fine).

Note that this will re-encode the audio and video parts. You can usually copy the audio without touching it, by using -c:a copy. To change the video quality, set the bitrate (for example with -b:v 1M) or have a look at the H.264 encoding guide if you want VBR options.

A solution is also to use this convenience script.

Solution 2

If you don't want to re-encode your video AND your player can handle rotation metadata you can just change the rotation in the metadata using ffmpeg:

ffmpeg -i input.m4v -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.m4v

Solution 3

Have you tried transpose yet? Like (from the other answer)

 ffmpeg -i input -vf transpose=2 output

If you are using an old version, you have to update ffmpeg if you want to use the transpose feature, as it was added in October 2011.

The FFmpeg download page offers static builds that you can directly execute without having to compile them.

Solution 4

To rotate the picture clockwise you can use the rotate filter, indicating a positive angle in radians. With 90 degrees equating with PI/2, you can do it like so:

ffmpeg -i in.mp4 -vf "rotate=PI/2" out.mp4

for counter-clockwise the angle must be negative

ffmpeg -i in.mp4 -vf "rotate=-PI/2" out.mp4

The transpose filter will work equally well for 90 degrees, but for other angles this is a faster or only choice.

Solution 5

I came across this page while searching for the same answer. It is now six months since this was originally asked and the builds have been updated many times since then. However, I wanted to add an answer for anyone else that comes across here looking for this information.

I am using Debian Squeeze and FFmpeg version from those repositories.

The MAN page for ffmpeg states the following use

ffmpeg -i inputfile.mpg -vf "transpose=1" outputfile.mpg

The key being that you are not to use a degree variable, but a predefined setting variable from the MAN page.

0=90CounterCLockwise and Vertical Flip  (default) 
1=90Clockwise 
2=90CounterClockwise 
3=90Clockwise and Vertical Flip
Share:
354,333
jocull
Author by

jocull

Specializing in Java, JVM, JavaScript, TypeScript, Node.js, Git, Linux, and Docker.

Updated on December 09, 2021

Comments

  • jocull
    jocull over 2 years

    I have been trying to figure out how to rotate videos with FFmpeg. I am working with iPhone videos taken in portrait mode. I know how to determine the current degrees of rotation using MediaInfo (excellent library, btw) but I'm stuck on FFmpeg now.

    From what I've read, what you need to use is a vfilter option. According to what I see, it should look like this:

    ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4
    

    However, I can't get this to work. First, -vfilters doesn't exist anymore, it's now just -vf. Second, I get this error:

    No such filter: 'rotate'
    Error opening filters!
    

    As far as I know, I have an all-options-on build of FFmpeg. Running ffmpeg -filters shows this:

    Filters:
    anull            Pass the source unchanged to the output.
    aspect           Set the frame aspect ratio.
    crop             Crop the input video to x:y:width:height.
    fifo             Buffer input images and send them when they are requested.
    format           Convert the input video to one of the specified pixel formats.
    hflip            Horizontally flip the input video.
    noformat         Force libavfilter not to use any of the specified pixel formats
     for the input to the next filter.
    null             Pass the source unchanged to the output.
    pad              Pad input image to width:height[:x:y[:color]] (default x and y:
     0, default color: black).
    pixdesctest      Test pixel format definitions.
    pixelaspect      Set the pixel aspect ratio.
    scale            Scale the input video to width:height size and/or convert the i
    mage format.
    slicify          Pass the images of input video on to next video filter as multi
    ple slices.
    unsharp          Sharpen or blur the input video.
    vflip            Flip the input video vertically.
    buffer           Buffer video frames, and make them accessible to the filterchai
    n.
    color            Provide an uniformly colored input, syntax is: [color[:size[:ra
    te]]]
    nullsrc          Null video source, never return images.
    nullsink         Do absolutely nothing with the input video.
    

    Having the options for vflip and hflip are great and all, but they just won't get me where I need to go. I need to the ability to rotate videos 90 degrees at the very least. 270 degrees would be an excellent option to have as well. Where have the rotate options gone?

  • jocull
    jocull over 13 years
    No, I have not. I didn't know it existed. I'll give that a shot.
  • jocull
    jocull over 13 years
    The transpose filter does not seem to exist in any of my FFmpeg builds. How am I supposed to add it?
  • jocull
    jocull about 13 years
    Thanks for the info! I was never able to actually get this working, as I generally have trouble building from source. I may see if I can get it working again now.
  • Peter Hansen
    Peter Hansen about 13 years
    In the version of the docs as of 2011-05-15 the correct link is now ffmpeg.org/ffmpeg-doc.html#SEC93
  • meduz
    meduz about 12 years
    the answer from @Alexy seems more relevant
  • srcspider
    srcspider over 11 years
    The video will still have the orientation information so now the video will be miss corrected on the iphone.
  • rwilliams
    rwilliams about 11 years
    It feels like i got punished for offering a useful answer 1.5 years before the accepted answer.
  • Usagi
    Usagi about 11 years
    So in order to rotate 180 degrees I would have to convert twice with "transpose=1"? I don't want the mirroring effect from using hflip.
  • Sadi
    Sadi over 10 years
    When I use this command, I get a low quality video output, unless -- as I've just discovered -- I use this parameter as well: -vcodec libx264. But it would be great if I didn't need to look up which encoding to use as ffmpeg should already know it. Any suggestions?
  • Alec Jacobson
    Alec Jacobson over 10 years
    Along the same lines as Sadi, is there a way to "copy" the quality of the original video?
  • Alex Reinking
    Alex Reinking over 10 years
    I would use the -a:c copy flag (goes after the -vf flag) to copy the audio stream. My phone's camera takes video using aac, which ffmpeg doesn't support by default on my distro.
  • LOlliffe
    LOlliffe over 10 years
    This was the only example that worked for me, but the quality was terrible, and very blocky. Also, it downsized a 1080x1920 video to 352x640. I guess I'm missing a switch or two. Any suggestions?
  • Andrew Schleifer
    Andrew Schleifer over 10 years
    @LOlliffe add -sameq
  • LOlliffe
    LOlliffe over 10 years
    @AndrewSchleifer Thanks. I tried that, but ffmpeg threw back at me Option 'sameq' was removed. If you are looking for an option to preserve the quality (which is not what -sameq was for), use -qscale 0 or an equivalent quality factor option. Failed to set value '1' for option 'sameq': Invalid argument -qscale also gives an error. Please use -q:a or -q:v, -qscale is ambiguous, but still works.
  • Alex Pliutau
    Alex Pliutau over 10 years
    For 180 you can use -vf "transpose=2,transpose=2"
  • Mikhail T.
    Mikhail T. over 10 years
    Ouch... This may work, but it is perfectly hideous. First of all, the general rule of shell-programming: you never need more than one of grep, cut, awk, or sed in a pipe. Any grep|cut|sed is doable with either sed or awk alone. Then, more specifically, exiftool can be asked to output just the tag(s) you want -- so instead of filtering out the Rotation, just "exiftool -Rotation". Third, you don't need so many "evals" and your "if test..." should be replaced with case $ROTATION in -- for both readability and efficiency. Good luck!
  • Nicky Smits
    Nicky Smits over 10 years
    @plutov.by Who transpose=2 and not transpose=1 twice?
  • Mathias Bynens
    Mathias Bynens almost 10 years
    @AlexReinking I think you meant -c:a copy.
  • Alex Reinking
    Alex Reinking almost 10 years
    Maybe I did... ffmpeg has some pretty fluid command-line flags. I posted that comment quite a while ago.
  • BitsAndBytes
    BitsAndBytes over 9 years
    This worked for Windows 7 64 bit machine, ffmpeg-20141221-git-41ee459-win64-shared -- fyi, ffmpeg.exe wanted to take nearly 100% of the resources, so i adjusted the affinity on ffmpeg.exe in the processes tab of windows task manager to only use 2 cores instead of 8 (right-click the process)
  • Jonas Borggren
    Jonas Borggren over 9 years
    How would this work with more than 1 filter? -vf transpose=2 crop=20:20? Because -vf transpose=2 -vf crop=20:20 ignores the rotation (transpose). Edit: -vf transpose=2 crop=20:20 did not work.
  • rwilliams
    rwilliams over 9 years
    @JonasB Separate the filters with commas. See stackoverflow.com/questions/6195872/…
  • MonsterMMORPG
    MonsterMMORPG over 9 years
    @acron where we put it ?
  • Antony Woods
    Antony Woods over 9 years
    @MonsterMMORPG ffmpeg -i in.mov -qscale 0 -vf "transpose=1" out.mov BUT read the man pages, as some codecs treat this differently. For example, when I recently worked with Theora, I had to use -qscale 28 as it ignored 0 and wanted a 1 - 31 value.
  • patrick
    patrick almost 9 years
    that is becuase you're applying the filter to the wrong file... try ffmpeg -i input.mp4 -vf "rotate=90" output.mp4, then it'll work
  • user1438038
    user1438038 over 8 years
    Did you mean "Ubuntu version of ffmpeg does not support videofilters"?
  • l --marc l
    l --marc l over 8 years
    FYI: "FFmpeg has returned in Ubuntu 15.04 Vivid Vervet." or can be compiled for Ubuntu. --> trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
  • l --marc l
    l --marc l over 8 years
    Current docs note that "Numerical values are deprecated, and should be dropped in favor of symbolic constants." ffmpeg.org/ffmpeg-filters.html#transpose Thus cclock_flip, clock, cclock or clock_flip instead of 0, 1, 2 or 3.
  • l --marc l
    l --marc l over 8 years
    "Rotate video by an arbitrary angle expressed in radians." Documentation: ffmpeg.org/ffmpeg-filters.html#rotate So, radians rotate=PI/2 or rotate=90*PI/180 is needed
  • Angie Quijano
    Angie Quijano over 8 years
    Great Alexy, it worked for me too. I tested it in Windows 8.1
  • lidaobing
    lidaobing over 8 years
    how about check the rotate metadata first with ffmpeg -i input.m4v 2>&1 | grep rotate
  • OrangeDog
    OrangeDog over 8 years
  • Sun
    Sun about 8 years
    this worked for me. I want to upload Clash Royale to YouTube. It rotated the video 90 degrees without affecting quality.
  • eqzx
    eqzx about 8 years
    this is great. I found that it's possible to get finer-grained resolution of radians, because * behaves as multiplication: ffmpeg -i in.avi -vf "rotate=-8*PI/40" -q 1 out.avi (slightly less rotation than -PI/4 = -10*PI/40)
  • stuzzo
    stuzzo about 8 years
    If you have an updated version of ffmpeg, it autorotates the video see superuser.com/questions/578321/…
  • migle
    migle almost 8 years
    This is the best answer by far. But there is a small improvement to do. To avoid loosing the remaining meta-data (such as date, camera) on the video do ffmpeg -i input.m4v -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.m4v. This way all global metadata on the input file will be copied as global metadata to output file and only the rotation meta-data is changed.
  • McVitas
    McVitas almost 8 years
    "transpose=2,transpose=2" - does this mean the rotation is done twice?? Meaning it will take double the time?
  • mivk
    mivk over 7 years
    If you have a portrait-mode video and just want to "UN-rotate" it to have a standard 1920x1080, then you probably want rotate=0.
  • Kalu Khan Luhar
    Kalu Khan Luhar over 7 years
    How can we achieve this in objective c code. We cant use "ffmpeg -i in.mov -vf "transpose=1" out.mov".
  • anon
    anon over 7 years
    Thank you, @mivk!
  • febot
    febot about 7 years
    Tried -metadata:s:v rotate="180", didn't work. Is that supposed to work?
  • Andreas
    Andreas about 6 years
    IMHO best solution because no reencoding is necessary and most video player support metadata rotation. Also Cloud services like Google Photos. However, remember that ffmpeg does not necessarily copy all metadata from the original file! Therefore, I would recommend to explicitly specify to copy all other metadata from original file: ffmpeg -i input.mp4 -codec copy -map_metadata 0 -metadata:s:v:0 rotate=0 output.mp4
  • SilverWolf
    SilverWolf almost 6 years
    @KaluKhanLuhar Sure you can! You can use NSTask (or the Swift equivalent, Process). Note that this may not work in a sandboxed app.
  • SilverWolf
    SilverWolf almost 6 years
    avconv does (in 14.04), and seems to work exactly the same. (But for later versions, definitely use FFmpeg.)
  • Reino
    Reino almost 5 years
    If you're looking for a "NTSC-film" framerate, then 24000/1001 would be more accurate.
  • Slipp D. Thompson
    Slipp D. Thompson almost 5 years
    No dice: [Parsed_pad_2 @ 0x7f8b15c3a580] Input area -420:0:1500:1080 not within the padded area 0:0:1080:1080 or zero-sized\n ` [Parsed_pad_2 @ 0x7f8b15c3a580] Failed to configure input pad on Parsed_pad_2\n Error reinitializing filters!\n Failed to inject frame into filter network: Invalid argument\n Error while processing the decoded data for stream #0:0\n Conversion failed!
  • arkon
    arkon over 4 years
    Not a proper solution since not all video players will honor the rotation. E.g. Snapchat (via the Camera Roll.)
  • Rodrigo Polo
    Rodrigo Polo over 4 years
    As it was stated in the answer: "If you don't want to re-encode your video AND your player can handle rotation metadata"
  • jocull
    jocull about 4 years
    Great information about v4l2 here. Rotating to the correct orientation at recording time is obviously preferable :) This almost 10 year old question (wow!) was originally asked about videos being uploaded from iOS devices if that helps provide any context :)
  • J.M.
    J.M. about 4 years
    I'm a bit new in this community and this question is much older then my seniority here... I truly think it's a good (and common) question, so I believe this old question will keep helping many FFmpeg users.
  • somenxavier
    somenxavier about 4 years
    The degrees are countwise, not anticountwise, like mathematicians do
  • Daniel Kobe
    Daniel Kobe about 4 years
    transpose=3 is causing the image to mirror
  • Daniel Kobe
    Daniel Kobe about 4 years
    transpose=2 worked without mirroring for my 270 rotated video
  • jocull
    jocull over 3 years
    Note that the fast command is only adding video metadata, which may already be present in many sources. The issue will be whether or not the player rendering the video respects the metadata flag or not :)
  • Sisir
    Sisir over 3 years
    Why my video size increase after transposing?
  • bmaupin
    bmaupin over 3 years
    @rwilliams While this is a great answer and was 1.5 years before Alexy's answer, it has the disadvantage that it doesn't include as much information on what transpose does or how to use it. It's great that you linked to it, but the disadvantage to linking to the pertinent info is that (as in this case) the link no longer points to the proper documentation. If nothing else I'll submit an edit to fix the link.
  • bmaupin
    bmaupin over 3 years
    This works great for Google Photos! However it should be noted ffmpeg won't copy all metadata and will leave out some metadata that might be useful (e.g. camera model). You can copy metadata after the fact using exiftool (video.stackexchange.com/a/28599/34178), e.g. exiftool -TagsFromFile input.mov "-all:all>all:all" output.mov (note that exiftool is smart enough not to overwrite the newly added rotation metadata)
  • Ste
    Ste almost 3 years
    @Sisir, mine is decreases in quality. Yet to find a solution to this.
  • Raleigh L.
    Raleigh L. about 2 years
    "Note that this will re-encode the audio and video parts" This disclaimer should have been at the beginning. No one wants to have their video quality suffer or wait an eternity and a half for their video to be encoded.