如何在Python中隐藏FFmpeg的控制台输出?

Shi*_*ini 6 python ffmpeg ffmpeg-python

我当时正在开发一个YouTube 视频下载器Python 程序。

我想将下载的数据编码为其他媒体格式,为此我使用了FFmpegFFmpeg-Python(在 Python 中使用 FFmpeg 的包)。

一切都很好,但我想问如何在控制台上禁用FFmpeg 输出?

这是我的程序的一些图片:-

主图形界面

但是当我的程序开始编码时,这个控制台经常出现,抑制主 GUI :-

FFMPEG - 输出

如果您知道我的问题的任何解决方案,请给我一些解决方案。这是我第一次尝试使用 Stackoverflow 来解决我的问题。

提前致谢 !!!!!

小智 9

将日志级别设置为安静

ffmpeg.input(file).output(filename, loglevel="quiet").run()
Run Code Online (Sandbox Code Playgroud)


小智 4

自从你问这个问题以来已经过去了 1 年零 8 个月,你可能已经有了解决方案。不过,我找到了解决您问题的方法。

你可以通过在打包python程序时修改原来的ffmpeg代码来解决这个问题。

首先,找到您的 ffmpeg lib 文件夹,如果您使用默认位置安装,您可以在此处检查您的库:C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages\ffmpeg。

其次,找到_probe.py并修改代码,这里是已经修改过的代码,任何修改都写在注释中。您需要在 Popen 中添加参数:shell=True, stdin=subprocess.PIPE。

import json
import subprocess
from ._run import Error
from ._utils import convert_kwargs_to_cmd_line_args


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]

    # Original: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # Popen add args: 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)
    return json.loads(out.decode('utf-8'))


__all__ = ['probe']
Run Code Online (Sandbox Code Playgroud)

然后,转到_run.py。需要添加shell=True,修改stdin=subprocess.PIPE或者修改pipe_stdin=True(下面的代码部分只是部分代码):

@output_operator()
def run_async(
    stream_spec,
    cmd='ffmpeg',
    pipe_stdin=False,
    pipe_stdout=False,
    pipe_stderr=False,
    quiet=False,
    overwrite_output=False,
):
    """Asynchronously invoke ffmpeg for the supplied node graph.

Args:
    pipe_stdin: if True, connect pipe to subprocess stdin (to be
        used with ``pipe:`` ffmpeg inputs).
    pipe_stdout: if True, connect pipe to subprocess stdout (to be
        used with ``pipe:`` ffmpeg outputs).
    pipe_stderr: if True, connect pipe to subprocess stderr.
    quiet: shorthand for setting ``capture_stdout`` and
        ``capture_stderr``.
    **kwargs: keyword-arguments passed to ``get_args()`` (e.g.
        ``overwrite_output=True``).

Returns:
    A `subprocess Popen`_ object representing the child process.

Examples:
    Run and stream input::

        process = (
            ffmpeg
            .input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
            .output(out_filename, pix_fmt='yuv420p')
            .overwrite_output()
            .run_async(pipe_stdin=True)
        )
        process.communicate(input=input_data)

    Run and capture output::

        process = (
            ffmpeg
            .input(in_filename)
            .output('pipe':, format='rawvideo', pix_fmt='rgb24')
            .run_async(pipe_stdout=True, pipe_stderr=True)
        )
        out, err = process.communicate()

    Process video frame-by-frame using numpy::

        process1 = (
            ffmpeg
            .input(in_filename)
            .output('pipe:', format='rawvideo', pix_fmt='rgb24')
            .run_async(pipe_stdout=True)
        )

        process2 = (
            ffmpeg
            .input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
            .output(out_filename, pix_fmt='yuv420p')
            .overwrite_output()
            .run_async(pipe_stdin=True)
        )

        while True:
            in_bytes = process1.stdout.read(width * height * 3)
            if not in_bytes:
                break
            in_frame = (
                np
                .frombuffer(in_bytes, np.uint8)
                .reshape([height, width, 3])
            )
            out_frame = in_frame * 0.3
            process2.stdin.write(
                frame
                .astype(np.uint8)
                .tobytes()
            )

        process2.stdin.close()
        process1.wait()
        process2.wait()

.. _subprocess Popen: https://docs.python.org/3/library/subprocess.html#popen-objects
"""
    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

    # Original: return subprocess.Popen(
    #           args, stdin=pipe_stdin, stdout=stdout_stream, stderr=stderr_stream)    
    # Add shell=True, modify stdin=subprocess.PIPE or modify pipe_stdin=True

    return subprocess.Popen(
        args, shell=True, stdin=subprocess.PIPE, stdout=stdout_stream, stderr=stderr_stream
    )
Run Code Online (Sandbox Code Playgroud)

  • 你好呀!请参考并在答案中突出显示您正在更改 python 库,并且该解决方案将在 ffmpeg 库的下一个 pip 更新中被覆盖 (2认同)