属性错误:模块"moviepy.audio.fx.all"没有属性"audio_fadein"



通过auto-py-to-exe或pyinstaller将代码转换为exe后,我得到了以下与moviepy有关的错误。我需要moviepy的原因是,在使用变量名时无法转换为mp4,尽管在下面的代码中设置为字符串时可以转换。据我所知,我已经安装了所有必要的库,即使在运行Anaconda CMD.exe Prompt时,我也会出现同样的错误。我尝试过使用互联网上的解决方案,但它们似乎涉及修改某些没有解决问题的init文件,或者从moviepy导入子模块,这些子模块不再适用于我的代码。我在网上看到的所有东西都试过了,但没有成功。如有任何帮助,我们将不胜感激。哦,顺便说一句,它作为一个.py文件工作得很好。它只有在转换为exe时才会中断。所以在转换到涉及moviepy的exe时可能缺少依赖项?以下是我的错误信息:

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 "PyInstallerloaderpyimod03_importers.py", line 531, in exec_module
File "moviepyeditor.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

这是我的源代码:

# 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")

谢谢!

您必须使用

from moviepy.audio.io.VideoFileClip import VideoFileClip

更换

import moviepy.editor as moviepy

来自moviepy.VideoClip导入VideoClip正在为我工作。来源/文档:https://moviepy.readthedocs.io/en/latest/_modules/moviepy/video/io/VideoFileClip.html

最新更新