12

I need to read the output from ffmpeg in order to even try the solution to my question from yesterday. This is a separate issue from my problem there, so I made a new question.

How the heck do I get the output from an ffmpeg -i command in PHP?

This is what I've been trying:

<?PHP
    error_reporting(E_ALL);
    $src = "/var/videos/video1.wmv";
    $command = "/usr/bin/ffmpeg -i " . $src;
    echo "<B>",$command,"</B><br/>";
    $command = escapeshellcmd($command);

    echo "backtick:<br/><pre>";
    `$command`;

    echo "</pre><br/>system:<br/><pre>";
    echo system($command);

    echo "</pre><br/>shell_exec:<br/><pre>";
    echo shell_exec($command);

    echo "</pre><br/>passthru:<br/><pre>";
    passthru($command);

    echo "</pre><br/>exec:<br/><pre>";
    $output = array();
    exec($command,$output,$status);
    foreach($output AS $o)
    {
            echo $o , "<br/>";
    }
    echo "</pre><br/>popen:<br/><pre>";
    $handle = popen($command,'r');
    echo fread($handle,1048576);
    pclose($handle);
    echo "</pre><br/>";
?>

This is my output:

<B>/usr/bin/ffmpeg -i /var/videos/video1.wmv</B><br/>
backtick:<br/>
    <pre></pre><br/>
system:<br/>
    <pre></pre><br/>
shell_exec:<br/>
    <pre></pre><br/>
passthru:<br/>
    <pre></pre><br/>
exec:<br/>
    <pre></pre><br/>
popen:<br/>
    <pre></pre><br/>

I don't get it. safe_mode is off. There's nothing in disable_functions. The directory is owned by www-data (the apache user on my Ubuntu system). I get a valid status back from exec() and system() and running the same command from the command line give me tons of output. I feel like I must be missing something obvious but I have no idea what it is.

4 Answers 4

29

The problem is you catch only stdout and not stderr (see Standard Streams). Change this line:

$command = "/usr/bin/ffmpeg -i " . $src;

into

$command = "/usr/bin/ffmpeg -i " . $src . " 2>&1";

and give it another try :)

3
  • Ahhh... yeah. That makes sense because my return status is 1 (not 0). I wish that worked, but I still get the same result, just with: "<B>/usr/bin/ffmpeg -i /var/videos/video1.wmv 2>&1</B>" at the beginning. I'll do some research to see if Ubuntu has some differences for redirecting stderr. Jul 10, 2009 at 18:16
  • I can confirm that the following should work. I've used this exact line in a hobby project once. exec('ffmpeg -i ' . escapeshellarg($filepath) . ' 2>&1', $output);
    – Werner
    Jul 10, 2009 at 18:51
  • Ahha! I figured it out. My problem was the escapeshellcmd() function. It was changing "2>&1" to "2\>\&1". I tried your code and it worked. Lesson I've learned: output the command after you've made all modifications to it =p. Thanks. Jul 10, 2009 at 19:07
8

Use ffprobe instead, it's much quicker and supports JSON output.

$output = shell_exec('ffprobe -v quiet -print_format json -show_format -show_streams "path/to/yourfile.ext"');
$parsed = json_decode($output, true);

And you have all your video info in a php array! This is much faster than ffmpeg -i for some reason.

2
  • 1
    Wow, what a useful function! I had no idea this existed. Thank you! Nov 20, 2012 at 21:29
  • 1
    It is also much quicker than initializing ffmpeg every time!
    – tweak2
    Nov 29, 2012 at 19:41
4

To get output status and output:

exec("ffmpeg -i input.avi output.mp4 2>&1", $output, $returnStatus);

print_r($output);

if($returnStatus === 0){
   // success
}
else {
   //fail
}
1
  • niiiiiiiiiiiiceeeeeeeeeeeee
    – Farzad
    Jun 29, 2017 at 19:22
0

You can use exec and print_r the output...

exec("ffmpeg -i input.avi -vcodec h264 -acodec aac -strict -2 output.mp4 2>&1",$output);

echo "<pre>";
print_r($output);
echo "</pre>";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.