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(這個方法我嘗試無效)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM