Reputation: 21898
I have a script that calls ffprobe, parses its output and outputs it to the console.
Here's a stripped-down version of it without the parsing code and command-line options:
"""Invoke ffprobe to query a video file and parse the output"""
def ffprobe(fname):
import subprocess as sub
import re
p = sub.Popen(['ffprobe', fname], stderr=sub.PIPE)
stdout, stderr = p.communicate()
def main():
ffprobe("foo.mp4")
#print options.formatstr % locals()
if __name__ == '__main__':
main()
You can see that the only print
statement in my code is commented out, so the program shouldn't really output anything. However, this is what I get:
mpenkov@misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
mpenkov@misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
mpenkov@misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
A newline is mysteriously output by each invocation. Where is it coming from, and how can I deal with it?
There appears to be a similar SO question, except it's not using the communicate
call (http://stackoverflow.com/questions/7985334/python-subprocess-proc-stderr-read-introduce-extra-lines).
Upvotes: 2
Views: 2527
Reputation: 40394
I cannot reproduce the problem, so maybe it depends on the file you're passing to ffprobe
.
Anyway, from what I see, stdout
isn't being captured, so maybe the problem is just that ffprobe
is printing a new line character to stdout
.
To confirm this, please replace:
p = sub.Popen(['ffprobe', fname], stderr=sub.PIPE)
stdin, stderr = p.communicate()
with:
p = sub.Popen(['ffprobe', fname], stdout=sub.PIPE, stderr=sub.PIPE)
stdout, stderr = p.communicate()
In the new version, stdout
is captured and the output from p.communicate
is correctly named since it returns stdout
not stdin
.
Upvotes: 5