Python Forum
Controlling what get outputted to stdout when running external commands
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controlling what get outputted to stdout when running external commands
#1
I am using Python to compress video files via Handbrake and subprocess. When I run my current code the HandbrakeCli output still outputs to stdout. Is there a better way to contain and control the output of HandbrakeCli in Python?

import subprocess
from tqdm import tqdm

handbrake_cli = "HandBrakeCLI.exe"
input_path = ""
json_preset = ""
output_path = ""


command = [handbrake_cli, "-i", input_path, "--json", json_preset, "-o", output_path]
p = subprocess.Popen(command, stdout=subprocess.PIPE, text=True)

with tqdm(total=100, dynamic_ncols=True, desc="Compressing file... ") as prbar:
	while (line := p.stdout.readline()) != "":
		line = line.rstrip()
		if '"Progress"' in line:
			progress = line.split(":")[1].lstrip(".").split(".")[0]
			# print(f"Progress: {progress}")
			prbar.update(progress)

print(f"End of output.  Return code: {p.wait()}")
Reply
#2
Here is the output of the parsing code above and the control as well. I am trying to get the output to should look like this so I can manipulate it further.

Output:
Progress: 0.60 Progress: 0.12 Progress: 0.22 Progress: 0.38 Progress: 0.42 Progress: 0.52 Progress: 0.60 Progress: 0.82 Progress: 0.94

Attached Files

.txt   raw_hb_output.txt (Size: 61.57 KB / Downloads: 71)
.txt   parsing_output.txt (Size: 11.03 KB / Downloads: 80)
Reply
#3
I wonder if some of this is because the console is getting both stdin and stderr.

If you run handbrake [options....] > /dev/null do you get the output you're trying to ignore? If so, (and if you don't need the stderr information), then try adding stderr=subprocess.DEVNULL to have that stream ignored.

If you do need to parse the stderr stream, then setting it to stderr=subprocess.STDOUT should work to combine them.
Reply
#4
(Jan-29-2022, 07:21 PM)bowlofred Wrote: I wonder if some of this is because the console is getting both stdin and stderr.

If you run handbrake [options....] > /dev/null do you get the output you're trying to ignore? If so, (and if you don't need the stderr information), then try adding stderr=subprocess.DEVNULL to have that stream ignored.

If you do need to parse the stderr stream, then setting it to stderr=subprocess.STDOUT should work to combine them.

I put the stderr stream inline with the stdout stream like shown above and that woked.
p = subprocess.Popen(command, stdout=subprocess.PIPE, text=True, stderr=subprocess.STDOUT)
Is there any reason as to why its is hard to capture two or three streams? If you have any good resorces on the topic I would a greatly aprecate it if you could send them my way.
Reply
#5
(Jan-30-2022, 04:06 PM)Daring_T Wrote: Is there any reason as to why its is hard to capture two or three streams? If you have any good resorces on the topic I would a greatly aprecate it if you could send them my way.

What streams do you want to capture? You can read from stdout and stderr directly. Combining them is usually easier because you don't have to do any complicated poll or non-blocking reads. If both streams are independent and you do a blocking read on one, you won't see any updates on the other one until the first one sends more data.
Daring_T likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [subprocess] Why stdout sent to stderr? Winfried 3 479 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Controlling program with TCP commands lavacode 1 549 Jul-10-2023, 04:39 PM
Last Post: deanhystad
  Performance options for sys.stdout.writelines dgrunwal 11 3,125 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  execute commands inside a running application mr_byte31 3 1,337 Apr-12-2022, 08:50 AM
Last Post: Larz60+
  Controlling text-to-speech pauses with pyttsx3 pmac1300 4 4,457 Mar-14-2022, 06:47 AM
Last Post: Coricoco_fr
  changing stdout and stderr Skaperen 4 2,687 Dec-02-2020, 08:58 PM
Last Post: Skaperen
  Looking For Help On Controlling Philips Hue From PyCharm RickyRay333 4 3,794 Aug-24-2020, 08:33 PM
Last Post: Nickd12
  Get stdout of a running process yok0 0 3,027 Aug-20-2020, 10:12 AM
Last Post: yok0
  will with sys.stdout as f: close sys.stdout? Skaperen 9 4,606 Nov-03-2019, 07:57 AM
Last Post: Gribouillis
  running multiple commands in a parallel pool Skaperen 6 3,943 Jul-30-2019, 05:49 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020