1

I'm trying to get the output of a command's STDOUT with the HandBrakeCLI program when encoding a video. I can't seem to get python to handle its output on the standard output stream. I've tried the following codes:

import subprocess
import sys

encode = subprocess.check_output("HandBrakeCLI -i video.mkv -o out.mp4", shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
print(encode)

This printed nothing as well as this which I also tried:

import subprocess
import sys

encode = subprocess.Popen("HandBrakeCLI -i video.mkv -o out.mp4", stdout=subprocess.PIPE, stderr = subprocess.PIPE, shell=True, universal_newlines=True)
print(encode.stdout.read())

As stated before, both will result in no output. This application is the type that will update text on a single line in bash as it's encoding. I'm not sure if that type of data stream creates a problem with python or not.

cellsheet
  • 476
  • 1
  • 4
  • 22
  • have you tried typing you cmd in shell, and does it have return? – SolaWing Mar 23 '15 at 03:17
  • unrelated: the second code example should use `out, err = encode.communicate()` to avoid the possibility of deadlock. – jfs Mar 23 '15 at 20:09

1 Answers1

1

It seems HandBrakeCLI changes its output depending on whether it prints to a terminal. Either specify a command-line flag to force the necessary output or you could trick it by providing a pseudo-tty (if your system supports it) using pexpect or pty module directly.

Code examples on how to get output from a subprocess using pexpect, pty modules:

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670