Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved quality of GIFs #1144

Merged
merged 1 commit into from Mar 24, 2021
Merged

Improved quality of GIFs #1144

merged 1 commit into from Mar 24, 2021

Conversation

WampyCakes
Copy link
Contributor

@WampyCakes WampyCakes commented Mar 22, 2021

Motivation

Currently, the GIFs generated by manim are fairly ugly. They have very large circular-looking pixels and are very slow. This PR fixes that insofar as I've tested.

Overview / Explanation for Changes

The only thing I have changed is the ffmpeg command to create a GIF. There are three improvements here:

  1. FPS is now set in the FFMPEG command so that GIFs are faster. However, the max is 50, otherwise it starts to become slower again.
  2. FFMPEG now uses palletegen and palleteuse to fix the issue with colors and "large pixels."
  3. stats_mode=diff combined with dither=bayer:bayer_scale=5:diff_mode=rectangle aid in making the GIF look better. Currently if a scene would benefit from different settings, that will have to be edited directly in scene_file_writer.py

In order to reduce the size of a GIF, you can either lower the frame rate, lower the resolution (and keep the same frame rate or change it), scale the GIF, etc.

Testing Status

I am not aware of any tests existing for GIFs in manim. I'd like to add some, but I am not sure if this is possible/how?

Further Comments

There are probably edge cases that still don't look too good with GIFs. For this, I will make a new issue with what I think should be a next step in the right direction.

Examples

Due to the inherent large size of GIFs and the file size limit of GitHub, I can only include small examples like these. Here you can see a GIF generated the old way, and then a new one.
Old_SquareToCircle_ManimCE_v0 4 0
New_SquareToCircle_ManimCE_v0 4 0

Acknowledgements

  • I have read the Contributing Guidelines
  • I have chosen a descriptive PR title (see top of PR template for examples)

Reviewer Checklist

  • Newly added functions/classes are either private or have a docstring
  • Newly added functions/classes have tests added and (optional) examples in the docs
  • Newly added documentation builds, looks correctly formatted, and adds no additional build warnings
  • The PR title is descriptive enough

@WampyCakes
Copy link
Contributor Author

(I'll make the new issue later)

@behackl
Copy link
Member

behackl commented Mar 22, 2021

For the examples of this change that you have tested: in how far does the file size of the output gifs change? I can see that for the example above, the new gif is approx. 200kb larger than the old one (which seems okay to me, given that there is a respectable change in the output quality).

@WampyCakes
Copy link
Contributor Author

@behackl In the limited cases I tried, it's almost double, though it probably depends on the GIF. I find this justifiable considering that how they currently render is just unacceptable imo.

So that leaves us with this tradeoff: quality vs. file size:
With this change, the quality, along with the file size, go considerably up. The way then that we reduce file size is not to omit this change. We can still reduce the quality with this change to lower the file size, and it will look better than without the change. One way this is possible (which I tested) is by lowering the frame_rate. So if you keep the resolution the same but lower the frame_rate, it will reduce file size. I did a test where I rendered the OpeningManim scene before and after this change. Here's what I found:

  1. Before change
    • Rendered at 50 fps (the max for these GIFs): ~22 MB
    • Rendered at 25 fps (half): ~11 MB
  2. After change
    • Rendered at 50 fps: ~48 MB
    • Rendered at 25 fps: ~22 MB

So we essentially see an ~50% decrease in file size by halving the frame_rate. There are other ways to do this as well. We can use the scale flag in ffmpeg or lower the resolution (I'll address these in a separate issue). Or, there's probably also ways to compress the GIF in general, but that's outside my knowledge.

@huguesdevimeux
Copy link
Member

Concerning tests of this :
There is a way to test video in manim here

I think you could try to use it for gif. I don't know if it's possible, but it's worth a try. (i'm fine if there is no direct test for that, so don't put too much effort here)

Copy link
Member

@huguesdevimeux huguesdevimeux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SGTM, and I'm fine with the +50% size, since people will generally not use a gif at 50 fps imo.

Please re-request a review from me if you decide to implement tests.

@jsonvillanueva jsonvillanueva added the enhancement Additions and improvements in general label Mar 23, 2021
@jsonvillanueva jsonvillanueva changed the title Enhancement: Improve quality of GIFs Improved quality of GIFs Mar 24, 2021
@jsonvillanueva jsonvillanueva merged commit 74ce5b7 into ManimCommunity:master Mar 24, 2021
@behackl behackl added this to the Release v0.5.0 milestone Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants