Search code examples
python-3.xmoviepy

How to add a new method to an existing import in python? Specifically moviepy


For whatever reason, Python is not allowing me to access a custom method I created in moviepy's preview.py file. I just want to know how to correctly implement it into the file. For reference, before I changed the name of the method, it was working correctly.

I checked at least two __init.py__ files and they were effectively empty. I couldn't find if methods are initialized anywhere, and is probably what I'm missing.

I also tried restarting Git Bash and that didn't work either (another solution I saw).

Original:

@convert_masks_to_RGB
def preview(clip, fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
            audio_nbytes=2, fullscreen=False):

Changed:

@requires_duration
@convert_masks_to_RGB
def preview_custom(clip, marker_overlay="marker_overlay.png", fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
            audio_nbytes=2, fullscreen=False):

There are more than a few differences between the changed and original method, however at the moment the only result I expect is having the method be called correctly. Error is below:

Traceback (most recent call last):
  File "T3AJM.py", line 249, in <module>
    main()
  File "T3AJM.py", line 34, in main
    GUI_main_menu()
  File "T3AJM.py", line 85, in GUI_main_menu
    GUI_play_markers()
  File "T3AJM.py", line 125, in GUI_play_markers
    video.preview_custom(marker_overlay=TEMP_OVERLAY_FILE)
AttributeError: 'VideoFileClip' object has no attribute 'preview_custom'

Thank you for your time.


Solution

  • I'm not even sure if this technically fixes the problem, but just doing:

    from moviepy.video.io.preview import *
    

    and

    preview_custom(video, marker_overlay=TEMP_OVERLAY_FILE)
    

    fixed the problem. I have no idea why I had to change the way it was called, as doing clip.preview(), or in this case video.preview() worked perfectly fine before, but whatever.