2

No matter what I do, the output gets printed to a command window instead of being captured. I'm using Strawberry Perl on Windows and trying to capture the output of ffprobe to do some batch conversions.

In every instance the command works properly, I just can't capture it. Output goes to the command window and the return value seems to be an empty string.

$output = `ffprobe.exe "$file"`; # nope

$output = qx/ffprobe.exe "$file"/; # nope

$return = system("ffprobe.exe \"$file\" > output.txt") # nope, and $return is 0

If I open a command window and run it like this:

perl myscript.pl > output.txt

output.txt contains things the script itself prints (like if I do "print 'command output starts here:'; it will contain that), but none of the program output.

Since I do this on Linux all the time it must be a quirk of Windows, I just can't figure out what.


Update, solved

It turns out that output was being sent to STDERR. I was able to capture it with IPC::Run3. Nothing else worked, so I guess I'm calling IPC::Run3 my solution for this problem on Windows. I'll leave this post here in case anyone has the same problem.

use IPC::Run3;
my ($stdout, $stderr);
$run = run3($command, undef, \$stdout, \$stderr); # backslashes are important here
say "output was $stderr"; # works
ikegami
  • 367,544
  • 15
  • 269
  • 518
felwithe
  • 2,683
  • 2
  • 23
  • 38

1 Answers1

1

You can simply redirect STDERR to STDOUT:

$output = `ffprobe.exe "$file" 2>&1`
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339