1

I'm trying to convert a large (40mb) mov file to mp4 using ffmpeg and php.

But it gives me a 504 time-out error on server. Can i fix this without changing set_time_limit ?

Here is my php code.

shell_exec($this->getFFmpegPath() . ' -i ' . $path . ' -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -preset slower -crf 18 -vf "scale=trunc(in_w/2)*2:trunc(in_h/2)*2" ' . $convertedPath . '.mp4');

So, any ideas on how to convert this without a timeout ? Any help will be much appreciated.

Isuru Buddhika
  • 530
  • 1
  • 6
  • 16

1 Answers1

1

You might want to run the converting in the background. I found this question that addresses that, but please do some more investigate on that subject. You can then allow the user to refresh to see whether the process is finished.

It might give a better user experience as well (better then looking at a page loading for a long time. There are many places in the connection that may decide to consider it a timeout: php engine, webserver, proxy, browser, depending on the infra.

Also you should be REALLY careful with shell_exec. The chances of somebody on the web misusing it to do whatever they want with your machine are quite high. Maybe not in the way you are using it today (depending on where $path and $convertedPath come from), but maybe in a future update of this code where you allow the user to specify the resulting filename for example.

Community
  • 1
  • 1
pjvleeuwen
  • 4,215
  • 1
  • 18
  • 31
  • Thanks Paul, I did some research and find out i can run the process in background but now i need to get the status of the background process. – Isuru Buddhika Apr 07 '17 at 06:05
  • I have not tried something similar myself, so I do not know which issues you might run in to. The linked question suggests to store the PID. Since you may have multiple parralel users, I can imagine storing it in the session might be a good option. Then you want a page that shows the status. To check that the process is still running you can use `if (file_exists( "/proc/$pid" )){` as suggested by this answer: http://stackoverflow.com/a/9874592/2799887 – pjvleeuwen Apr 07 '17 at 09:19
  • When starting the converting as a background process you will not have access to the stdout and stderr anymore (and there also no ability to determine progress information). So it might be a good idea to write stdout and stderr to `$tempdir/$pid.stdout.log` and `$tempdir/$pid.stderr.log` or something similar. – pjvleeuwen Apr 07 '17 at 09:22
  • I'm planing to implement this solution http://stackoverflow.com/a/31482600/4626803 – Isuru Buddhika Apr 07 '17 at 09:52
  • I think hope the things mentioned above will still be helpful to you. That answer you referred to states "Periodically recheck the conversion status using web redirects, AJAX or Web Sockets." The remarks above are mainly about that specific part. Make sure to store the PID somewhere and make sure you retain access to stdout and stderr. This is needed to be able to feedback possible conversion errors to the user and also needed if you decide to give the user access to progress info (e.g. 45% completed). Does this mean your original question is answered? – pjvleeuwen Apr 07 '17 at 12:58