[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: catching process output (Kernel#system

Paul Lutus

9/23/2006 3:36:00 PM

Chris Donhofer wrote:

> hi!
>
> when executing a program via the system method, how do i catch the
> output of the executed program?

By not using "Kernel::system()".

To get the error status of a completed process:

err = system("command")

To get the output from a process:

output = `command`

>
> furthermore, is there a tutorial describing how to use the system
> function the best way?

That depends on what you think is the best way. Maybe you could describe the
application you have in mind.

--
Paul Lutus
http://www.ara...
11 Answers

Chris Donhofer

9/23/2006 4:13:00 PM

0

hi!

Paul Lutus wrote:
>
> By not using "Kernel::system()".
>
> To get the error status of a completed process:
>
> err = system("command")
>
> To get the output from a process:
>
> output = `command`
>
>

thx!

>>furthermore, is there a tutorial describing how to use the system
>>function the best way?
>
>
> That depends on what you think is the best way. Maybe you could describe the
> application you have in mind.
>

im trying to create a simple application that does the following:

1. get a list of files(media files) in a certain directory
2. for every file:
a. get the filetype
b. choose the appropriate converter (f.i. ffmpeg, swf2flv)
c. convert the file
d. continue
3. write process output to logfile

cheers, chris

Vincent Fourmond

9/23/2006 4:16:00 PM

0


Hello !

> im trying to create a simple application that does the following:
>
> 1. get a list of files(media files) in a certain directory
> 2. for every file:
> a. get the filetype
> b. choose the appropriate converter (f.i. ffmpeg, swf2flv)
> c. convert the file
> d. continue
> 3. write process output to logfile

In that case, the `command` syntax is perfectly adapted. (or system,
for the conversion). In case you need to check the return value of the
`commands`, use $? .

If you need to work with bigger output, where it would not be suitable
to store it all in a Ruby variable, give a look at IO.popen

Cheers !

Vince

Robert Klemme

9/23/2006 4:28:00 PM

0

Vincent Fourmond wrote:
> In that case, the `command` syntax is perfectly adapted. (or system,
> for the conversion). In case you need to check the return value of the
> `commands`, use $? .
>
> If you need to work with bigger output, where it would not be suitable
> to store it all in a Ruby variable, give a look at IO.popen

Plus, an alternative syntax fpr `ls foo` is %x{ls foo}.

robert

Chris Donhofer

9/23/2006 5:21:00 PM

0

thx for the answers guys!

but still, i cant get the output into my array. below is the code im
using - what am i doing wrong?

cheers, chris

def do_convert
#testcode
#convert file
puts "STARTING CONVERSION..."
op = ["ffmpeg result/output:".upcase, " "]

#using ls instead of ffmpeg, so everyone can test this
IO.popen("ls") do |l|
op.insert(-1, l)
end

#i also tried "p = IO.popen('ls')" and then
#my_array = p.readlines, but with the same result

#display output
op.each do |line|
puts line
end

end

Chris Donhofer

9/23/2006 5:32:00 PM

0

i was a bit too quick posting the last msg, the code should be:

IO.popen("ffmpeg -i my.mov -t flv fruhwirth150306.flv") do |l|
op.insert(-1, l.gets) #using gets now
end

still...the only outpu i get is nil.

Vincent Fourmond

9/23/2006 6:56:00 PM

0


Hello !

This works, at least where popen is available

op = []

IO.popen("ls") do |l|
op += l.readlines
end
p op

However, if you are on a Win box, my guess is it won't work... Use
backticks, then. Just for information, the argument of the block of
popen or open is an the IO object representing the file you opened.

Cheers

Vince


MonkeeSage

9/23/2006 7:20:00 PM

0

Chris Donhofer wrote:
> i was a bit too quick posting the last msg, the code should be:
>
> IO.popen("ffmpeg -i my.mov -t flv fruhwirth150306.flv") do |l|
> op.insert(-1, l.gets) #using gets now
> end
>
> still...the only outpu i get is nil.

Hmmm...you're telling ffmpeg to output a file. So...what output are you
trying to get from ffmpeg on stdout? The encoding progress or
something?

Regards,
Jordan

Chris Donhofer

9/23/2006 8:40:00 PM

0

@vincent:
thx, this works perfectly for 'ls', but unfortunately not for ffmpeg - i
guess i didn't see the difference between the ways those two commands
do their output.

MonkeeSage wrote:
>
> Hmmm...you're telling ffmpeg to output a file. So...what output are you
> trying to get from ffmpeg on stdout? The encoding progress or
> something?
>
> Regards,
> Jordan

im trying to catch this:

ffmpeg version 0.4.9-pre1, build 4747, Copyright (c) 2000-2004 Fabrice
Bellard
configuration: --build i386-linux --enable-gpl --enable-pp
--enable-zlib --enable-vorbis --enable-a52 --enable-dts --disable-debug
--prefix=/usr
built on Mar 8 2006 21:32:15, gcc: 3.3.5 (Debian 1:3.3.5-13)
Input #0, mov,mp4,m4a,3gp,3g2, from
'/home/insel/projects/flv_encoder/in/fruhwirth150306.mov':
Duration: 01:17:40.0, start: 0.000000, bitrate: 64 kb/s
Stream #0.0: Video: rpza, 800x600, 1.00 fps
Output #0, flv, to 'fruhwirth150306.flv':
Stream #0.0: Video: flv, 800x600, 1.00 fps, q=2-31, 200 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop encoding
frame= 4660 q=0.0 Lsize= 76131kB time=4660.0 bitrate= 133.8kbits/s
video:76059kB audio:0kB global headers:0kB muxing overhead 0.095749%

cheers, chris

MonkeeSage

9/23/2006 8:49:00 PM

0

Hi Chris,

ffmpeg writes that on stderr rather than stdout, so a simple way to
capture it is with output redirection (the 2>&1 part means redirect
stderr to stdout):

op = ["ffmpeg result/output:".upcase, " "]
....
op << %x{ffmpeg -v 2>&1}

For a more robust solution to capturing stderr look at popen3 in the
standard library [1] or the popen4 extension from rubyforge [2].

Also note that it's somewhat complicated to try to read the output of
programs like ffmpeg to get the encoding status for realtime display in
your app. To do that you'll probably have to use threads and a polling
loop or IO#select among other things. I don't know if that's why you
want to read the output, but I just thought I'd let you know.

[1] http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/...
[2] http://popen4.ruby...

Regards,
Jordan

Chris Donhofer

9/23/2006 9:56:00 PM

0

thx jordan, works perfectly =)

cheers, chris

MonkeeSage wrote:
> Hi Chris,
>
> ffmpeg writes that on stderr rather than stdout, so a simple way to
> capture it is with output redirection (the 2>&1 part means redirect
> stderr to stdout):
>
> op = ["ffmpeg result/output:".upcase, " "]
> ...
> op << %x{ffmpeg -v 2>&1}
>
> For a more robust solution to capturing stderr look at popen3 in the
> standard library [1] or the popen4 extension from rubyforge [2].
>
> Also note that it's somewhat complicated to try to read the output of
> programs like ffmpeg to get the encoding status for realtime display in
> your app. To do that you'll probably have to use threads and a polling
> loop or IO#select among other things. I don't know if that's why you
> want to read the output, but I just thought I'd let you know.
>
> [1] http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/...
> [2] http://popen4.ruby...
>
> Regards,
> Jordan
>