当前位置: 移动技术网 > IT编程>脚本编程>Python > pyinstaller打包PySide2写的GUI程序,调用ffmpeg隐藏CMD控制台解决方案

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

2020年01月06日  | 移动技术网IT编程  | 我要评论

陈彦行老公,突袭沙塔斯港口,java贪吃蛇

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.csdn.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.csdn.net/iserfj/article/details/94487538 ------ creationflags=0x08000000 或者 creationflags=subprocess.creat_no_window(这个方法我尝试无效)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网