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.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(这个方法我尝试无效)

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyInstaller是一个用于将Python程序打包成可执行文件的工具。它可以将PythonGUI程序打包成可在不同操作系统上运行的独立应用程序。 要使用PyInstaller打包GUI程序,首先需要安装PyInstaller库。可以使用命令"pip install PyInstaller"来安装PyInstaller。 在安装好PyInstaller后,可以使用subprocess库调用系统里的pyinstaller.exe和pyi-makespec来进行打包。通过调用这两个命令,可以制作完成程序本身的打包。 具体操作步骤如下: 1. 安装PyInstaller库:运行命令"pip install PyInstaller"来安装PyInstaller。 2. 进入GUI程序所在的目录:使用命令行切换到GUI程序所在的目录。 3. 执行PyInstaller命令:运行命令"pyinstaller your_program.py"来使用PyInstaller进行打包,其中your_program.py是你的GUI程序的文件名。 4. 等待打包完成:PyInstaller会自动分析你的程序依赖的模块和资源文件,并将它们打包到一个独立的可执行文件中。等待打包过程完成。 5. 完成打包打包完成后,在dist目录下会生成一个可执行文件,可以将这个文件复制到其他机器上运行。 请注意,使用PyInstaller打包GUI程序可能会遇到一些问题,比如依赖库的路径问题、资源文件的加载问题等。如果遇到问题,可以查看PyInstaller的官方文档或者在相关的技术论坛上寻求帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [pyinstaller打包python GUI程序](https://blog.csdn.net/i_Truth/article/details/86739331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [pyinstaller打包程序图形界面版](https://download.csdn.net/download/zjp1981/10910260)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值