使用 cx_Freeze 在冻结的 Python 应用程序中运行 MoviePy 时出错

问题描述 投票:0回答:1

我在尝试运行使用 moviepy 的 Python 应用程序时遇到了一个持续存在的问题,该应用程序已使用 cx_Freeze 冻结。尽管在开发中正确设置了 ffmpeg 路径并确保 ffmpeg 包含在构建中,但当我尝试运行冻结的可执行文件时,应用程序会抛出与 ffmpeg 相关的错误。

错误消息

Traceback (most recent call last):
File "C:\***\***\AppData\Local\Programs\Python\Python39\Lib\site-packages\cx_Freeze\initscripts\_startup__.py", line 141, in run
  module_init.run(name + "__main__")
File "C:\***\***\AppData\Local\Programs\Python\Python39\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
  exec(code, main_globals)
File "merger.py", line 8, in <module>
  from moviepy.editor import VideoFileClip
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\editor.py", line 36, in <module>
  from .video.io.VideoFileClip import VideoFileClip
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 3, in <module>
  from moviepy.audio.io.AudioFileClip import AudioFileClip
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 3, in <module>
  from moviepy.audio.AudioClip import AudioClip
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\audio\AudioClip.py", line 7, in <module>
  from moviepy.audio.io.ffmpeg_audiowriter import ffmpeg_audiowrite
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py", line 7, in <module>
  from moviepy.config import get_setting
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\config.py", line 36, in <module>
  FFMPEG_BINARY = get_exe()
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\imageio\plugins\ffmpeg.py", line 173, in get_exe
  return imageio_ffmpeg.get_ffmpeg_exe()
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\imageio_ffmpeg\_utils.py", line 28, in get_ffmpeg_exe
  exe = _get_ffmpeg_exe()
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\imageio_ffmpeg_utils.py", line 44, in _get_ffmpeg_exe
  exe = os.path.join(_get_bin_dir(),
  FNAME_PER_PLATFORM.get(plat, ""))
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\site-packages\imageio_ffmpeg_utils.py", line 69, in _get_bin_dir
  ref = importlib.resources.files("imageio_ffmpeg.binaries") / "__init__.py"
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\importlib\resources.py", line 147, in files
  return _common.from_package(_get_package(package))
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\importlib_common.py", line 14, in from_package
  return fallback_resources(package.__spec__)
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\importlib_common.py", line 18, in fallback_resources
  package_directory = pathlib.Path(spec.origin).parent
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 1082, in __new__
  self = cls._from_parts(args, init=False)
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 707, in _from_parts
  drv, root, parts = self._parse_args(args)
File "C:\***\***\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 691, in _parse_args
  a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

当我尝试使用 MoviePy 的 VideoFileClip 函数时,特别会发生此错误。

详情

Python 版本:3.9

moviepy 版本:来自 GitHub 的最新

cx_Freeze 版本:7.1.1

操作系统:Windows 11

我已使用 include_files 选项将 ffmpeg 包含在 cx_Freeze 构建中,并在开发和冻结设置中设置 IMAGEIO_FFMPEG_EXE 环境变量。

代码片段:

import os
import sys
from moviepy.editor import VideoFileClip

def configure_ffmpeg():
    ffmpeg_path = os.path.join(os.path.dirname(sys.executable), 'ffmpeg', 'bin', 'ffmpeg.exe')
    if not os.path.exists(ffmpeg_path):
        raise FileNotFoundError(f"ffmpeg executable not found at {ffmpeg_path}")
    os.environ["IMAGEIO_FFMPEG_EXE"] = ffmpeg_path

configure_ffmpeg()
clip = VideoFileClip('path_to_video.mp4')

Setup.py 配置:

from cx_Freeze import setup, Executable

build_exe_options = {
    "packages": ["os", "moviepy", "idna"],
    "include_files": [('C:/ffmpeg/bin', ffmpeg/bin')]}

setup(
    name="MyApp",
    version="0.1",
    description="My MoviePy application",
    options={"build_exe": build_exe_options},
    executables=[Executable("script.py")])

我不确定为什么可执行文件无法找到或利用 ffmpeg,即使路径似乎设置正确并且可以在冻结环境之外工作。如何解决此错误以确保 ffmpeg 在冻结的 cx_Freeze 环境中正常工作?

尽管在开发中正确设置了 ffmpeg 路径并确保 ffmpeg 包含在构建中,但当我尝试运行冻结的可执行文件时,应用程序会抛出与 ffmpeg 相关的错误。

python ffmpeg cx-freeze moviepy
1个回答
0
投票

这是一个完全不常见的错误,所以我不确定这是否来自 cx_freeze、pythonnet 或 pywin32。不过我最喜欢使用 pyinstaller,而不是 cx_freeze。

© www.soinside.com 2019 - 2024. All rights reserved.