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

ffmpeg_extract_subclip仅在Python exe中报错求助

问题描述

代码在Spyder IDE中运行正常,但通过auto-py-to-exe转换为exe后崩溃:

  • 推测未在打包配置中显式包含ffmpeg,手动将ffmpeg/ffprobe放入exe目录仍无效
  • 注释try/except块后程序崩溃,Windows事件查看器显示错误码0xC0000409
  • 添加日志后得到错误信息:'NoneType' object has no attribute 'write',但ffmpeg_extract_subclip并无write方法,且其他日志可正常写入文件
  • 使用Python 3.11版本

代码片段:

try:
    ffmpeg_extract_subclip(required_video_file, clipStart , clipEnd)    
except Exception as e:
    with open('DEBUGGING.txt', 'a+') as f:
        f.writelines('\n EXCEPCION:\n'+str(e))
        f.close()
解决方案

1. 手动指定ffmpeg/ffprobe路径

打包后程序无法自动定位ffmpeg二进制文件,需在代码中显式指定路径:

from moviepy.config import change_settings
import os

# 获取当前程序运行目录
current_dir = os.path.dirname(os.path.abspath(__file__))
# 拼接ffmpeg/ffprobe路径(确保这两个文件和exe同目录)
ffmpeg_path = os.path.join(current_dir, "ffmpeg.exe")
ffprobe_path = os.path.join(current_dir, "ffprobe.exe")

# 覆盖moviepy的默认配置
change_settings({"FFMPEG_BINARY": ffmpeg_path, "FFPROBE_BINARY": ffprobe_path})

2. 修复日志与子进程输出问题

日志中的NoneType错误并非来自ffmpeg_extract_subclip,而是其内部调用子进程时输出流处理异常,可通过以下方式解决:

  • 调用ffmpeg_extract_subclip时禁用日志输出:
    from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
    
    try:
        # 关闭verbose和logger,避免输出流导致的NoneType问题
        ffmpeg_extract_subclip(required_video_file, clipStart, clipEnd, 
                               verbose=False, logger=None)
    except Exception as e:
        # with语句自动管理文件,无需手动close
        with open('DEBUGGING.txt', 'a+', encoding='utf-8') as f:
            f.write(f'\n EXCEPCION:\n{str(e)}\n')
    
  • 改用subprocess直接调用ffmpeg命令,可控性更强:
    import subprocess
    import os
    
    current_dir = os.path.dirname(os.path.abspath(__file__))
    ffmpeg_path = os.path.join(current_dir, "ffmpeg.exe")
    
    try:
        # 构造ffmpeg切割命令,替换输出文件名为你的需求
        cmd = [
            ffmpeg_path,
            "-i", required_video_file,
            "-ss", str(clipStart),
            "-to", str(clipEnd),
            "-c", "copy",
            "output_clip.mp4"
        ]
        # 捕获输出,避免流处理异常
        subprocess.run(cmd, check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        with open('DEBUGGING.txt', 'a+', encoding='utf-8') as f:
            f.write(f'\n FFMPEG错误:\n{e.stderr}\n')
    except Exception as e:
        with open('DEBUGGING.txt', 'a+', encoding='utf-8') as f:
            f.write(f'\n 其他异常:\n{str(e)}\n')
    

3. 优化auto-py-to-exe打包配置

  • 在Additional Files选项中添加ffmpeg.exe和ffprobe.exe,设置Destination为.(当前目录),打包时自动复制到exe目录
  • 优先选择Directory模式打包,排查问题更方便;若用One File模式,需确保所有依赖被正确打包
  • 勾选Console Window选项,直接查看程序运行时的控制台输出,快速定位问题

4. 兼容性适配

  • 升级moviepy到最新版本,解决Python 3.11的兼容性问题:
    pip install --upgrade moviepy
    
  • 使用最新版本的ffmpeg二进制文件,避免旧版本的适配问题

内容的提问来源于stack exchange,提问作者Kevin M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 10:51:11