Forum: Ruby on Rails Using IO.popen to capture stderr?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Cfd2ef8c8ccf61c83b37185499162261?d=identicon&s=25 mvargo (Guest)
on 2007-01-17 20:54
(Received via mailing list)

I'm trying to write a utility to run an external process and capture
it's stderr.  It seems that IO.popen only lets me read the stdout.  Any
tips for how to read stderr?


snippet of code...

  def process_file(filename)
    @filename = filename
    puts "Processing: " + @filename
    cmdline = "ffmpeg -i " + @filename
    ffmpeg = IO.popen(cmdline, "w+")
    result = ffmpeg.gets
    result.each do |line|
      puts "Line from ffmpeg: " + line

This code does not work because the good stuff I want from ffmpeg went
to stderr and not stdout.  Please refrain from commenting on the merits
of ffmpeg.  That could go on all day.  :-)
Cfd2ef8c8ccf61c83b37185499162261?d=identicon&s=25 mvargo (Guest)
on 2007-01-22 08:08
(Received via mailing list)
Hello again,

No takers so far...

I tried this:

IO.popen(cmdline, "w+") { |f| puts f.gets }

and the command executes but my silly command (fffmpeg) writes all the
good stuff to stderr.  I grep'ed the source for ffmpeg and there are :
$ grep -r fprintf.stderr *  | wc
    284    2271   25075

way to many for me to want to change.  Is there some cool command line
trick to map stderr to stdout?

Thanks for any help...

4f81e5f80b6da610c2488838bd565e7f?d=identicon&s=25 Ian Gallagher (crash)
on 2008-01-05 13:10
This has probably long-been resolved for you, but for others who end up
here hoping for the answer..

You can just stick "2>&1" in the argument to IO.popen after your
command, like so:

ffmpeg = IO.popen(cmdline + " 2>&1", "w+")
This topic is locked and can not be replied to.