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);
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.txtand 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 isbuffer.txtis created with special permissions (not only read/write, but also group and owner (Linux)). When PHP creates a file, by default, it usesnobody nobodyfor 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 executingls -lsin Linux orDIR /Qin Windows to get the idea.Besides everything, make sure that you’re not running PHP in safe mode, as a result
execmight not work as you expect it, because it’s disabled if PHP is in safe mode.shell_exec()(functional equivalent of backticks)This function is disabled when PHP is running in safe mode.
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:
What does that mean? If you say
2>&1then you are redirectingstderrto whereverstdoutis currently redirected to. Ifstdoutis going to the console thenstderris, too. Ifstdoutis going to a file thenstderris 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:
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!