Skip to content

Instantly share code, notes, and snippets.

@amandaghassaei
Last active January 3, 2024 01:11
Show Gist options
  • Save amandaghassaei/9ba9949b34302929231aee437a89dd40 to your computer and use it in GitHub Desktop.
Save amandaghassaei/9ba9949b34302929231aee437a89dd40 to your computer and use it in GitHub Desktop.
Golden Angle Animation using ffmpeg

Golden Angle Animation using ffmpeg

Update: I've turned this into a web app!

This is a single line command to generate a "golden angle" animation by rotating an image in increments of 137.5 degrees. Try this out on images of pinecones, succulents, flowers, fruits, and vegetables – anything that exhibits fibonacci spirals. I'll post some examples in the comments below.

I came across this idea while reading about the artist John Edmark, who creates hypnotizing zoetrope sculptures. He said in an interview that you could take a pineapple or pinecone and put it on a turntable with a strobe light for the same effect, and I wanted to test it out.

Steps To Use:

Install ffmpeg and run the following command:

FILE="image.jpg"; CENTER_X="1000"; CENTER_Y="1000"; ffmpeg -loop 1 -i $FILE -r 10 -vframes 157 -filter_complex "crop=min(iw-2*abs(iw/2-$CENTER_X)\,ih-2*abs(ih/2-$CENTER_Y)):out_w:$CENTER_X-out_w/2:$CENTER_Y-out_h/2" -c:v libx264 -preset slow -crf 22 -pix_fmt yuv420p -an "${FILE%.*}_golden_tmp.mp4"; ffmpeg -i "${FILE%.*}_golden_tmp.mp4" -filter_complex "rotate=a=n*2.4:c=black:ow=trunc(1/sqrt(2)*iw/2)*2:oh=ow" -c:v libx264 -preset slow -crf 22 -pix_fmt yuv420p -an "${FILE%.*}_golden.mp4"; rm "${FILE%.*}_golden_tmp.mp4"

At the beginning of the line:

  • Set FILE to your image (can be any image format).
  • Set CENTER_X and CENTER_Y to the x and y position to rotate around (measured in pixels with (0, 0) at the top left corner of the image). For a perfectly centered image CENTER_X is width / 2 and CENTER_Y is height / 2. I use the ruler tool in Photoshop to measure the location of the center point.

More Info:

  • -loop 1 -r 10 -vframes 157 creates a video from the image with a framerate of 10 frames per second and a total 157 frames. 157 is a good number to use because 157 * 2.4 radians / 6.28 radians = 60 rotations (this will give you a perfectly looping video).
  • -filter_complex "crop=min(iw-2*abs(iw/2-$CENTER_X)\,ih-2*abs(ih/2-$CENTER_Y)):out_w:$CENTER_X-out_w/2:$CENTER_Y-out_h/2" crops the video to a square, centered around the desired center point.
  • -filter_complex "rotate=a=n*2.4:c=black:ow=trunc(1/sqrt(2)*iw/2)*2:oh=ow" rotates each frame of the video by 2.4 radians (137.5 degrees) and crops to a tight fit for all rotated frames. There is some extra code in there to ensure that the resulting width and height of the output video are divisible by 2 (required for mp4). You might also try a=-n*2.4 to rotate the frames in the opposite direction. I think some plants have other special angles, but I'm still figuring that out... let me know in the comments if you find something!
  • -c:v libx264 -preset slow -crf 22 encodes as h.264 with better compression settings.
  • -pix_fmt yuv420p makes the mp4 compatible with web browsers.
  • -an creates a video with no audio.
  • rm "${FILE%.*}_golden_tmp.mp4" removes a temp file created in this process. I would like to avoid temp files, but found that ffmpeg has some kind of bug that was causing duplicated frames or inconsistent rotation between frames when I ran this as a single command. Let me know in the comments if you know a better way.
@amandaghassaei
Copy link
Author

amandaghassaei commented Dec 22, 2021

ezgif com-gif-maker (1)

succulent: original image by Dennis Hill on flickr: https://www.flickr.com/photos/fontplaydotcom/506443759/

ezgif com-gif-maker

pinecone: original image by Kai Schreiber on flickr: https://www.flickr.com/photos/genista/4140485

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