fnord12
fnord12

Reputation: 145

How can I run FFPROBE in a Python script without triggering the Windows Command window?

I am using ffmeg/ffprobe to get video durations (in an addon for Kodi). The code:

result = subprocess.run(["ffprobe", "-hide_banner", "-v", "quiet", "-show_entries",
                                 "format=duration", "-of",
                                 "default=noprint_wrappers=1:nokey=1", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

The above code and the file importing that code both have a .pyw extension (after first trying regular .py).

This works fine but in Windows 11 it causes the black Windows Command window to briefly flash for each video, despite the -hide_banner flag and loglevel being set to quiet. In Linux Mint it runs without any such window popping up.

Found the answer: subprocess.run just needed a final shell=True as the last argument.

Upvotes: 0

Views: 50

Answers (2)

Yaniek
Yaniek

Reputation: 82

To suppress the command window when running a Python script with subprocess function you can use creationflags parameter (which was added in Python 3.7) with CREATE_NO_WINDOW option inside your subprocess function like this:

import subprocess

# Your filename variable
filename = "path_to_your_video_file"

result = subprocess.run(
    ["ffprobe", "-hide_banner", "-v", "quiet", "-show_entries",
     "format=duration", "-of",
     "default=noprint_wrappers=1:nokey=1", filename],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    creationflags=subprocess.CREATE_NO_WINDOW  # This suppresses the command window
)

# Get the duration from the result
duration = result.stdout.decode().strip()
print(duration)

Explanation of the code: Adding creationflags=subprocess.CREATE_NO_WINDOW to your subprocess function prevents console window from popping up. More info about this parameter you can find here.

Upvotes: 0

fnord12
fnord12

Reputation: 145

It just needed shell=True at the end. The final working code:

result = subprocess.run(["ffprobe", "-hide_banner", "-v", "quiet", "-show_entries",
                                 "format=duration", "-of", 
                                 "default=noprint_wrappers=1:nokey=1", filename], stdout=subprocess.PIPE, stderr=None, shell=True)

Upvotes: 0

Related Questions