SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: exec() problem

  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    exec() problem

    Hello all,

    I'm trying to use the exec() command to do some ffmpeg stuff.

    When I do:
    exec('ffmpeg', $output);
    or
    exec('ffmpeg -formats', $output);

    It works, but when I try:
    exec('ffmpeg -version', $output);
    or anything with the -i flag the output returns nothing.

    If I use the same commands in the shell it does work.
    Also, when I do the command 'whoami' I get the same result both in the shell as in PHP.

    Any ideas?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Trying redirecting stderr to stdout:
    PHP Code:
    exec('ffmpeg -version 2>&1'$output); 

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That fixed it!
    I don't really understand why, but it fixes everything.

    Thank you very much, sk89q, I've been searching the web for a while now.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Programs have access to two "pipes" - standard out and standard error. A lot of programs will write status messages to standard error so that you can use standard out for actual data. Instead of having ffmpeg write to a file, you can instead have it write to standard out (so you can just get the data from memory). Obviously, if there was no other pipe to send the status messages to, there would be no way to tell you or another program what was happening. The program can't write to standard out at that point, 'cause that's where the data goes.

    "2>&1" redirects standard error (2) to standard out (1).

    exec() is simple, and it will only give you stuff printed to stdout, which is why we have to redirect stderr to stdout. You can use proc_open() to get access to stdout and stderr separately, but if you don't need to get stdout separately, then you don't need to use it.

    When you use your terminal, no distinction is made between stderr and stdout, so none of this is inherently obvious.

    There are quite a few articles about stdout, stderr, stdin, pipe redirection, and such, if you care to know more.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •