3

I have the following PHP code:

<?php
$video = "C:\Users\Administrator\myVideo\processing\video.mp4";
$cmd = 'ffmpeg -i "' . $video .'" 2>&1 | wtee buffer.txt'; // wtee is a Windows version of tee

exec($cmd);
echo($cmd);
?>

If I run aa.php, which contains the above code, it won't work.

But if I run the $cmd that is being echoed out on the command prompt directly, the file buffer.txt is created and works.

I want to get the output of this:

$cmd = 'ffmpeg -i "' . $video .'"'

Into a variable like, e.g. $output.

This code, so far, prints blank:

exec($cmd,$output,$result);
print_r($output);
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
user1777711
  • 1,604
  • 6
  • 22
  • 32

2 Answers2

2

Like you mentioned, that your code works directly from command line but not from PHP CLI, is most likely because of log file permissions, which means, you must create a log file with correct permissions first, and then write your output there!

For example, from command line (not PHP) make sure that you delete your log file buffer.txt and run your PHP code again. Most likely it will not work as your file doesn't exist. Remember, when you run that command from command line directly, your log file, which is buffer.txt is created with special permissions (not only read/write, but also group and owner (Linux)). When PHP creates a file, by default, it uses nobody nobody for owner/group and you already have a log file (as you ran that same command from command line), which is created by other user (I suppose administrator), as a result you can be sure that PHP will not be able to use it, just because of the wrong file permissions. Try to see what is on your current log file owner/group by executing ls -ls in Linux or DIR /Q in Windows to get the idea.

Besides everything, make sure that you're not running PHP in safe mode, as a result exec might not work as you expect it, because it's disabled if PHP is in safe mode.

  1. shell_exec() (functional equivalent of backticks) This function is disabled when PHP is running in safe mode.

  2. exec() You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have components in the path to the executable. escapeshellcmd() is executed on the argument of this function.

You can check your server's PHP settings with the phpinfo() function.

Your code should rather look like this:

exec("$cmd 2>&1", $output);
log_conversion($config['logs_path']. '/' .$video_id. '_log.txt', $cmd);
log_conversion($config['logs_path']. '/' .$video_id. '_log.txt', implode("\n", $output));

What does that mean? If you say 2>&1 then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well.

Please read more about command redirections in articles Using command redirection operators for Windows or All about redirection for Linux.

Your log conversion function, could be something like this:

function log_conversion($file_path, $text)
{
    $file_dir = dirname($file_path);
    if( !file_exists($file_dir) || !is_dir($file_dir) || !is_writable($file_dir) )
        return false;

    $write_mode = 'w';
    if( file_exists($file_path) && is_file($file_path) && is_writable($file_path) )
        $write_mode = 'a';

    $handle = fopen($file_path, $write_mode);
    if( !$handle )
        return false;

    if( fwrite($handle, $text. "\n") == FALSE )
        return false;

    @fclose($handle);
}

It's very important to make sure that the log file you create is writable and has correct permissions!

I personally delete the log file in case it exists, to make sure that I have recent and reasonable log file.

I bet it will work either like this or with small tweaking!

If it doesn't work for you, please let me know and I will elaborate!

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
0

exec requires an array for the output variable. So try:

$output = array();
exec($cmd, $output, $result);

That will give you an array where each element is a line returned by your command.

Anthony DeSimone
  • 1,994
  • 1
  • 12
  • 21