views:

57

answers:

2

I'm calling rtmpdump via subprocess and trying to redirect its output to a file. The problem is that I simply can't redirect it.

I tried first setting up the sys.stdout to the opened file. This works for, say, ls, but not for rtmpdump. I also tried setting the sys.stderr just to make sure and it also didn't work.

I tried then using a ">> file" with the command line argument but again it doesn't seem to work.

Also for the record, for some reason, Eclipse prints rtmpdump's output even if I use subprocess.call instead of subprocess.check_output, and without having to call the print method. This is black magic!

Any suggestions?

Edit: Here's some sample code.

    # /!\ note: need to use os.chdir first to get to the folder with rtmpdump!
    command = './rtmpdump -r rtmp://oxy.videolectures.net/video/ -y 2007/pascal/bootcamp07_vilanova/keller_mikaela/bootcamp07_keller_bss_01 -a video -s http://media.videolectures.net/jw-player/player.swf -w ffa4f0c469cfbe1f449ec42462e8c3ba16600f5a4b311980bb626893ca81f388 -x 53910 -o test.flv'
    split_command = shlex.split(command)
    subprocess.call(split_command)
A: 

See the link on getting the output from subprocess on SO

I guess the way would be to collect the output and write it to a file directly or provide file descriptors to which you output can be written.

Something like this:

f = open('dump.txt', 'wb')
p = subprocess.Popen(args, stdout=f, stderr=subprocess.STDOUT, shell=True)
pyfunc
Good answer except I wouldn't recommend using shell=True except for conversions from legacy code.
Douglas Leeder
@Douglas Leeder: Thanks. Thats right. I was using shell to validate the code.
pyfunc
+2  A: 

sys.stdout is the python's idea of the parent's output stream.

In any case you want to change the child's output stream.

subprocess.call and subprocess.Popen take named parameters for the output streams.

So open the file you want to output to and then pass that as the appropriate argument to subprocess.

f = open("outputFile","wb")
subprocess.call(argsArray,stdout=f)

Your talk of using >> suggest you are using shell=True, or think you are passing your arguments to the shell. In any case it is better to use the array form of subprocess, which avoid an unnecessary process, and any weirdness from the shell.

EDIT:

So I downloaded RTMPDump and tried it out, it would appear the messages are appearing on stderr.

So with the following program, nothing appears on the programs output, and the rtmpdump logs when into the stderr.txt file:

#!/usr/bin/env python

import os
import subprocess

RTMPDUMP="./rtmpdump"
assert os.path.isfile(RTMPDUMP)
command = [RTMPDUMP,'-r','rtmp://oxy.videolectures.net/video/',
        '-y','2007/pascal/bootcamp07_vilanova/keller_mikaela/bootcamp07_keller_bss_01',
        '-a','video','-s',
        'http://media.videolectures.net/jw-player/player.swf',
        '-w','ffa4f0c469cfbe1f449ec42462e8c3ba16600f5a4b311980bb626893ca81f388'
        ,'-x','53910','-o','test.flv']


stdout = open("stdout.txt","wb")
stderr = open("stderr.txt","wb")
subprocess.call(command,stdout=stdout,stderr=stderr)
Douglas Leeder
@Douglas Leeder: You beat me to it. Essentially my answer is the same.
pyfunc
@Doubles Leeder: Output is still the Eclipse console with this. :/
dmc
@dmc could you run `rtmpdump` from the command line, redirecting stdout to one file, and stderr to a different file, to see if the output does get redirected?
Douglas Leeder
@dmc there are ways that programs can reopen the tty their parent is connected to, which some password prompts are done that way.
Douglas Leeder
@Douglas Leeder: Tried redirecting them directly in the shell, didn't work.
dmc