pyinstaller打包PySide2写的GUI程序,调用ffmpeg隐藏CMD控制台解决方案

时间:2023-03-10 07:07:59
pyinstaller打包PySide2写的GUI程序,调用ffmpeg隐藏CMD控制台解决方案

1 问题描述

使用PySide2写了一个GUI程序,调用ffmpeg命令行工具,做简单的批量视频处理(调整帧宽度、帧高度、视频变速、降低视频码率达到限制视频大小),使用了ffmpeg、 ffmpeg-python库;

挺简单的事儿,但遇到一个问题:

pyinstaller打包程序时:

  1. 不加 -w 或 --noconsole,有CMD丑黑框,程序可以正常运行,但是程序界面背后一个大大的CMD黑框真心难看。。。
  2. 加上 -w 或 --noconsole,没有CMD黑框,程序会直接无限等待,无法正常运行,猜测是调用 ffmpeg 时需要一个shell环境供PIPE的输入输出

2 解决方案

心急的直接看 2.2 隐藏CMD黑框。。。

2.1 CMD黑框依旧在,不显示verbose信息(治标不治本)

  1. 使用subprocess.call():

    调用CMD命令时,在 ffmpeg 后面的参数加上 -loglevel quiet,就只有黑框,不显示实时进度信息

  2. 使用ffmpeg、ffmpeg-python库

    ffmpeg.run(quiet=True),将quiet设置为 True,就只有黑框,不显示实时进度信息

2.2 隐藏CMD黑框(啊哈哈哈舒服了)

(我使用到了ffmpeg库的 probe(调用ffprobe.exe获取视频流信息)和run(调用ffmpeg.exe执行操作)方法)

  1. 找到ffmpeg库的 _probe.py_run.py 文件

    备份这两个文件,修改完、打包完程序后再恢复原样

    把这两文件复制到桌面修改好再放回去(这步坑了我一点时间,win10没用管理员权限打开文件,由于ffmpeg库安装在C盘的Program...路径下,在PyCharm中做出的修改一直没保存。。。)

  2. 修改 _probe.py

    源码: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    Popen 参数添加 shell=True, stdin=subprocess.PIPE

    def probe(filename, cmd='ffprobe', **kwargs):
    """Run ffprobe on the specified file and return a JSON representation of the output. Raises:
    :class:`ffmpeg.Error`: if ffprobe returns a non-zero exit code,
    an :class:`Error` is returned with a generic error message.
    The stderr output can be retrieved by accessing the
    ``stderr`` property of the exception.
    """
    args = [cmd, '-show_format', '-show_streams', '-of', 'json']
    args += convert_kwargs_to_cmd_line_args(kwargs)
    args += [filename] # 源码: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # Popen 参数添加 shell=True, stdin=subprocess.PIPE, p = subprocess.Popen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    if p.returncode != 0:
    raise Error('ffprobe', out, err)
    p.wait()
    return json.loads(out.decode('utf-8'))
  3. 修改 _run.py

    源码: return subprocess.Popen(args, stdin=pipe_stdin, stdout=stdout_stream, stderr=stderr_stream)

    添加 shell=True, 修改 stdin=subprocess.PIPE 或者修改 pipe_stdin=True

    def run_async(
    stream_spec,
    cmd='ffmpeg',
    pipe_stdin=False,
    pipe_stdout=False,
    pipe_stderr=False,
    quiet=False,
    overwrite_output=False,
    ):
    args = compile(stream_spec, cmd, overwrite_output=overwrite_output)
    stdin_stream = subprocess.PIPE if pipe_stdin else None
    stdout_stream = subprocess.PIPE if pipe_stdout or quiet else None
    stderr_stream = subprocess.PIPE if pipe_stderr or quiet else None # 源码: return subprocess.Popen(
    # args, stdin=pipe_stdin, stdout=stdout_stream, stderr=stderr_stream)
    # 添加 shell=True, 修改 stdin=subprocess.PIPE或者修改 pipe_stdin=True return subprocess.Popen(
    args, shell=True, stdin=subprocess.PIPE, stdout=stdout_stream, stderr=stderr_stream
    )
  4. 将修改完的 _probe.py 和 _run.py 放回 ffmpeg库

  5. pyinstaller 打包程序时,添加 -w 或 --noconsole参数,这时CMD黑框就不显示了

    黑框没有了,一切都舒服了

附1:不用ffmpeg库,而是直接使用 subprocess调用ffmpeg.exe的,只需要在调用subprocess.Popen()时指定参数 shell=True, stdin=subprocess.PIPE 即可

附2:打包完程序,把备份的 _probe.py 和 _run.py 恢复到ffmpeg库里,不知道这么写有啥不好的影响,还是恢复原样吧

参考1:https://blog.****.net/polyhedronx/article/details/82432148 ------ subprocess.Popen设置shell=True,stdin=subprocess.PIPE

参考2:https://my.oschina.net/u/2396236/blog/1610765 ------ subprocess.Popen设置shell=True,stdin=subprocess.PIPE

参考3:https://blog.****.net/iserfj/article/details/94487538 ------ creationflags=0x08000000 或者 creationflags=subprocess.CREAT_NO_WINDOW(这个方法我尝试无效)