# Trouble capturing output of spawned process

**URL:** <https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764>\
**Category:** Ruby\
**Created:** [March 13, 2008, 2:32am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764 "2008-03-13T02:32:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 2:32am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/1 "2008-03-13T02:32:56Z")

</div>

Hello-

I’m using the backtick notation to drive FFmpeg from Ruby. When FFmpeg  
runs, it outputs a fair amount of informational text to the console. I’m  
unable to read that data from my script. Here is my code:

result = `ffmpeg -i test.avi -f flv -acodec mp3 output.flv`  
puts “HERE IS THE RESULT\n”  
puts result  
puts “\nRESULT COMPLETE”

Here is the result when I run it:

FFmpeg version SVN-rUNKNOWN, Copyright © 2000-2007 Fabrice Bellard, et  
al.  
configuration: --enable-gpl --enable-pp --enable-swscaler  
–enable-pthreads --enable-libvorbis --enable-libtheora --enable-libogg  
–enable-libgsm --enable-dc1394 --disable-debug --enable-shared  
–prefix=/usr  
libavutil version: 1d.49.3.0  
libavcodec version: 1d.51.38.0  
libavformat version: 1d.51.10.0  
built on Jun 3 2007 20:59:25, gcc: 4.1.3 20070528 (prerelease)  
(Ubuntu 4.1.2-9ubuntu2)  
Input #0, avi, from ‘test.avi’:  
Duration: 00:00:01.8, start: 0.000000, bitrate: 6571 kb/s  
Stream #0.0: Video: cinepak, yuv420p, 640x480, 25.00 fps®  
Output #0, flv, to ‘output.flv’:  
Stream #0.0: Video: flv, yuv420p, 640x480, q=2-31, 200 kb/s, 25.00  
fps©  
Stream mapping:  
Stream #0.0 -\> #0.0  
Press [q] to stop encoding  
frame= 45 q=31.0 Lsize= 436kB time=1.8 bitrate=1985.8kbits/s  
video:435kB audio:0kB global headers:0kB muxing overhead 0.203409%  
HERE IS THE RESULT

RESULT COMPLETE

I was expecting the output to be bracketed by the text “HERE IS THE  
RESULT” and “RESULT COMPLETE”, but it looks like the infomation isn’t  
being put into the variable.

I’m running this using Ruby 1.8.6 on Ubuntu 7.10. I’m connecting to the  
machine using PuTTY from my Windows XP machine.

Does anyone know what might be going on?

Regards-  
Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 3:08am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/2 "2008-03-13T03:08:59Z")

</div>

Eric M. wrote:

> I was expecting the output to be bracketed by the text “HERE IS THE  
> RESULT” and “RESULT COMPLETE”, but it looks like the infomation isn’t  
> being put into the variable.

It may not be doing what you think it is. You may find that the output  
from ffmpeg is going directly to stderr. Inspect result to see if it  
contains anything, and if not then try

result=`ffmpeg -i test.avi -f flv -acodec mp3 output.flv 2>&1`

to see if that does the trick.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 3:28am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/3 "2008-03-13T03:28:08Z")

</div>

Hi Paul-

Thanks for your reply. I inspected the variable result and it is an  
empty string. I tried the syntax that you recommended, but it caused  
that commnd to hang. What is the “2\>&1” supposed to do? Does that pipe  
stderr into stdout?

Regards-  
Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 11:15am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/4 "2008-03-13T11:15:33Z")

</div>

Eric M. wrote:

> that commnd to hang. What is the “2\>&1” supposed to do? Does that pipe  
> stderr into stdout?

Yes. Exactly that.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 4:11am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/5 "2008-03-13T04:11:17Z")

</div>

Actually, it looks like the 2\>&1 syntax might be working. I think the  
last time I ran the script, it paused to ask me if I wanted to overwrite  
a file, since I didn’t see the prompt, I wasn’t able to repond. If the  
file doesn’t exist, it seems to complete just fine.

A second test I just did confirmed this.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 13, 2008, 11:17am UTC](https://www.ruby-forum.com/t/trouble-capturing-output-of-spawned-process/131764/6 "2008-03-13T11:17:04Z")

</div>

Eric M. wrote:

> last time I ran the script, it paused to ask me if I wanted to overwrite  
> a file, since I didn’t see the prompt, I wasn’t able to repond. If the

If that’s going to be an issue you can use IO#popen to control it more  
interactively.
