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

Add moviepy 2 compatibility #747

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

Daraan
Copy link

@Daraan Daraan commented Jan 15, 2025

Currently moviepy v2+ cannot be used to add videos because the content of moviepy.editor was refactored to moviepy.__init__.py.

This PR now uses just from moviepy import ImageSequenceClip instead of moviepy.editor.ImageSequenceClip.
Furthermore as fallback to moviepy < 2 it will use the direct location from moviepy.video.io.ImageSequenceClip import ImageSequenceClip instead via moviepy.editor which would import more modules and dependencies like requests which are not actually necessary and listed in the current error message.

@Daraan
Copy link
Author

Daraan commented Jan 15, 2025

On second though using from moviepy.video.io.ImageSequenceClip import ImageSequenceClip without using headfiles might be even better.

@lanpa
Copy link
Owner

lanpa commented Jan 15, 2025

For the python 3.11 error, I might have misconfigured the coverage test token. Let me check.

@Daraan
Copy link
Author

Daraan commented Jan 15, 2025

I've looked at the test and think they do not cover moviepy right?

I've tested v2 vs v1; with the setup test it fails downstream in imageio/plugins/pillow.py when it should write the gif with one color channel

ValueError: Can't write images with one color channel.

Converting this back to a draft and checking what changed

@Daraan Daraan marked this pull request as draft January 15, 2025 17:19
@Daraan
Copy link
Author

Daraan commented Jan 16, 2025

Thank you for the update.

I think there are about possible ways to get around the problem. There is one thing that confuses me though.
If I run the test with moviepy==1.0.3 imageio==2.27 protobuf==4.22.3 the test fails due to a mismatch with test_summary.test_video.expect.

For moviepy v2 the reason is that the test gif is saved as RGB rather than grayscale making the file bigger, why the 1.0.3 is different from the current version I cannot tell.

@lanpa could you maybe verify if you would generate a different file nowadays as well?


I found different way that allows to pass a 2D grayscale tensor in moviepy v2 as well but that solution is more involved


Minor note: The last commit theoretically allows to run the make_video code to run on imageio > 2.27 as well.

@Daraan Daraan marked this pull request as ready for review January 16, 2025 15:30
Copy link
Author

@Daraan Daraan left a comment

Choose a reason for hiding this comment

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

I now modified the tests to not depend on the imageio/pillow versions.

tensorboardX/summary.py Outdated Show resolved Hide resolved
return
try:
from moviepy import editor as mpy
# moviepy v2+
from moviepy import ImageSequenceClip
Copy link
Owner

@lanpa lanpa Jan 19, 2025

Choose a reason for hiding this comment

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

In line 393, using the Version function is a good idea. Instead of trying to import a specific package, should we also use it here to explicitily determine the version of moviepy 1 and moviepy 2?

Copy link
Author

Choose a reason for hiding this comment

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

I've reworked the version parsing to use importlib.metadata instead. Im undecided what to do with the actual import. I would like to avoid using from moviepy.editor import ... as this might fail due to a missing requests module.

We could just use from moviepy.video.io.ImageSequenceClip import ImageSequenceClip as it will not enter the except clause with moviepy v1. Just in the event of another moviepy refactoring that statement will break.

def _iter_gif(encoded_image):
import io

from PIL import Image, ImageSequence
Copy link
Owner

Choose a reason for hiding this comment

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

Can this be imported at the beginning of the file?

Copy link
Author

Choose a reason for hiding this comment

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

Yes they should be installed with the test requirements. I put it down there as I was not 100% sure if pillow will be installed with the normal setup.

@lanpa
Copy link
Owner

lanpa commented Jan 19, 2025

Hi, I have tested the newest commit with moviepy==2.1.2 (with python examples/demo.py). But in the tensorboard webpage, the gif is a static image. If I run with moviepy==1.0.3, it's normal. What should I do? Thanks!

@Daraan
Copy link
Author

Daraan commented Jan 19, 2025

Hi, I have tested the newest commit with moviepy==2.1.2 (with python examples/demo.py). But in the tensorboard webpage, the gif is a static image. If I run with moviepy==1.0.3, it's normal. What should I do? Thanks!

Thank you for checking.
This might be related imageio/imageio#974

Can you tell me you imagio and pillow versions.

@@ -369,25 +368,49 @@ def video(tag, tensor, fps=4, dataformats="NTCHW"):

def make_video(tensor, fps):
try:
Copy link
Author

Choose a reason for hiding this comment

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

should I move the imports to the top of the file? I am not sure how you like to handle it.

tensorboardX/summary.py Outdated Show resolved Hide resolved
@lanpa
Copy link
Owner

lanpa commented Jan 20, 2025

Python 3.12.0
pillow==10.4.0
imageio==2.26.1

full packages:

Package                 Version
----------------------- -----------
absl-py                 2.1.0
boto3                   1.35.81
botocore                1.35.81
certifi                 2024.12.14
cffi                    1.17.1
charset-normalizer      3.4.0
contourpy               1.3.0
coverage                7.6.9
cryptography            44.0.0
cycler                  0.12.1
decorator               5.1.1
filelock                3.16.1
fonttools               4.55.3
fsspec                  2024.10.0
grpcio                  1.68.1
idna                    3.10
imageio                 2.26.1
imageio-ffmpeg          0.6.0
iniconfig               2.0.0
jinja2                  3.1.4
jmespath                1.0.1
jsonpatch               1.33
jsonpointer             3.0.0
kiwisolver              1.4.7
markdown                3.7
markupsafe              3.0.2
matplotlib              3.9.4
moto                    4.2.14
moviepy                 2.1.2
mpmath                  1.3.0
networkx                3.2.1
numpy                   2.0.2
packaging               24.1
pillow                  10.4.0
pluggy                  1.5.0
proglog                 0.1.10
protobuf                4.22.3
pycparser               2.22
pyparsing               3.2.0
pytest                  8.3.4
pytest-cov              6.0.0
python-dateutil         2.9.0.post0
python-dotenv           1.0.1
pyyaml                  6.0.2
requests                2.32.3
responses               0.25.3
ruff                    0.8.4
s3transfer              0.10.4
scipy                   1.13.1
setuptools              75.6.0
six                     1.17.0
soundfile               0.12.1
sympy                   1.13.1
tensorboard             2.18.0
tensorboard-data-server 0.7.2
torch                   2.5.1+cpu
torchvision             0.20.1+cpu
tornado                 6.4.2
tqdm                    4.67.1
typing-extensions       4.12.2
urllib3                 1.26.20
visdom                  0.2.4
websocket-client        1.8.0
werkzeug                3.1.3
xmltodict               0.14.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants