14

I need to produce timelapse video from a webcam that was mounted at a slightly off-vertical angle, and I need to rotate the image about 3 degrees counter-clockwise. (The webcam is in a virtually inaccessible location and due to weather may get kicked askew eventually even if we did fix the angle. So I need to fix it in software.)

I have had success using ImageMagick's convert tool with the command line option:

  convert infile.jpg -distort ScaleRotateTranslate 750,50,-3  outfile.jpg

but of course it is painfully slow to convert. I can do absolutely everything else I need to do (cropping and overlaying a logo on the image) using FFmpeg filters, but there doesn't seem to be a filter that allows rotating an image by an arbitrary angle, only by 90 or 180 degrees.

Perhaps there is some sort of generic linear transformation filter that can do this?

Thanks for any help.

Tom
  • 243
  • 1
  • 2
  • 4

1 Answers1

22

A rotate filter exists in FFmpeg, which allows rotation by an arbitrary angle.

To use it, you can download a recent snapshot build or build the lastest version from git.

The angle is specified in radians; positive is clockwise and negative is counterclockwise. If you have degrees, multiply by PI/180 to convert to radians. For example, to rotate 3° counterclockwise:

ffmpeg -i in.mp4 -vf "rotate=-3*PI/180" out.mp4

Check out the documentation for more details and additional examples.

slhck
  • 223,558
  • 70
  • 607
  • 592
mark4o
  • 5,382
  • 1
  • 35
  • 30
  • 1
    This looks promising. Definitely the answer I was looking for. I do wish there was a way to specify the center of rotation. I'll figure out a workaround by doing some translational displacement of the image before and after rotation to achieve the same effect. I'll try to remember to post a followup on what I find. – Tom Jul 06 '13 at 20:19
  • 1
    Irony is that the version of ffmpeg I was using, I built from sources on June 11, the same day the new rotate feature was added. If I had just waited a few hours I would have already had it! – Tom Jul 06 '13 at 20:36
  • 1
    It's hilarious that its not just degrees by default. I just tried "rotate=-4" and got some serious wacky result! – FinancialRadDeveloper Dec 07 '16 at 22:27
  • @FinancialRadDeveloper: see http://math.stackexchange.com/q/1952206 and linked/related questions – mark4o Dec 07 '16 at 23:56
  • 1
    I also do feel that while radians have mathematical significance and degrees don't, the latter are the actual unit practically used for measurement hence rather than forcing each user to write *PI/180 it should be built-in hence accepting input in degrees. – jamadagni Nov 14 '21 at 16:08