AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'

Viewed 564

After converting my code to exe either via auto-py-to-exe or pyinstaller, I get the following errors in relation to moviepy. The reason I need moviepy is that I cannot convert to mp4 when using a variable name although I can when set as a string in the code seen below. I have all the necessary libraries installed as far as I am aware btw and I have the same error even when running through Anaconda CMD.exe Prompt. I have tried using the solutions from the internet but they seem to involve modifying certain init files that didn't fix the problem or importing submodules from moviepy which doesn't work anymore with my code. I have tried everything I have seen on the internet with no luck. Any help would be greatly appreciated. Oh and btw, it works just fine as a .py file. It only breaks when converted to exe. So maybe missing dependency when converting to exe involving moviepy? Below is my error message:

Traceback (most recent call last):
  File "cdn_python_script.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
  File "moviepy\editor.py", line 87, in <module>
  File "<string>", line 1, in <module>
AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'
[11968] Failed to execute script cdn_python_script

Here is my source code:

# libraries imported for usage within the written code.
import requests
import moviepy.editor as moviepy
import os

# shows menu of options for the user to select from in range of 1-4.
print("Type 1 to Download a video of the Earth")
print("Type 2 to Download a video of an animated Rabbit")
print("Type 3 to Download a video of an Ocean")
print("Type 4 to Download a video of Sea Life")

# starts a loop so that the message is continuously repeated if necessary due to try and except functions.
while True:
    try:
        # user input saves to variable name message for future use in functions.
        message = int(
            # user is prompted to enter a value on their keyboard which is checked against requirements and rules.
            input(
                "Choose a video from the above menu, then enter the corresponding number and press enter: "
            )
        )
    # checks for value of user input as to ensure number is selected otherwise repeats question.
    except ValueError:
        print("Sorry, please enter a number.")
        # if the input is a number then the loop continues instead of looping back to the start.
        continue

    # part of the loop that makes sure the variable message isn't 0 or below as to ensure it is within the range of 1-4.
    if message <= 0:
        print("Sorry, your response was not within the range of 1-4.")
    # part of the loop that makes sure the variable message isn't above 4 as to ensure it is within the range of 1-4.
    elif message > 4:
        print("Sorry, your response was not within the range of 1-4.")
        continue
    # if input is a number within the correct range then it makes it to this code where the loop breaks.
    else:
        # selected number was successfully parsed, and we're happy with its value.
        # we're ready to exit the loop.
        break

# checks the value saved in the variable message is eitehr 1, 2, 3 or 4 and if so prints video selected.
if message in range(1, 5):
    print("Video Selected")
# checks the variable message is eitehr 1, 2, 3 or 4 and if not prints video not seelcted and will run previous loop.
else:
    print("Video not selected.")

# runs the correct block of code depending on if user selected 1, 2, 3 or 4 based on user input saved to the variable.
if message == 1:
    # saves the below string (URL) to variable name file_url.
    file_url = "https://myawsbuckettest12321.s3.eu-west-2.amazonaws.com/file_example_MP4_1920_18MG.mp4"
    # requests.get function saved to variable r and uses the saved file_url for the download later on.
    # requests library is used here within this code.
    r = requests.get(file_url, stream=True)

elif message == 2:
    file_url = (
        "https://myawsbuckettest12321.s3.eu-west-2.amazonaws.com/sample-mp4-file.mp4"
    )
    r = requests.get(file_url, stream=True)

elif message == 3:
    file_url = (
        "https://myawsbuckettest12321.s3.eu-west-2.amazonaws.com/sample_1280x720.mp4"
    )
    r = requests.get(file_url, stream=True)

elif message == 4:
    file_url = "https://myawsbuckettest12321.s3.eu-west-2.amazonaws.com/sample_960x400_ocean_with_audio.mp4"
    r = requests.get(file_url, stream=True)

# prompts the user to enter a name for the file and the input is then saved to file_name.
# the prompt from the user is to avoid overwriting files if the user downloads multiple videos because of a set string.
file_name = input("Please give the video a name to save as, then press enter: ")
print("Please wait while your video is downloading...")

# takes the file_name value depending on number selected earlier, opens file in binary mode and attempts to save as mp4.
with open(file_name, "wb") as mp4:
    for chunk in r.iter_content(chunk_size=1024):

        # writing one chunk at a time to mp4 file.
        if chunk:
            mp4.write(chunk)

# file_name variable above does not save as mp4 while it does if set as a string instead of a variable.
# therefore, moviepy library is used here to convert the file after downloaded to mp4 format to view the video.
clip = moviepy.VideoFileClip(file_name)
# saves the video as file_name (what the user named the file) plus .mp4 to ensure it always has the correct file format.
clip.write_videofile(file_name + ".mp4")

# os library is imported here because the converted mp4 and original file still exist.
# this line simply deleted the original file leaving the mp4 as it looks for the file_name without the mp4 on the end.
os.remove(file_name)

# startfile function from within the os library is used here to autoplay the downloaded and converted mp4 for the user.
from os import startfile

# file_input meaning what the user named the file as plus .mp4 is found once downloaded so as to play the mp4.
startfile(file_name + ".mp4")

Thank you!

1 Answers

You have to use

from moviepy.audio.io.VideoFileClip import VideoFileClip

Replace

import moviepy.editor as moviepy
Related