2

我正在尝试使用 Chaquopy 在我的 android 应用程序中运行 python 代码,我也在使用 moviepy 库(我正在将 mp4 文件转换为 gif 文件)。当我尝试进行转换时,我收到以下错误消息:

2019-02-06 15:46:47.586 3924-3924/com.udacity.gradle.builditbigger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.udacity.gradle.builditbigger, PID: 3924
com.chaquo.python.PyException: AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'
    at <python>.moviepy.editor.<module>(<string>:1)
    at <python>.moviepy.editor.<module>(editor.py:80)
    at <python>.java.android.importer.load_module_impl(importer.py:435)
    at <python>.java.android.importer.load_module(importer.py:353)
    at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
    at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
    at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
    at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
    at <python>.java.chaquopy.import_override(import.pxi:18)
    at <python>.gif_convert.<module>(gif_convert.py:1)
    at <python>.java.android.importer.load_module_impl(importer.py:435)
    at <python>.java.android.importer.load_module(importer.py:353)
    at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
    at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
    at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
    at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
    at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:994)
    at <python>.importlib.import_module(__init__.py:126)
    at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
    at com.chaquo.python.Python.getModule(Native Method)
    at com.udacity.gradle.builditbigger.newPost.visualMediaPost.VisualMediaPostFragment.moveFile(VisualMediaPostFragment.java:247)
    at com.udacity.gradle.builditbigger.camera.LifeCycleCamera.getFilePath(LifeCycleCamera.java:1161)
    at com.udacity.gradle.builditbigger.camera.LifeCycleCamera.stopRecordingVideo(LifeCycleCamera.java:1078)
    at com.udacity.gradle.builditbigger.newPost.visualMediaPost.VisualMediaPostFragment.lambda$onCreateView$5(VisualMediaPostFragment.java:162)
    at com.udacity.gradle.builditbigger.newPost.visualMediaPost.-$$Lambda$VisualMediaPostFragment$WS1G2y9p8sE-re_eOWFa7Rr0OCU.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:6897)
    at android.view.View$PerformClick.run(View.java:26089)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6940)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

我找到了解决这个问题的方法(它涉及更改通常由 android studio 生成的 python 代码,因此对 python 文件所做的任何更改都将在构建时被覆盖。

python代码如下所示

from moviepy.editor import VideoFileClip

def convert(path):
    clip = VideoFileClip(path)
    return clip.write_gif("output.gif", fps=24)

用于启动pyhton代码的java代码:

Python.start(new AndroidPlatform(getActivity()));
        Python py = Python.getInstance();
        PyObject gifConvert = py.getModule("gif_convert");
        PyObject gif = gifConvert.callAttr("convert", file.getAbsolutePath());
        Log.i("gif type", gif.toString());

任何帮助将不胜感激。

4

1 回答 1

0

旧版本的 Chaquopy 在pkgutil.iter_modules. 这已在 Chaquopy 6.2.1 中修复

于 2019-02-08T12:03:30.153 回答