Leonardo the Vinchi
Leonardo the Vinchi

Reputation: 439

subprocess.check_output fails with CalledProcessError but error is empty string. Command works in terminal

I want to run the command ffprobe -i test.m4a -show_entries format=duration -v quiet -of csv="p=0". It works in the terminal and returns output code 0, but running it with subprocess, i.e.

subprocess.check_output(['ffprobe', '-i', 'test.m4a', '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv="p=0"'])

raises a CalledProcessError - {Command} returned non-zero exit status 1.. I tried running this command in a try-except loop and printing the error details, but it just outputs as an empty byte string b''.

Upvotes: 0

Views: 904

Answers (2)

Rotem
Rotem

Reputation: 32094

One way for debugging the issue is adding -report argument:

subprocess.check_output(['ffprobe', '-i', 'output.mp4', '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv="p=0"', '-report'])

-report is used for creating a log file with name like ffprobe-20220811-232043.log.

The log files shows the following error:
[csv @ 00000213297fe640] Failed to set option '"p' with value '0"' provided to writer context

The log files shows that the executed "shell command" is:
ffprobe -i output.mp4 -show_entries "format=duration" -v quiet -of "csv=\"p=0\"" -report

The solution is removing the quotes from "p=0":

subprocess.check_output(['ffprobe', '-i', 'output.mp4', '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=p=0'])

Upvotes: 1

CatDad
CatDad

Reputation: 129

I recommend using the subprocess.run instead of check_output.

subprocess.run(command, stdout=output, encoding="utf-8")

Command = is the variable that houses the command you want , no need for separation using the commas.

stdout = Output = means that the output should be recorded in the file called (Output) which you can create beforehand.

encoding = just means to make sure it's encoded into texts from bytes

Upvotes: 0

Related Questions