shell_exec echo'ing too fast or too late

北城以北 提交于 2021-02-05 11:57:27

问题


So I read throug this article: https://trac.ffmpeg.org/wiki/PHP and found this:

<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null &");
echo "Done.\n";

the code works almost perfectly fine, but the only thing that bothers me is, that because the shell_exec is being executed in the background (so there is no loading sign on the tab the whole time) all the echo's are being executed immediatly. This means, that the "Done" echo is being written before ffmpeg finished its task.

But when I remove the '&' the site does also what it is intended to do but now it waits until the shell_exec is finished until it echo's out the "Starting ffmpeg..."

So I kinda have the problem of figuring out how to get to such an order:

1) echo something 2) execute commad and wait until its finished. 3) echo again

I am a total beginner to php but have experience in programming overall. Please have mercy.

Thank you for your time!


回答1:


You might want to use flush before you do your shell_exec. There are a few caveats to using this which are well explained in the php documentation page. Hope this helps.

<?php
  echo "Starting ffmpeg...\n\n";

  ob_flush();
  flush();
  echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null");

  // a sleep could effectively be the same as your shell_exec operation
  // sleep(5);

  echo "Done.\n";
  ob_end_flush();


来源:https://stackoverflow.com/questions/39500922/shell-exec-echoing-too-fast-or-too-late

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!