4

I'm trying to read the duration of video files using mediainfo. This shell command works

mediainfo --Inform="Video;%Duration/String3%" file

and produces an output like

00:00:33.600

But when I try to run it in python with this line

subprocess.check_output(['mediainfo', '--Inform="Video;%Duration/String3%"', file])

the whole --Inform thing is ignored and I get the full mediainfo output instead.

Is there a way to see the command constructed by subprocess to see what's wrong?

Or can anybody just tell what's wrong?

user1785730
  • 3,150
  • 4
  • 27
  • 50

1 Answers1

3

Try:

subprocess.check_output(['mediainfo', '--Inform=Video;%Duration/String3%', file])

The " in your python string are likely passed on to mediainfo, which can't parse them and will ignore the option.

These kind of problems are often caused by shell commands requiring/swallowing various special characters. Quotes such as " are often removed by bash due to shell magic. In contrast, python does not require them for magic, and will thus replicate them the way you used them. Why would you use them if you wouldn't need them? (Well, d'uh, because bash makes you believe you need them).

For example, in bash I can do

$ dd of="foobar"

and it will write to a file named foobar, swallowing the quotes.

In python, if I do

subprocess.check_output(["dd", 'of="barfoo"', 'if=foobar'])

it will write to a file named "barfoo", keeping the quotes.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119