You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将依赖.mp4的Python程序打包为全桌面兼容的.exe文件?

解决Python转EXE并打包MP4文件的问题

推荐使用PyInstaller替代py2exe

PyInstaller对资源文件的支持更灵活,能满足你的需求,步骤如下:

  1. 安装PyInstaller
    在终端执行命令:

    pip install pyinstaller
    
  2. 打包时包含MP4文件
    有两种方式指定要打包的资源文件:

    • 命令行直接打包:
      Windows系统执行:
      pyinstaller --onefile --add-data "yaai.mp4;." your_script.py
      
      macOS/Linux系统执行:
      pyinstaller --onefile --add-data "yaai.mp4:." your_script.py
      
      --onefile参数会生成单个EXE文件,若不需要单文件可去掉该参数,生成包含所有依赖的文件夹。
    • 使用.spec文件(适合复杂场景):
      1. 先生成基础.spec文件:
        pyinstaller your_script.py
        
      2. 打开生成的your_script.spec文件,修改datas字段为:
        datas = [('yaai.mp4', '.')],
        
      3. 基于修改后的.spec文件重新打包:
        pyinstaller your_script.spec
        
  3. 修改Python代码适配打包路径
    打包后EXE运行时的资源路径和源码目录不同,需调整代码获取正确路径:

    import vlc
    import os
    import sys
    
    # 获取资源文件的正确路径
    def get_resource_path(relative_path):
        try:
            base_path = sys._MEIPASS  # PyInstaller打包后的临时目录
        except Exception:
            base_path = os.path.abspath(".")  # 未打包时的源码目录
        return os.path.join(base_path, relative_path)
    
    # 创建VLC播放器实例
    instance = vlc.Instance("--no-xlib")
    player = instance.media_player_new()
    
    # 设置媒体文件(用适配后的路径)
    media = instance.media_new(get_resource_path("yaai.mp4"))
    player.set_media(media)
    
    # 设置全屏循环播放
    player.set_fullscreen(True)
    player.play()
    
    # 保持播放循环
    while True:
        pass
    
  4. 处理VLC依赖问题
    若运行EXE时提示找不到VLC相关库:

    • 确保目标桌面设备已安装VLC播放器;
    • 或手动将VLC的核心库(Windows下的libvlc.dll、libvlccore.dll,路径一般在C:\Program Files\VideoLAN\VLC\)添加到打包参数,比如Windows下:
      pyinstaller --onefile --add-data "yaai.mp4;." --add-binary "C:\Program Files\VideoLAN\VLC\libvlc.dll;." --add-binary "C:\Program Files\VideoLAN\VLC\libvlccore.dll;." your_script.py
      

内容的提问来源于stack exchange,提问作者NielsNL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 01:00:54