# Using IO.popen to capture stderr?

**URL:** <https://www.ruby-forum.com/t/using-io-popen-to-capture-stderr/82155>\
**Category:** Rails\
**Created:** [January 17, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/using-io-popen-to-capture-stderr/82155 "2007-01-17T20:54:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [January 17, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/using-io-popen-to-capture-stderr/82155/1 "2007-01-17T20:54:15Z")

</div>

Hello,

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?

Mike

snippet of code…

def process\_file(filename)  
@filename = filename  
puts "Processing: " + @filename  
cmdline = "ffmpeg -i " + @filename  
ffmpeg = IO.popen(cmdline, “w+”)  
ffmpeg.close\_write  
result = ffmpeg.gets  
result.each do |line|  
puts "Line from ffmpeg: " + line  
end  
end

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. 🙂

---

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [January 22, 2007, 8:08am UTC](https://www.ruby-forum.com/t/using-io-popen-to-capture-stderr/82155/2 "2007-01-22T08:08:03Z")

</div>

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…

Mike

---

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [January 5, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/using-io-popen-to-capture-stderr/82155/3 "2008-01-05T13:10:10Z")

</div>

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+”)
