1

i'm trying to get the ouput from a bash command. If I execute the some command directly in the bash, i see the result. But not with PHP exec() function..

$command = "ffmpeg -i '$video_path' 2>&1 | grep Video | perl -wle 'while(<>){ $_ =~ /.*?(\d+x\d+).*/; print $1; }'";
$res = exec($command/*, $output*/);

print_r($res);
// print_r($output);

This command try to get a video resolution using ffmpeg lib. The video path it's fine, I triple check that.

Thanks!

EDIT: screenshot from the output in bash

enter image description here

6
  • 1
    Per the docs, "The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function." Commented Aug 15, 2017 at 15:29
  • 2
    The easiest way to get the full output of a command into a string is to use backticks (`) Commented Aug 15, 2017 at 15:30
  • It looks like you have tried to use the output argument to exec, which will contain all of the output, rather than the last line. What is in $output when you run it that way? Commented Aug 15, 2017 at 15:34
  • The output has just one line, I add an screenshot in the question where you can check that. Commented Aug 15, 2017 at 15:34
  • Should I need to escape some of this chars? $_ =~ /.*?(\d+x\d+).*/; ? Commented Aug 15, 2017 at 15:35

1 Answer 1

2

Try escaping the $ in your regex with a backslash. Since you're wrapping everything in double quotes, PHP is trying to insert the value of $_, which is not a thing. I got an undefined variable notice when I ran your code. It worked for me when I escaped the $.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.